【发布时间】:2015-04-23 14:39:57
【问题描述】:
我有一个应用程序可以保存和使用 plist 文件中的数据。我正在开发一个 WatchKit 扩展,它需要访问同一个 plist 文件来显示数据并保存到文件中。我知道我需要使用应用组,但我不知道如何在 iOS 应用和 WatchKit 扩展之间共享 plist。
这是我目前在 iOS 应用中保存到 plist 的方式。
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"locations.plist"];
BOOL fileExists = [fileManager fileExistsAtPath:docPath];
NSError *error = nil;
if (!fileExists) {
NSString *strSourcePath = [[NSBundle mainBundle]pathForResource:@"locations" ofType:@"plist"];
[fileManager copyItemAtPath:strSourcePath toPath:docPath error:&error];
}
NSString *path = docPath;
NSMutableArray *plistArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.locationNameTextField.text, @"locationName", latString, @"latitude", longString, @"longitude", nil];
[plistArray insertObject:locationDictionary atIndex:0];
[plistArray writeToFile:docPath atomically:YES];
【问题讨论】:
标签: ios objective-c watchkit ios-app-group