我正在向我的应用程序添加一个应用程序组,以便在应用程序和手表之间共享一个 plist。当应用程序首次启动时,我曾经将 plist 从捆绑包复制到 Documents。但是对于手表,我现在正试图将其转换为保存到容器中,但它似乎总是空的。目标启用了应用程序组,并且我在代码中使用了正确的名称。可能出了什么问题?
老路
// COPY PLIST TO DOCUMENTS
NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"badger.com.vacations.plist"];
NSLog(@"plist path %@",destinationPath);
if (![fileManger fileExistsAtPath:destinationPath]){
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"badger.com.vacations.plist"];
[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
}
新方法 - 不工作
// COPY PLIST TO CONTAINER
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xxxxx.xxx.container"];
containerURL = [containerURL URLByAppendingPathComponent:@"name.com.data.plist"];
NSString *destinationPath= containerURL.path;
NSLog(@"destinationPath %@", containerURL);
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"name.com.data.plist"];
[fileManger copyItemAtPath:sourcePath toPath:containerURL.path error:&error];