iOS Swift 3:将共享扩展中的数据共享到主机应用

编程入门 行业动态 更新时间:2024-10-24 18:17:46
本文介绍了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.absoluteStringponents(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: "grouppany.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.myconmanyName.AppName") userDefaults?.set(self?.imagesData, forKey: key) userDefaults?.synchronize()

它有效

感谢斯特凡教堂

更多推荐

iOS Swift 3:将共享扩展中的数据共享到主机应用

本文发布于:2023-11-30 03:32:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机   数据   iOS   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!