You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iPad全屏模式分享文件URL出现[ShareSheet]报错求助

解决iPad全屏模式下分享文件URL的控制台报错问题

问题原因

iPad全屏模式下,UIActivityViewController的popoverPresentationController仅设置sourceView不足以让系统正确定位分享弹窗,导致系统抛出[ShareSheet] Failed to request default share mode for fileURL:...的控制台警告,但该警告不影响分享功能的实际执行,因此回调中不会返回错误。而iPad分屏模式下系统会自动适配弹窗定位逻辑,iPhone则是全屏模态展示无需popover配置,所以这两种场景下均正常。

修复方案

完善popoverPresentationController的配置,补充sourceRect指定弹窗的锚定区域,若分享操作由按钮触发,也可直接设置barButtonItem。

调整后的代码

let avc = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
// 针对iPad全屏模式补充popover配置
if let popover = avc.popoverPresentationController {
    popover.sourceView = self.view
    // 将弹窗锚定在视图中心,可根据需求改为按钮frame等具体区域
    popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
    // 若从导航栏按钮触发分享,可替换为:popover.barButtonItem = yourBarButtonItem
}

let h: UIActivityViewController.CompletionWithItemsHandler = { [weak self] (type: UIActivity.ActivityType?, completed: Bool, modifiedItems: [Any]?, error: Error?) -> Void in
    self?._handleShareResponse(completed: completed, error: error)
}
avc.completionWithItemsHandler = h
self.present(avc, animated: true, completion: nil)

补充说明

该警告属于系统级日志,不影响分享流程的正常完成,但通过完善popover配置可以消除此报错。

内容的提问来源于stack exchange,提问作者Annett Schwarze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:05:00