如何更新 iPhone 应用程序中的数据

发布于 2024-09-15 21:40:58 字数 148 浏览 10 评论 0原文

我创建了一个应用程序,数据存储在 plist 中。现在我可以通过哪些方式更新数据?也许如果我创建一个网页并从中检索数据而不是存储在 plist 中?或者有什么方法可以在插入 iTunes 时更新我的​​数据?或者还有其他建议吗?我想要实现的是在用户下载应用程序后更新我的数据的方法。

I create an app, with data store in plist. Now what are the ways I can update the data? Maybe if I create a webpage and retrieve my data from it instead of storing in plist? Or is there a way I can update my data when I plug into iTunes? Or any other suggestion? What I want to achieve is a way of updating my data once user have download the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2024-09-22 21:40:58
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [path objectAtIndex:0];
NSString *filePath = [dir stringByAppendingPathComponent:@"my.plist"];

NSMutableDictionary *localDictionary;
NSUrl *remoteUrl = [NSURL URLWithString:@"http://server.com/my.plist"];
NSMutableDictionary *remoteDictionary = [NSMutableDictionary dictionaryWithContentsOfURL:remoteUrl];
if(remoteDictionary != nil) {
     [remoteDictionary writeToFile:filePath atomically:YES];
     localDictionary = remoteDictionary;
}
else {
     localDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
     if(localDictionary == nil) localDictionary = [NSMutableDictionary dictionary];
}

这将尝试从 http://server.com/ 获取 my.plist 的副本,成功后会将其写入磁盘并指向 localDictionary,出错时它将尝试从磁盘读取(例如,在有连接时提前下载),如果磁盘上没有文件,它将创建一个空的NSMutableDictionary。无论哪种方式,localDictionary最终都会指向一个NSMutableDictionary

这非常简单,每次运行代码时都会请求该文件。例如,您可以使用 NSFileModificationDate (通过 NSFileManger attributeOfItemAtPath:error: 获得)和 NSDate 来确定是否需要更新。或者对于大文件版本控制也很有意义,拥有一个包含较大文件版本的小文件 - 从服务器获取小文件(例如使用 NSString),检查缓存的版本文件是否包含较低的版本号,如果是这样,则获取大文件。这始终取决于内容的刷新频率。

查看完整文档: NSDictionary< /a>, NSString< /a>, NSFileManager< /a>, NSDate< /a>

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [path objectAtIndex:0];
NSString *filePath = [dir stringByAppendingPathComponent:@"my.plist"];

NSMutableDictionary *localDictionary;
NSUrl *remoteUrl = [NSURL URLWithString:@"http://server.com/my.plist"];
NSMutableDictionary *remoteDictionary = [NSMutableDictionary dictionaryWithContentsOfURL:remoteUrl];
if(remoteDictionary != nil) {
     [remoteDictionary writeToFile:filePath atomically:YES];
     localDictionary = remoteDictionary;
}
else {
     localDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
     if(localDictionary == nil) localDictionary = [NSMutableDictionary dictionary];
}

This will try to obtain a copy of my.plist from http://server.com/, on success it will write it to the disk and point localDictionary to it, on error it will try to read from disk (e.g. downloaded earlier when there was a connection), if there is no file on disk it will create a empty NSMutableDictionary. Either way localDictionary will point to a NSMutableDictionary in the end.

This is very simple and will request the file every time the code is run. You could e.g. use NSFileModificationDate (obtained by NSFileManger attributesOfItemAtPath:error:) and NSDate to determine if a update is necessary. Or for big files versioning would make sense as well, having a small file containing the version of the bigger file - getting the small file from the server (e.g. with NSString), checking whether the cached version file contains a lower version number, if so get the big file. It always depends on how often the content is refreshed.

See full documentation: NSDictionary, NSString, NSFileManager, NSDate

一念一轮回 2024-09-22 21:40:58

Plist 不容易修改,因为您必须将它们读回内存,进行更改并将它们写回文件。

要编写 plist,请查看 NSDictionary 或 NSArray 的 writeToFile:atomically: 方法。请记住,您的 plist 基本上是由 NSArrays、NSDictionaries、NSStrings 和 NSData 组成的 XML,并且您总是希望写出根节点。

或者寻找其他存储数据的方式:用户默认值或 sqlite 数据库。仅在某些情况下将数据存储在“云中”才有意义,例如您想在不同设备之间共享数据或想要远程更新数据。如果可能的话最好避免它。

Plists are not easily modifiable since you have to read them back into memory, make the changes and write them back to the file.

For writing the plists look at NSDictionary's or NSArray's writeToFile:atomically: method. Remember your plist is basically an XML made up from NSArrays, NSDictionaries, NSStrings and NSData, and you'll always want to write out the root node.

Alternatively look for other ways of storing your data: user defaults or sqlite databases. Storing data 'in the cloud' makes sense only in some cases, like if you want to share it between different devices or want to update it remotely. It's best to avoid it if possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文