美文网首页
iOS使用ShareExtension获取分享的图片

iOS使用ShareExtension获取分享的图片

作者: ufogxl | 来源:发表于2021-09-28 23:44 被阅读0次

    一、新建shareExtension

    Xcode菜单选择file-new-target,在弹出的地方找到shareExtension并选择,填写好对应的信息后,会生成一个新的target

    选择Share Extension 新生成的target概览

    生成之后在工程文件中选中新建的extension,在Signing & Capabilities中添加App Groups:


    新建的target
    App Group

    添加一个group-id(主app也需要新建一个app-group,和此处一致):


    Group-ID

    在info.plist中配置文件接收的类型


    info.plist

    最后写处理的逻辑

    ShareViewController.swift

    
    let APP_GROUP_ID = "之前新建的app-group-id"
    
    override func viewDidLoad() {
        guard let extensionItem = extensionContext?.inputItems[0] as? NSExtensionItem else {
            didSelectPost()
            return
        }
        guard let provider = extensionItem.attachments?.first else {
            didSelectPost()
            return
        }
        if provider.hasItemConformingToTypeIdentifier("public.image"){
            provider.loadItem(forTypeIdentifier: "public.image", options: nil){[weak self] (item,error)  in
                if error != nil{
                    print(error!)
                }
                if item is URL{
                    self?.imageData = try? Data(contentsOf: item as! URL)
                }else{
                    self?.imageData = (item as? UIImage)?.jpegData(compressionQuality: 1.0)
                }
                self?.takeImageToMainApp()
            }
        }else if provider.hasItemConformingToTypeIdentifier("public.url"){
            provider.loadItem(forTypeIdentifier: "public.url", options: nil){[weak self] (item,error)  in
                if error != nil{
                    print(error!)
                }
                if let data = try? Data(contentsOf: item as! URL){
                    if let _ = UIImage(data: data){
                        self?.imageData = data
                    }
                }
                self?.takeImageToMainApp()
            }
        }
    }
    
    func takeImageToMainApp(){
        if imageData != nil{
            let defaults = UserDefaults.init(suiteName: APP_GROUP_ID)
            defaults?.setValue(imageData, forKey: "share_image")
            var responder = self.next
            while responder != nil{
                let sel = Selector("openURL:")
                if responder!.responds(to: Selector("openURL:")){
                    responder!.perform(sel, with: URL(string:"你的主app配置的一个URLScheme://"))
                    break
                }
                responder = responder?.next
            }
        }
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }
    

    二、在主APP处理

    AppDelegate.swift

    func applicationDidBecomeActive(_ application: UIApplication) {
        //读取shareExtension分享的图片
        let defaults = UserDefaults.init(suiteName: APP_GROUP_ID)
        if let data = defaults?.data(forKey: share_image){
            defaults?.setValue(nil, forKey: share_image)//清空
            if let image = UIImage(data: data){
                //处理图片
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS使用ShareExtension获取分享的图片

          本文链接:https://www.haomeiwen.com/subject/mejdnltx.html