iOS扩展--Share Extension
2018-05-19 本文已影响117人
墨凌风起
查看本文之前,建议先看以下同行的分享,了解什么是iOS的扩展,及如何新建扩展
https://www.jianshu.com/p/863ce6729455
虽然网上的关于share Extension的资料很多,但多千篇一律。所以我将自己学习过程中遇到的问题与大家分享以下,希望可以帮到大家。
首先创建一个工程,新建扩展Share, 展开share 目录下的info.plist文件
屏幕快照 2018-05-19 下午6.33.06.png1.我使用的自定义分享界面 ,所以将NSExtensionMainStoryboard换成NSExtensionPrincipalClass,MainstoryBoard换成MineShareViewController
2.NSExtensionAttributes 下添加
NSExtensionActivationSupportsImageWithMaxCount:9 ->表示最多分享9张图片,选中超过9张是,share Extension 栏隐藏app,
NSExtensionActivationSupportsMovieWithMaxCount:1->视屏,同上
NSExtensionActivationSupportsWebURLWithMaxCount:1
如果分享之前需要预览分享的数据,至少得判断该数据类型,图片展示,视频播放,等等
shareExtension 部分:
//数据解析
NSExtensionItem * extensionItem = [self.extensionContext.inputItems firstObject];
NSItemProvider * itemProvider = [[extensionItem attachments] firstObject];
self.shareType = nil;
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.image"]) //图片
{
for (NSExtensionItem *obj in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in obj.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.image"]){
[itemProvider loadItemForTypeIdentifier:public_image options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSURL class]])
{
//从相册中分享,此时图片已经在相册中,取到的是路径Url
NSURL *imageUrl = (NSURL *)item;
NSString *url = imageUrl.absoluteString;
dispatch_async(dispatch_get_main_queue(), ^{
//imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]];
});
}else{
//截屏后点击分享,此时图片还未入库,所以拿到的是Image
UIImage *image = (UIImage *)item;
dispatch_async(dispatch_get_main_queue(), ^{
//imageView.image = image;
});
}
}];
}
}
}
}else if ([itemProvider hasItemConformingToTypeIdentifier:@"public.movie"]){//视频
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[itemProvider loadItemForTypeIdentifier:@"public.movie" options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSURL class]])
{
NSURL *movieUrl = (NSURL *)item;//move url
}
}];
});
}else if ([itemProvider hasItemConformingToTypeIdentifier:@"public.file-url"]){//文件
[itemProvider loadItemForTypeIdentifier:@"public.file-url" options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSURL class]])
{
NSURL *share_url = (NSURL *)item;
NSString *urlStr =share_url.absoluteString;//file路径
NSString *fileName = urlStr.lastPathComponent;
NSString *string = [fileName stringByRemovingPercentEncoding];
}
}];
}else if ([itemProvider hasItemConformingToTypeIdentifier:@"public.url"]){//url连接
[itemProvider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSURL class]])
{
NSURL *share_url = (NSURL *)item;
NSString *urlStr =share_url.absoluteString;//file路径
NSString *fileName = urlStr.lastPathComponent;
NSString *string = [fileName stringByRemovingPercentEncoding];
}
}];
}else if ([itemProvider hasItemConformingToTypeIdentifier:@"public.plain-text"]){ //文字
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[itemProvider loadItemForTypeIdentifier:@"public.plain-text" options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSString class]])
{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *content = (NSString *)item;
//titleLabel.text =content;
});
}
}];
});
}
//数据拼接
//此处用到app Group数据共享
//TARGET->Capblities->App groups 打开 +一个新的账号(group.appid)
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.appid"];
//writeToURL写入containerURL
//数据传送
NSString *str =[NSString stringWithFormat:@"XXXX"];//跳转时需要传送的数据,文件类型,校验码
NSURL *appBUrl = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:appBUrl];
[self.extensionContext completeRequestReturningItems:@[] completionHandler:^(BOOL expired) {
}];
宿主app
//appdelegate
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSString *urlStr = url.absoluteString;//上面跳转时传入的数据,可在此处校验
return YES;
}
//获取数据
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.appid"];
// containerURL.path
//为了避免数据的混乱,可在share中按照自定义的协议将数据拼接containerURL,然后在宿主app的解析