iOS 系统分享 ShareExtension

1,创建Share Extension
(Share Extension:扩展不能单独创建,必须依赖于应用工程项目)


创建

2,配置Share Extension

允许发送的数据类型:text、url、image、mp3、mp4、pdf、word、excel、ppt


plist

3,自定义UI
新建一个控制器,继承UIviewcontroller,注意在info.plist里面修改成自定义控制器的类名。

4,文件解析

NSExtensionItem *etItem = [self.extensionContext.inputItems firstObject];
    NSItemProvider *etItemProvider = [[etItem attachments] firstObject];

    if (etItemProvider) {
        NSArray *registeredTypeIdentifiers = etItemProvider.registeredTypeIdentifiers;
        NSLog(@"%@", registeredTypeIdentifiers);
    }

    if ([etItemProvider hasItemConformingToTypeIdentifier:@"public.file-url"]){
        //文件
        [etItemProvider loadItemForTypeIdentifier:@"public.file-url" options:nil completionHandler:^(id  _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];
                NSLog(@"file路径:%@", urlStr);
                NSData *mp3_data = [NSData dataWithContentsOfURL:share_url];
                NSLog(@"file_data的数据:%@", mp3_data);
                
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self.file_NameLb.text = string;
                });
                
            }
            
        }];
        
        
        
    } else if ([etItemProvider hasItemConformingToTypeIdentifier:@"public.image"]) {
        //图片
        for (NSExtensionItem *obj in self.extensionContext.inputItems) {
            for (NSItemProvider *itemProvider in obj.attachments) {
                if ([itemProvider hasItemConformingToTypeIdentifier:@"public.image"]){
                    [etItemProvider loadItemForTypeIdentifier:@"public_image" options:nil completionHandler:^(id  _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]]];
                                self.file_NameLb.text = url;
                            });
                        }

                    }];
                }
            }
        }
        
    } else {
        
        
    }
    

5,跳转需要

- (void)goConnationWithUrlSTr:(NSString *)urlStr {
   UIResponder *responder = self;
   while (responder) {
       SEL openSelector = NSSelectorFromString(@"openURL:");
       if([responder respondsToSelector:openSelector]) {
           [responder performSelector:openSelector withObject:[NSURL URLWithString:urlStr] afterDelay:1.0];
           break;
       }
       responder = [responder nextResponder];
   }
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    if ([[url absoluteString] hasPrefix:@"mp3share://"]) {
        NSLog(@"mp3跳转url:mp3share://");


     }

  return YES;
}

你可能感兴趣的:(iOS 系统分享 ShareExtension)