gpt4 book ai didi

iOS swift 3 : Share Data from share extension to Host App

转载 作者:行者123 更新时间:2023-11-28 06:13:05 25 4
gpt4 key购买 nike

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

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
成功但数组为零,因此条件为假。我该怎么办?,我用谷歌搜索了很多但没有找到任何答案

此外,当我尝试通过调试附加扩展进程时,我没有收到日志。

附言: 使用Xcode 8.3.3 iOS 10.3, iPhone 6 实体机

更新:也尝试通过 App Groups Suit Names

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

仍然没有运气

最佳答案

根据@Stefan Church 的说法,我试过这样

let imagesData: [Data] = []

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

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

它有效

谢谢斯特凡·丘奇

关于iOS swift 3 : Share Data from share extension to Host App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45897547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com