iOS Swift 3:将共享扩展中的数据共享到主机应用 [英] iOS Swift 3: Share Data from share extension to Host App

查看:156
本文介绍了iOS Swift 3:将共享扩展中的数据共享到主机应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经为我的应用实现了共享扩展程序,其中可以从图库中选择图像并发送到特定视图.现在的问题是,当我尝试保存图像数组(从画廊中挑选的图像)时

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

并在AppDelegate方法中获取该数组

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
}

我正在发送URL let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY"),并且能够获取 PHOTOKEY
成功但数组变为nil,因此条件为false. 我该怎么办?,我在Google上搜索了很多,但是没有找到答案

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. :使用Xcode 8.3.3 iOS 10.3,iPhone 6物理设备

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

更新:也通过App Groups Suit Names尝试过

Update: Tried Via App Groups Suit Names also

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

仍然没有运气

推荐答案

根据@Stefan Church,我尝试过这样

According to @Stefan Church, i tried like this

let imagesData: [Data] = []

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

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

它有效

感谢斯特凡教堂

这篇关于iOS Swift 3:将共享扩展中的数据共享到主机应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆