iOS Swift 3: Share Data from share extension to Host App

断了今生、忘了曾经 提交于 2019-12-24 10:56:06

问题


Hello i have implemented Share extension for my app in which picks images from gallery and send to a particular view. Now the problem is when i'm trying to save array of images(images picked from gallery)

func manageImages() {

    let content = extensionContext!.inputItems[0] as! NSExtensionItem
    let contentType = kUTTypeImage as String

    for (index, attachment) in (content.attachments as! [NSItemProvider]).enumerated() {
        if attachment.hasItemConformingToTypeIdentifier(contentType) {

            attachment.loadItem(forTypeIdentifier: contentType, options: nil) { data, error in

                if error == nil, let url = data as? URL {
                    do {
                        let imageData = try Data(contentsOf: url)
                        let image = UIImage(data: imageData)
                        self.selectedImages.append(image!)

                        if index == (content.attachments?.count)! - 1 {
                            self.imgCollectionView.reloadData()
                            UserDefaults.standard.set(self.selectedImages, forKey: "PHOTOKEY")
                            UserDefaults.standard.synchronize()
                        }
                    }
                    catch let exp {
                        print("GETTING ERROR \(exp.localizedDescription)")
                    }

                } else {
                    print("GETTING ERROR")
                    let alert = UIAlertController(title: "Error", message: "Error loading image", preferredStyle: .alert)

                    let action = UIAlertAction(title: "Error", style: .cancel) { _ in
                        self.dismiss(animated: true, completion: nil)
                    }

                    alert.addAction(action)
                    self.present(alert, animated: true, completion: nil)
                }
            }
        }
    }
}

and fetching that array in AppDelegate method

func application(_ app: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    if let key = url.absoluteString.components(separatedBy: "=").last, let _ = UserDefaults.standard.array(forKey: key) {

        let myVC = UIStoryboard.getViewController(storyboardName: "Main",
                                                         storyboardId: "MyViewController")

        let navVC = UIStoryboard.getViewController(storyboardName: "Main",
                                                   storyboardId: "MyNavigationVC") as! UINavigationController
        navVC.viewControllers.append(myVC)
        self.window?.rootViewController = navVC
        self.window?.makeKeyAndVisible()

        return true
    }

    return false
}

I'm sending url let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY") and able to get PHOTOKEY
successfully but array getting nil and hence condition is false. What should i do ?, i googled a lot but didn't find any answer

Also i'm not getting logs when i'm trying to attach extension process via debug.

P.S. : Using Xcode 8.3.3 iOS 10.3, iPhone 6 physical device

Update: Tried Via App Groups Suit Names also

let userDefaults = UserDefaults(suiteName: "group.com.company.appName")
userDefaults?.set(self.selectedImages, forKey: "PHOTOKEY")
userDefaults?.synchronize()

Still No luck


回答1:


According to @Stefan Church, i tried like this

let imagesData: [Data] = []

saving image Data format into array instead of UIImage

let userDefaults = UserDefaults(suiteName: "group.com.myconmanyName.AppName")
userDefaults?.set(self?.imagesData, forKey: key)
userDefaults?.synchronize()

and it works

Thanks Stefan Church



来源:https://stackoverflow.com/questions/45897547/ios-swift-3-share-data-from-share-extension-to-host-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!