iOS Swift 3:将数据从共享扩展共享到Host App

时间:2017-08-26 16:35:09

标签: ios swift nsuserdefaults ios8-share-extension

您好我已经为我的应用实施了分享扩展,其中从图库中选择图像并发送到特定视图。现在的问题是我正在尝试保存图像阵列(从图库中选取的图像)

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)
                }
            }
        }
    }
}

并在AppDelegate方法中获取该数组

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
}

我正在发送网址let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY")并且能够 PHOTOKEY
 成功但阵列变为零,因此条件是错误的。 我该怎么办?,我搜索了很多但没有找到任何答案

当我尝试通过调试附加扩展进程时,我也没有获取日志。

P.S。 :使用Xcode 8.3.3 iOS 10.3,iPhone 6物理设备

更新:通过应用程序组套装名称进行尝试

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

仍然没有运气

1 个答案:

答案 0 :(得分:1)

根据@Stefan教会,我试过这样的

let imagesData: [Data] = []

将图片Data格式保存到数组而不是UIImage

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

并且有效

感谢Stefan教会