use*_*509 5 macos ios uiactivityviewcontroller swift mac-catalyst
背景
shareImage()
下面代码中的函数启动UIActivityViewController
并共享图像,例如发送到消息或保存到照片。
图像共享后,UIActivityViewController
完成并调用该completionWithItemsHandler
函数activityComplete()
。
该代码在 iOS / iPadOS / MacOS 上运行良好,符合预期并共享图像,但在 MacOS 上,该completionWithItemsHandler
函数activityComplete()
永远不会被调用,并使用标志确认print("Share complete.")
。
Xcode 会记录以下输出:
Scene destruction request failed with error: (null)
问题
导致该completionWithItemsHandler
函数activityComplete()
无法在 MacOS 上调用的问题的原因是什么?如何在代码中纠正此问题?
代码
注意:根据答案添加了两行更新。
import UIKit
class ViewController: UIViewController {
var shareButton = UIButton(type: UIButton.ButtonType.custom)
var activityImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Create share button
shareButton.setTitle("Share", for:UIControl.State())
shareButton.setTitleColor(.white, for: UIControl.State())
shareButton.backgroundColor = .purple
shareButton.frame = CGRect(x: 50, y: 100, width: 200, height: 200)
shareButton.addTarget(self, action: #selector(ViewController.shareImage), for:.touchUpInside)
view.addSubview(shareButton)
}
private var activity: UIActivityViewController? // <-- Added
@objc func shareImage() {
// Take view screenshot
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
self.view.drawHierarchy(in: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false)
activityImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Share view screenshot
let activity = UIActivityViewController(activityItems: [activityImage as Any], applicationActivities: nil)
// Present iPhone share options
if UIDevice.current.userInterfaceIdiom == .phone {
self.present(activity, animated: true, completion: nil)
}
// Present iPad share options
if UIDevice.current.userInterfaceIdiom == .pad {
self.present(activity, animated: true, completion: nil)
if let popOver = activity.popoverPresentationController {
popOver.sourceView = self.shareButton
}
}
// Complete share
activity.completionWithItemsHandler = {activity, success, items, error in
self.activityComplete()
}
self.activity = activity // <-- Added
}
func activityComplete() {
print("Share complete.")
}
}
Run Code Online (Sandbox Code Playgroud)
尝试在呈现后保留您的活动视图控制器。就像是:
private var activityViewController: UIActivityViewController?
@objc func presentActivityViewController(activityItems: [Any]) {
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
activityViewController.completionWithItemsHandler = { activity, success, items, error in
// some logic
}
self.activityViewController = activityViewController // <- retaining activityViewController
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
635 次 |
最近记录: |