gpt4 book ai didi

ios - UIActivityViewController completionWithItemsHandler 函数未在 MacOS 应用程序中调用

转载 作者:行者123 更新时间:2023-12-05 03:37:19 25 4
gpt4 key购买 nike

背景

下面代码中的函数 shareImage() 启动 UIActivityViewController 并共享图像,例如发送到消息或保存到照片。

图片分享后,UIActivityViewController结束,调用completionWithItemsHandler函数activityComplete()

代码在 iOS/iPadOS/MacOS 上运行良好,如预期的那样并共享图像,除了在 MacOS 上,completionWithItemsHandler 函数 activityComplete() 从不被调用,使用标志 print("Share complete.") 确认。

Xcode 改为记录以下输出:场景销毁请求失败,出现错误:(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.")
}
}

最佳答案

尝试在呈现后保留您的事件 View Controller 。像这样的东西:

    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
}

关于ios - UIActivityViewController completionWithItemsHandler 函数未在 MacOS 应用程序中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69367599/

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