使用分享扩展在应用之间传递数据
来源:互联网 发布:淘宝怎么注册企业账号 编辑:程序博客网 时间:2024/06/03 21:44
简单的研究了扩展,记录一下
1,创建一个分享扩展
File ----> New --- >Target 出现
在项目中就会出现
要传递数据的需要设置App Groups
在扩展中也设置同样的
调试扩展
在扩展中打开Container App
在扩展中设置跳转的代码
// 在扩展中打开 contaier app - (void)openApp { NSString *destURLString = @"doubiDemo://"; UIResponder* responder = self; while ((responder = [responder nextResponder]) != nil){ if([responder respondsToSelector:@selector(openURL:)] == YES){ [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:destURLString]]; [self.extensionContext completeRequestReturningItems:nil completionHandler:NULL]; } }}
我做的是 从其他应用里面传文件过来,,例如从微信将ppt 传到我们的应用
我简单的理解是 将微信中ppt 文件 复制到 扩展 ,我们的应用在从扩展中去取数据
我的示例代码
#import "ShareViewController.h"#import <MobileCoreServices/MobileCoreServices.h>@interface ShareViewController ()@end@implementation ShareViewController-(void)viewDidLoad{ [super viewDidLoad]; NSExtensionItem *extensionItem = [self.extensionContext.inputItems firstObject]; __weak __typeof(self) __weak_self = self; BOOL isExit = YES; for(NSItemProvider *itemProvider in [extensionItem attachments]) { if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeFileURL]) { isExit = NO; [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeFileURL options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) { NSURL *URL = (NSURL *)item; if ([URL isFileURL] && [__weak_self copyResToAppGroup:URL]) [__weak_self openApp ]; else [self.extensionContext completeRequestReturningItems:nil completionHandler:NULL]; }]; } } if (isExit) [self.extensionContext completeRequestReturningItems:nil completionHandler:NULL];}// 在扩展中打开 contaier app - (void)openApp { NSString *destURLString = @"doubiDemo://"; UIResponder* responder = self; while ((responder = [responder nextResponder]) != nil){ if([responder respondsToSelector:@selector(openURL:)] == YES){ [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:destURLString]]; [self.extensionContext completeRequestReturningItems:nil completionHandler:NULL]; } }}// 将文件复制到 app group- (BOOL) copyResToAppGroup:(NSURL *) sorURL{ NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *groupURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.DoubiDemo"]; groupURL = [groupURL URLByAppendingPathComponent:@"upload"]; BOOL isDir = NO; if (![fileManager fileExistsAtPath:groupURL.relativePath isDirectory:&isDir] && !isDir) [fileManager createDirectoryAtURL:groupURL withIntermediateDirectories:YES attributes:nil error:&error]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]]; NSString *name = [[sorURL lastPathComponent] stringByDeletingPathExtension]; NSString *ext = [sorURL pathExtension]; groupURL = [groupURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@.%@", name, currentDateStr, ext]]; return [fileManager copyItemAtURL:sorURL toURL:groupURL error:&error];}
注意:需要 #import <MobileCoreServices/MobileCoreServices.h>
0 0
- 使用分享扩展在应用之间传递数据
- 使用Bundle在Activity之间传递数据
- 使用Bundle在Activity之间传递数据
- 使用bundle在Activity之间传递数据
- 使用Intent在activity之间传递数据
- 使用全局变量在Activity之间传递数据
- 使用Bundle在fragment之间传递数据
- 利用粘贴板技术在应用之间传递数据。
- 在安卓中使用Bundle在Activity之间传递数据
- 在Service之间传递数据
- 在活动之间传递数据
- 在Activity之间传递数据
- 使用Server.Transfer在页面之间传递数据
- iPhone使用委托在不同的窗口之间传递数据
- iPhone使用委托在不同的窗口之间传递数据
- json的使用以及在页面之间数据的传递
- 使用bundle在多个activity之间传递数据
- Android开发之使用Intent在Activity之间传递数据
- hdu 4907 Task schedule
- C语言统计字符(空格、换行、制表)数
- 习题31 做出决定
- Source Insight 使用
- 使用httpclient发送get请求
- 使用分享扩展在应用之间传递数据
- JavaScript性能优化-数据存取
- Vue.js计算属性
- TTL电平、CMOS电平、RS232电平的区别
- Examining the Rooms hdu 3625
- solr配置文件schema文件中字段的介绍
- Qualcomm平台camera调试移植入门
- hdu 3549 Flow Problem【Dinic最大流】
- 51nod1486 大大走格子