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

问题描述:

您好,我已经为我的应用程序实施了共享扩展,其中从图库中选取图像并发送到特定视图。现在的问题是,当我试图保存图像的阵列iOS Swift 3:将数据从共享扩展共享到主机应用程序

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
成功,但数组得到零,因此条件是错误的。 我该怎么办?,我GOOGLE了很多,但没有找到任何答案

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

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

仍然没有运气

+0

我不认为你将能够存储UIImages数组中userdefaults。您必须将它们转换为类似UIImagePNGRepresentation的数组NSData,并在启动应用程序时再次返回。 –

+0

Userdefaults也可能不是共享图像的最佳方式。我没有看到它,但像你的应用程序组的共享目录可能会更好地工作:https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati –

+0

我也尝试保存在文档目录中,但也它也给nil –

据@Stefan教堂,我想这样

let imagesData: [Data] = [] 

保存图像Data格式转换成阵列的代替UIImage

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

和它的作品

感谢斯特凡教堂

+2

对不起,今天我有点忙,发布了一个正确的答复。很高兴你能解决问题。 –