Granit
Granit

Reputation: 1281

Retrieve data from XML file stored on server and load iPhone

I am developing an app that plays radio stations. The links for the radio stations are stored on the server and added via a back-end solution.

All information for a radio station such as: name, frequency and of course the stream link are generated by the backend and stored in a XML file. This is my first project so I don't have a clear idea how to download that file, which is stored on a password protected directory.

Do i have to download it via sftp or https?And does anyone have an idea how to perform this task?

Any help would be greatly appreciated.

Thank you in advance.

Granit

Upvotes: 0

Views: 893

Answers (1)

Satheesh
Satheesh

Reputation: 11276

You could use NSURLConnection, a good implementation here.To do what you want to do, plain and simple

NSData *responseData =  [NSData dataWithContentsOfURL:url];

This happens asynchronously i.e, non - blocking call

 dispatch_async(queue, ^{

                NSData *responseData =  [NSData dataWithContentsOfURL:url];

                dispatch_sync(dispatch_get_main_queue(), ^{

                  // handle your responesData here.
                  //convert data to nsstring or something then use a parser to parse details.
                });
            });                       

Look here for XML Parsing

For basic authentication try this

NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:30.0];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];

// NSURLConnection Delegates
 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{    if ([challenge previousFailureCount] == 0) {
        NSLog(@"received authentication challenge");
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER"
                                                                    password:@"PASSWORD"
                                                                 persistence:NSURLCredentialPersistenceForSession];
        NSLog(@"credential created");
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        NSLog(@"responded to authentication challenge");    
    }
    else {
        NSLog(@"previous authentication failure");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    ...
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    ...
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    ...
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    ...
}

Give your credentials in the willSendRequestForAuthenticationChallenge method or if you want an even better implementation see this, it is an synchronous NSURLConnection subclass imp that also handles authentication.

Upvotes: 2

Related Questions