"callExtension"雖然作為一個(gè)拓展程序,但是他與宿主程序擁有兩個(gè)不同的沙盒路徑,所以數(shù)據(jù)無法共享!但是iOS 8.0 以后,蘋果提供了一個(gè)叫做Appgroups的數(shù)據(jù)共享操作.
開啟AppGroups
- PS:拓展程序(CallExtension)與宿主程序(callText)必須選擇同一個(gè)"AppGroups"
使用AppGroups需要去蘋果開發(fā)者網(wǎng)站上配置"pofile"文件.具體操作可以參照以下鏈接
http://blog.csdn.net/songchunmin_/article/details/51316806
AppGroups數(shù)據(jù)讀寫
- 讀取數(shù)據(jù):
//獲取文件路徑
NSURL *fileUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"AppGroups名稱"];
NSString *filePath = [fileUrl.absoluteString substringFromIndex:(@"file://".length)];
NSString *filePathIdentification = [filePath stringByAppendingString:@"存儲(chǔ)的文件名稱"];
//讀取數(shù)據(jù)
NSArray *txlArr = [NSArray arrayWithContentsOfFile:filePathIdentification];
- 存入數(shù)據(jù)
//獲取文件路徑
NSURL *fileUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"AppGroups名稱"];
NSString *filePath = [fileUrl.absoluteString substringFromIndex:(@"file://".length)];
NSString *filePathIdentification = [filePath stringByAppendingString:@"存儲(chǔ)的文件名稱"];
//存入數(shù)據(jù)
NSArray *callArr = [NSArray new];
BOOL success = [callArr writeToFile:filePathIdentification atomically:YES];
- PS:?AppGroups存儲(chǔ)數(shù)據(jù)是一次最好不要超過20條,否則會(huì)存儲(chǔ)失敗.