通过以下方式向NSUserDefaults中保存数据:
- (void)saveTextByNSUserDefaults { NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xx.test"]; [shared setObject:_textField.text forKey:@"test"]; [shared synchronize]; }需要注意的是:
读数据
对应的读取数据方式:
- (NSString *)readDataFromNSUserDefaults { NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xx.test"]; NSString *value = [shared valueForKey:@"test"]; return value; } 3.2.1 通过NSFileManager共享数据NSFileManager在iOS7提供了containerURLForSecurityApplicationGroupIdentifier方法,可以用来实现app group共享数据。
存数据
- (BOOL)saveTextByNSFileManager { NSError *err = nil; NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xx.test"]; containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/good"]; NSString *value = _textField.text; BOOL result = [value writeToURL:containerURL atomically:YES encoding:NSUTF8StringEncoding error:&err]; if (!result) { NSLog(@"%@",err); } else { NSLog(@"save value:%@ success.",value); } return result; }读数据
- (NSString *)readTextByNSFileManager { NSError *err = nil; NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xx.test"]; containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/good"]; NSString *value = [NSString stringWithContentsOfURL:containerURL encoding:NSUTF8StringEncoding error:&err]; return value; }PS:
两个应用共同读取同一份数据,就会引发数据同步问题。WWDC2014的视频中建议使用NSFileCoordination实现普通文件的读写同步,而数据库可以使用CoreData,Sqlite也支持同步。
3.3 动态加载, 资源共享这个感兴趣的可以看下, 无关紧要~
和数据共享类似,Extension和Containing App很自然地会有一些业务逻辑上可以共用的代码,这时可以通过iOS8中刚开放使用的framework实现。苹果在中是这样描述的:
In iOS 8.0 and later, you can use an embedded framework to share code between your extension and its containing app. For example, if you develop image-processing code that you want both your Photo Editing extension and its containing app to share, you can put the code into a framework and embed it in both targets.
即将framework分别嵌入到extension和containing app的target中实现代码共享。但这样岂不是需要分别要将framework分别copy到extension和containing app的main bundle中?
参考extension和containing app数据共享,我试想能不能将framework只保存一份放在App Groups区域?
3.3.1 copy framework到App Groups在app首次启动的时候将framework放到App Groups区域:
- (BOOL)copyFrameworkFromMainBundleToAppGroup { NSFileManager *manager = [NSFileManager defaultManager]; NSError *err = nil; NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xx.test"]; NSString *sorPath = [NSString stringWithFormat:@"%@/Dylib.framework",[[NSBundle mainBundle] bundlePath]]; NSString *desPath = [NSString stringWithFormat:@"%@/Library/Caches/Dylib.framework",containerURL.path]; BOOL removeResult = [manager removeItemAtPath:desPath error:&err]; if (!removeResult) { NSLog(@"%@",err); } else { NSLog(@"remove success."); } BOOL copyResult = [[NSFileManager defaultManager] copyItemAtPath:sorPath toPath:desPath error:&err]; if (!copyResult) { NSLog(@"%@",err); } else { NSLog(@"copy success."); } return copyResult; } 3.3.2 使用framework - (BOOL)loadFrameworkInAppGroup { NSError *err = nil; NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xx.test"]; NSString *desPath = [NSString stringWithFormat:@"%@/Library/Caches/Dylib.framework",containerURL.path]; NSBundle *bundle = [NSBundle bundleWithPath:desPath]; BOOL result = [bundle loadAndReturnError:&err]; if (result) { Class root = NSClassFromString(@"Person"); if (root) { Person *person = [[root alloc] init]; if (person) { [person run]; } } } else { NSLog(@"%@",err); } return result; }经过测试,竟然能够加载成功。
对比一下, 有如下总结: