让自己的App显示在系统的分享列表里:需要修改 *.plist 文件
Key为:CFBundleDocumentTypes
Value是:数组,可以包含n个字典,一般一个字典表示支持一种类型的文件
字典:
Key | Value |
---|---|
CFBundleTypeName | 文件类型名称(自己起个名) |
LSHandlerRank | 包含Owner,Default,Alternate,None四个可选值 |
LSItemContentTypes | 数组类型,包含支持的文件类型:官方标识符文档(也可以自定义) |
这里给一个我需要支持.bin文件的例子:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>Binary</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.data</string>
<string>public.executable</string>
<string>com.apple.mach-o-binary</string>
<string>com.apple.pef-binary</string>
</array>
</dict>
</array>
然后就可以.bin文件的分享列表里看到自己的app了,如图:
当从其他app分享文件过来时,会调用:
// MARK: - 其他app分享过来时回调
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
print("openURLContexts:\(URLContexts)")
}
保存的位置:会在Document下新建一个Inbox文件夹,分享过来的文件都会存在这个文件夹下:
// 获取 Document/Inbox 里从其他app分享过来的文件
let manager = FileManager.default
let urlForDocument = manager.urls(for: .documentDirectory, in: .userDomainMask)
var documentUrl = urlForDocument[0] as URL
documentUrl.appendPathComponent("Inbox", isDirectory: true)
do {
let contentsOfPath = try manager.contentsOfDirectory(at: documentUrl,
includingPropertiesForKeys: nil,
options: .skipsHiddenFiles)
self.url = contentsOfPath.first // 保存,为了展示分享
print("contentsOfPath:\n\(contentsOfPath)")
} catch {
print("error:\(error)")
}
二、分享文件到其他App
// MARK: - 点击分享文件
@objc func clickShare() {
if let url = self.url {
documentController = UIDocumentInteractionController(url: url)
documentController?.presentOptionsMenu(from: self.view.bounds, in: self.view, animated: true)
}
}
以上~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有