2015-04-03 26 views
1

我正在將應用程序組添加到我的應用程序中,以在應用程序和手錶之間共享單個plist。我曾經將軟件包中的plist複製到應用程序第一次啓動時的文檔中。但是現在我正在試圖將它轉換成保存到容器中,但它總是顯得空虛。目標已啓用應用程序組,並且在我的代碼中使用了正確的名稱。可能會出現什麼問題?將plist複製到應用程序組容器 - iOS 8

老方法

// 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]; 
+0

STK的回答是不錯的,但我看不出有任何理由爲什麼你不應該直接使用plist文件。究竟發生了什麼?沒有文件被寫入?空文件被寫入?錯誤中有什麼? – gregheo 2015-04-04 03:53:23

回答

1

您不容分享的plist作爲文件(或者我只是鴕鳥政策瞭解此功能),而不是你只需生成一個新的NSUserDefaults實例,它可以在目標之間共享。

看一看這裏: https://devforums.apple.com/message/977151#977151

或Apple文檔

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

  • 在蘋果的會員中心,在 '標識符>應用組' 註冊一個新的應用程序組(如group.de.myApp.sharedGroup)
  • 在Apple會員中心的「標識符>應用程序ID」下,刷新您的應用程序ID以查找需要使用的目標克至使用App組功能
  • 重新生成所有需要的資源調配配置文件,並讓他們進入的Xcode
  • 回到Xcode中:在「能力」在你的每一個目標,需要共享數據,設置「應用組」對和添加先前註冊的App組。
  • 聊到共享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);