1

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

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
}

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. : Using Xcode 8.3.3 iOS 10.3, iPhone 6 physical device

Update: Tried Via App Groups Suit Names also

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

Still No luck

Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
  • I don't think you'll be able to store an array of UIImages in userdefaults. You'll have to covert them to an array NSData with something like UIImagePNGRepresentation and back again when you launch your app. – Stefan Church Aug 26 '17 at 22:56
  • Userdefaults is also probably not the best way of sharing images. I haven't looked into it, but something like a shared directory with your app group might work better: https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati – Stefan Church Aug 26 '17 at 23:21
  • I tried save in document directory also but it also giving nil – Abhishek Thapliyal Aug 27 '17 at 03:49
  • Can you put an answer that i can try it will be great help @StefanChurch – Abhishek Thapliyal Aug 27 '17 at 03:50

1 Answers1

1

According to @Stefan Church, i tried like this

let imagesData: [Data] = []

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

and it works

Thanks Stefan Church

Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69