我是Objective C的新手,我想知道是否有人可以帮助我(或指向我的教程)将.plist文件下载到我的iOS应用程序然后读取它,我需要异步下载的文件所以它下载时不会暂停应用程序。
我正在使用的当前代码是:
//UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]];
我在网上看了很多,找不到任何教程,我知道这很简单但是你的帮助会非常感激。 感谢。
答案 0 :(得分:0)
您可以使用NSURLConnection来实现此目标。
或强>
您可以简单地使用GCD,例如:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]];
});
答案 1 :(得分:0)
如果您想知道您的应用程序已完成下载,之后您想要执行某些操作,那么在这种情况下,您需要编写自己的自定义代理,该代理将在应用程序下载完成后更新。但对于异步下载,您使用Midhun提到的GCD。请参阅此How to write Custom Delegate?
答案 2 :(得分:0)
以下是使用NSURLConnection
的实施草图。请注意,下载完成后会调用completionHandler
(正常或错误),您可以从那里调用处理Array
的函数。
此处提供的其他答案也是有效的,最终您需要确定最适合您情况的答案。
NSURLRequest* theRequest = [NSURL URLWithString:@"file to url"];
[NSURLConnection sendAsynchronousRequest:theRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* theError) {
if (theData) {
NSError* err = nil;
id Array [NSPropertyListSerialization propertyListWithData:theData
options:NSPropertyListImmutable
format:NULL
error:&err];
if ([Array isKindOfClass:[NSArray class]) {
// Do whatever you need with downloaded array
} else {
// Error -- wrong data, check err
}
} else {
// Error while downloading, check theError
}
}];
答案 3 :(得分:0)
尝试使用完成处理程序下载。
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error)
{
//do your stuff when downloading complete..
}