IOS Swift 3: sharing data from a host sharing application

Hello, I have implemented the Share extension for my application, in which it selects images from the gallery and sends them to a specific view. Now the problem is that I'm trying to save an array of images (images selected from the 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) } } } } } 

and fetching this array in the 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 } 

I am sending url let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY") and can get PHOTOKEY
successful, but the array gets zero, and therefore the condition is false. What should I do ?, I searched a lot, but could not find an answer

Also, I do not receive logs when I try to connect the extension process via debug.

PS: Using Xcode 8.3.3 iOS 10.3, iPhone 6 physical device

Update : Curved with application name names also

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

Still out of luck

+1
source share
1 answer

According to @Stefan church, I tried like this

 let imagesData: [Data] = [] 

save Data image to array instead of UIImage

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

and he works

Thanks, Stephen's Church

+1
source

Source: https://habr.com/ru/post/1233837/


All Articles