Ios 异步下载
我对Objective C有点陌生,我想知道是否有人能帮我(或给我指点教程)将.plist文件下载到我的iOS应用程序中,然后阅读它,我需要异步下载该文件,这样下载时不会暂停应用程序 我目前使用的代码是:Ios 异步下载,ios,objective-c,download,plist,asynchronous,Ios,Objective C,Download,Plist,Asynchronous,我对Objective C有点陌生,我想知道是否有人能帮我(或给我指点教程)将.plist文件下载到我的iOS应用程序中,然后阅读它,我需要异步下载该文件,这样下载时不会暂停应用程序 我目前使用的代码是: //UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]]; 我在网上看了很多,找不到任何教程,我知道这很简单,但非常感谢你的帮助。 谢谢。您可以用它来实现这一点 或
//UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]];
我在网上看了很多,找不到任何教程,我知道这很简单,但非常感谢你的帮助。
谢谢。您可以用它来实现这一点
或
您可以简单地使用,如:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]];
});
如果您想知道您的应用程序已经完成下载,并且在此之后您想执行一些操作,那么在这种情况下,您需要编写自己的自定义委托,该委托将在应用程序完成下载时更新。但对于异步下载,您可以使用Midhun提到的GCD。请参阅此下面是使用
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
}
}];
使用completion hander尝试此下载
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..
}
我怎么知道它什么时候完成下载?你可以在
初始化后使用nsnotificationcenter
发布通知,内容为URL
,或者你甚至可以调用[self doneLoading]
@johannburges:很高兴:)你需要从网上获取文件,或者它在你的计算机上?