我正在开发一个播放广播电台的应用。无线电台的链接存储在服务器上,并通过后端解决方案添加。
无线电台的所有信息,例如:姓名,频率,当然还有流链接,由后端生成并存储在XML文件中。这是我的第一个项目,所以我不清楚如何下载该文件,该文件存储在受密码保护的目录中。
我是否必须通过sftp或https下载?有没有人知道如何执行此任务?
非常感谢任何帮助。
提前谢谢。
花岗岩
答案 0 :(得分:2)
你可以使用 NSURLConnection ,一个很好的实现here。做你想做的事,简单明了
NSData *responseData = [NSData dataWithContentsOfURL:url];
这是异步发生的,即非阻塞呼叫
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.
});
});
在这里查看XML Parsing
对于基本身份验证,请尝试此
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 {
...
}
在willSendRequestForAuthenticationChallenge方法中提供您的凭据,或者如果您想要更好的实现,请参阅this,它是一个同步NSURLConnection子类imp,它也处理身份验证。