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

您好,我已经实现了我的应用程序的共享扩展,从图库中挑选图像并发送到特定的视图。 现在的问题是当我试图保存图像arrays(从图库中挑选的图像)

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 } 

我发送的URL let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY")并能够获得PHOTOKEY
成功,但数组获得零,因此条件是错误的。 我该怎么办?我search了很多,但没有find答案

另外我没有得到日志,当我试图通过debugging附加扩展过程。

PS:使用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() 

仍然没有运气

根据@斯特凡教堂,我试过这样的

 let imagesData: [Data] = [] 

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

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

它的工作

谢谢Stefan教会