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