1

我正在向我的应用程序添加一个应用程序组,以便在应用程序和手表之间共享一个 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];
4

1 回答 1

1

您不能将 plist 共享为文件(或者我根本不知道此功能),而是生成一个新的 NSUserDefaults 实例,该实例可在目标之间共享。

看看这里: https ://devforums.apple.com/message/977151#977151

或 Apple 文档

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

  • 在 Apple 会员中心,在“标识符 > 应用组”下注册一个新的应用组(例如 group.de.myApp.sharedGroup)
  • 在 Apple 会员中心的“标识符 > 应用程序 ID”下,刷新您的应用程序 ID,以获取需要共享以使用应用程序组功能的目标
  • 重新生成所有需要的 Provisioning Profiles 并将它们放入 Xcode
  • 返回 Xcode:在需要共享数据的每个目标中的“功能”下,将“应用程序组”设置为打开并添加之前注册的应用程序组。
  • 像这样与可共享的 NSUserDefaults 容器交谈:

存储东西:

NSUserDefaults *groupDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.myApp.sharedGroup"];
[groupDefaults setInteger:1337 forKey:@"testEntry"];
[groupDefaults synchronize];

阅读资料:

NSUserDefaults *groupDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.myApp.sharedGroup"];
NSInteger testEntry = [groupDefaults integerForKey:@"testEntry"];
NSLog(@"testEntry: %ld", (long)testEntry);
于 2015-04-03T20:47:18.017 回答