macOS 13下用NSSharingServicePicker向iOS设备共享文件夹异常求助
macOS 13下NSSharingServicePicker AirDrop共享文件夹到iOS设备为空的解决方案
问题背景
在macOS 11/12上通过NSSharingServicePicker共享文件夹可正常完成AirDrop传输,但升级到macOS 13后,向iOS 15/16设备AirDrop文件夹时,设备端接收的文件夹为空;单个文件(如PDF)共享无异常,向邮件、Notes等应用共享文件夹也正常。
解决方案
方案1:将文件夹打包为ZIP压缩包后共享
macOS 13的AirDrop对直接传递文件夹URL的处理逻辑可能存在变更,将文件夹打包为ZIP后再共享可规避此问题。示例代码如下:
// 定义压缩后的ZIP文件路径 let zipURL = exportURL.deletingLastPathComponent().appendingPathComponent("\(exportURL.lastPathComponent).zip") // 删除已存在的旧ZIP文件 try? FileManager.default.removeItem(at: zipURL) // 打包文件夹为ZIP do { try FileManager.default.createZipFile(at: zipURL, withContentsOf: exportURL) // 使用ZIP文件URL创建共享选择器 let picker = NSSharingServicePicker(items: [zipURL]) picker.delegate = self picker.show(relativeTo: myShareButton.bounds, of: myShareButton, preferredEdge: .maxY) } catch { print("打包文件夹失败:\(error.localizedDescription)") }
方案2:使用NSFileWrapper包装文件夹
NSSharingServicePicker支持传入NSFileWrapper类型的项目,传递文件夹对应的NSFileWrapper可让AirDrop正确识别并包含文件夹内的所有内容。示例代码:
do { // 创建文件夹对应的NSFileWrapper let fileWrapper = try NSFileWrapper(url: exportURL, options: .immediate) let picker = NSSharingServicePicker(items: [fileWrapper]) picker.delegate = self picker.show(relativeTo: myShareButton.bounds, of: myShareButton, preferredEdge: .maxY) } catch { print("创建FileWrapper失败:\(error.localizedDescription)") }
方案3:验证文件权限与沙盒设置
- 确保
exportURL对应的文件夹及其内部所有文件都具备可读权限,若应用处于沙盒环境,需确认已通过NSOpenPanel或文件书签获取了该文件夹的访问权限。 - 检查沙盒配置中是否开启了
Outgoing Connections (Client)权限(AirDrop需要此权限完成传输)。
内容的提问来源于stack exchange,提问作者berbie
相关产品推荐
相关产品推荐

