WWDC2014之App Extensions学习笔记
本文转载至 http://www.cocoachina.com/industry/20140627/8960.html
extension是iOS8新开放的一种对几个固定系统区域的扩展机制,它可以在一定程度上弥补iOS的沙盒机制对应用间通信的限制。
“”转自王中周的技术博客
- @interface UIViewController(NSExtensionAdditions) <NSExtensionRequestHandling>
- // Returns the extension context. Also acts as a convenience method for a view controller to check if it participating in an extension request.
- @property (nonatomic,readonly,retain) NSExtensionContext *extensionContext NS_AVAILABLE_IOS(8_0);
- @end
- //通过openURL的方式启动Containing APP
- - (void)openURLContainingAPP
- {
- [self.extensionContext openURL:[NSURL URLWithString:@"appextension://123"]
- completionHandler:^(BOOL success) {
- NSLog(@"open url result:%d",success);
- }];
- }
- //让隐藏的插件重新显示
- - (void)showTodayExtension
- {
- [[NCWidgetController widgetController] setHasContent:YES forWidgetWithBundleIdentifier:@"com.wangzz.app.extension"];
- }
- //隐藏插件
- - (void)hiddeTodayExtension
- {
- [[NCWidgetController widgetController] setHasContent:NO forWidgetWithBundleIdentifier:@"com.wangzz.app.extension"];
- }
- TARGETS-->AppExtensionDemo-->Capabilities-->App Groups
- TARGETS-->TodayExtension-->Capabilities-->App Groups
- - (void)saveTextByNSUserDefaults
- {
- NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.wangzz"];
- [shared setObject:_textField.text forKey:@"wangzz"];
- [shared synchronize];
- }
- - (NSString *)readDataFromNSUserDefaults
- {
- NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.wangzz"];
- NSString *value = [shared valueForKey:@"wangzz"];
- return value;
- }
- - (BOOL)saveTextByNSFileManager
- {
- NSError *err = nil;
- NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.wangzz"];
- 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.wangzz"];
- containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/good"];
- NSString *value = [NSString stringWithContentsOfURL:containerURL encoding:NSUTF8StringEncoding error:&err];
- return value;
- }
- - (BOOL)copyFrameworkFromMainBundleToAppGroup
- {
- NSFileManager *manager = [NSFileManager defaultManager];
- NSError *err = nil;
- NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.wangzz"];
- 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;
- }
- - (BOOL)loadFrameworkInAppGroup
- {
- NSError *err = nil;
- NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.wangzz"];
- 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;
- }
- - (void)logAppPath
- {
- //app group路径
- NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.wangzz"];
- NSLog(@"app group:\n%@",containerURL.path);
- //打印可执行文件路径
- NSLog(@"bundle:\n%@",[[NSBundle mainBundle] bundlePath]);
- //打印documents
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSLog(@"documents:\n%@",path);
- }
- 2014-06-23 19:35:03.944 AppExtensionDemo[7471:365131] app group:
- /private/var/mobile/Containers/Shared/AppGroup/89CCBFB1-CA5E-4C7F-80CB-A3EB9E841816
- 2014-06-23 19:35:03.946 AppExtensionDemo[7471:365131] bundle:
- /private/var/mobile/Containers/Bundle/Application/1AC73797-A3BB-4BDE-A647-3D083DA6871A/AppExtensionDemo.app
- 2014-06-23 19:35:03.948 AppExtensionDemo[7471:365131] documents:
- /var/mobile/Containers/Data/Application/E5E6E516-0163-4754-9D10-A5F6C33A6261/Documents
- Jun 23 19:37:49 autonavis-iPad com.foogry.AppExtensionDemo.TodayExtension[7638] <Warning>: app group:
- /private/var/mobile/Containers/Shared/AppGroup/89CCBFB1-CA5E-4C7F-80CB-A3EB9E841816
- Jun 23 19:37:49 autonavis-iPad com.foogry.AppExtensionDemo.TodayExtension[7638] <Warning>: bundle:
- /private/var/mobile/Containers/Bundle/Application/596717B7-7CB8-4F53-BCD4-380F34ABD30F/AppExtensionDemo.app/PlugIns/com.foogry.AppExtensionDemo.TodayExtension.appex
- Jun 23 19:37:49 autonavis-iPad com.foogry.AppExtensionDemo.TodayExtension[7638] <Warning>: documents:
- /var/mobile/Containers/Data/PluginKitPlugin/57581433-3DBD-4930-971F-78D30C150E8A/Documents
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。