[英]Updating Plist file via download iOS
我在Apple應用商店上有一個測驗應用。 問題存儲在Plist文件中。 我想要做的是找到一種方法,可以通過下載新版本來更新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序列化到內存中即可使用它-您只需查詢所需的特定問題即可。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.