简体   繁体   English

从存储在服务器上的XML文件中检索数据并加载iPhone

[英]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. 无线电台的所有信息,例如:名称,频率,当然还有流链接,都由后端生成并存储在XML文件中。 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? 我是否必须通过sftp或https下载?有没有人知道如何执行此任务?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you in advance. 先感谢您。

Granit 格兰尼特

You could use NSURLConnection , a good implementation here .To do what you want to do, plain and simple 你可以使用NSURLConnection的 ,一个好的执行这里 。要做到你想做的事,简单明了什么

NSData *responseData =  [NSData dataWithContentsOfURL:url];

This happens asynchronously ie, 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 在这里查看XML解析

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. 在willSendRequestForAuthenticationChallenge方法中提供您的凭据,或者如果您想要更好的实现,请参阅此内容 ,它是一个同步NSURLConnection子类imp,它也处理身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM