0

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

Granit
  • 1,261
  • 6
  • 19
  • 35

1 Answers1

2

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.

Community
  • 1
  • 1
Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • In my case i have a username and password on the server, where do i have to insert them? – Granit Sep 26 '13 at 11:57
  • Is it basic authentication or form based authentication? – Satheesh Sep 26 '13 at 12:10
  • It is a basic authentication – Granit Sep 26 '13 at 12:15
  • @Grangji Is the solution ok? were you able to do it? – Satheesh Sep 26 '13 at 13:35
  • Sorry, i wasn't at the computer until now. I tried your answer and the log returns: 2013-09-26 18:15:47.727 ShqipCom[4270:c07] received authentication challenge 2013-09-26 18:15:47.728 ShqipCom[4270:c07] credential created 2013-09-26 18:15:47.728 ShqipCom[4270:c07] responded to authentication challenge 2013-09-26 18:15:47.828 ShqipCom[4270:c07] previous authentication failure Does this means it's failing to auth? – Granit Sep 26 '13 at 16:17
  • Also when i log NSLog(@"%@", responseData); i get the following message: 2013-09-26 18:37:30.789 ShqipCom[4403:c07] <3c21444f 43545950 45204854 4d4c2050 55424c49 4320222d 2f2f4945 54462f2f 44544420 48544d4c 20322e30 2f2f454e 223e0a3c 48544d4c 3e0a3c48 4541443e 0a3c5449 544c453e 34303120 41757468 6f72697a 6174696f 6e205265 71756972 65643c2f 5449544c 453e0a3c 2f484541 443e0a3c 424f4459 3e0a3c48 313e4175 74686f72 697a6174 696f6e20 52657175 772e0a20 20202d2d 3e0a> – Granit Sep 26 '13 at 16:39
  • Yes previous authentication failure means the authentication failed one ore more times. So you are getting some data from the server so convert the data to a string NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); – Satheesh Sep 27 '13 at 05:04
  • Thank you very much, i was able to connect to the server. :) – Granit Sep 27 '13 at 07:17
  • @saatheeshwaran Just one additional question: what if i have a structure like this: http://domain.com/apps/source/data/file.xml and each of the directories are password protected, how do i give the right credentials for each folder? – Granit Sep 27 '13 at 12:09
  • For every authentication challenge from your server , the delegate is called. So if the password is same you can hard code it there per user session – Satheesh Sep 27 '13 at 13:27