2014-10-10 80 views
-1

我在苹果应用程序商店有一个测验应用程序。问题存储在Plist文件中。我正在寻找的方法是通过下载新版本来更新Plist文件,而不必每次有新的问题时都要提交更新通过下载更新Plist文件iOS

有没有人知道一个体面的教程可以帮助我?

非常感谢。

+0

[DOWNLOADFile](http://stackoverflow.com/questions/19101179/download-plist-from-server)___ [deletionFile](http://stackoverflow.com/questions/15505529/delete-file-obj- c) – 2014-10-10 15:50:42

回答

0

我不知道有关的教程,但步骤来实现你的描述非常简单:

  1. 到远程数据创建一个URL请求
  2. 解析返回的数据
  3. 写解析数据到一个新的地方的plist

如:

// Create a NSURL to your data 
NSString *dataPath = @"www.mydata.com/path"; 
NSURL *dataURL = [NSURL URLWithString:dataPath]; 

// Create an asycnhronous request along with a block to be executed when complete 
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:dataURL] 
            queue:[[NSOperationQueue alloc] init] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 
    if (error) { 
     NSLog(@"%@", error.localizedDescription); 
     // Handle the request error 
    } 
    else { 
     // We have some data, now we need to serialize it so that it's usable 
     NSError *serializationError; 
     NSDictionary *serializedDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&serializationError]; 
     if (serializationError) { 
      NSLog(@"%@", serializationError.localizedDescription); 
      // Handle the serialization error 
     } 
     else { 
      // We have a serialized NSDictionary, now we just want to write it 
      // First we create the path to write it to, eg. uers documents directory 
      NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
      // Then write it 
      BOOL success = [serializedDictionary writeToFile:documentsDirectory.path atomically:YES]; 
      if (!success) { 
       NSLog(@"Error writing file"); 
      } 
     } 
    } 
}]; 

注意:您可能想考虑将数据存储在远程数据库中,如Parse。通过这种方式,您可以查询新问题,只下载这些问题,以免使用不必要的带宽。您也可以考虑使用Core Data来维护您的本地数据,而不是将它写入plist。主要优点是您无需将整个plist串行化为内存以便使用它 - 您只需查询所需的特定问题即可。

希望这会有所帮助。

+0

我已经使用Parse推送通知,但不知道它也可以这样做。关于退出Plist的唯一问题是已经存在并迁移它的3000个问题。 – nobbsy 2014-10-10 16:27:44

+0

数据驱动应用程序的一个常见做法是使用plist作为起点,并将其输入到“核心数据”中。然后,您可以合并定期的服务器查询以查看是否有新内容,如果有,使用新的远程数据更新'Core Data' – chrysAllwood 2014-10-10 16:32:48

+0

因此,示例流程可能是: - 启动时检查是否已填充“Core Data” (例如运行一个没有谓词的查询) - 如果它返回0个对象,那么这是第一次启动,所以序列化你的plist,然后迭代每个问题,为每个问题插入一个新的NSManagedObject - 否则,你已经完成了这项工作 - “核心数据”很好用 - 在上次更新后更新的所有问题的查询解析(通过存储在NSUserDefaults中的'NSDate'记录此问题) - 如果有任何问题结果,然后为它们创建新的NSManagedObjects或更新当前的 – chrysAllwood 2014-10-10 16:42:30