首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Swift 3:从共享扩展到主机应用程序的共享数据

iOS Swift 3:从共享扩展到主机应用程序的共享数据
EN

Stack Overflow用户
提问于 2017-08-26 16:35:08
回答 1查看 1.7K关注 0票数 1

你好,我已经为我的应用程序实现了共享扩展,在该应用程序中,从图库中选择图像并发送到特定视图。现在的问题是当我试图保存图像数组(从图片库中提取的图像)时。

代码语言:javascript
复制
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方法中获取该数组

代码语言:javascript
复制
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
}

我正在发送url let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY"),并能得到光通信

成功,但数组获得零,因此条件是假的。我应该怎么做?,我搜索了很多,但没有找到任何答案。

而且,当我试图通过调试附加扩展进程时,我不会获得日志。

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

更新:尝试通过应用程序组设置名称

代码语言:javascript
复制
let userDefaults = UserDefaults(suiteName: "group.com.company.appName")
userDefaults?.set(self.selectedImages, forKey: "PHOTOKEY")
userDefaults?.synchronize()

还是没有运气

EN

回答 1

Stack Overflow用户

发布于 2017-08-27 04:21:06

据斯特凡·丘奇说,我试过这样做

代码语言:javascript
复制
let imagesData: [Data] = []

将图像Data格式保存为数组而不是UIImage格式

代码语言:javascript
复制
let userDefaults = UserDefaults(suiteName: "group.com.myconmanyName.AppName")
userDefaults?.set(self?.imagesData, forKey: key)
userDefaults?.synchronize()

而且起作用了

谢谢斯特凡教堂

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45897547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档