一、从其他App获取文件:
让自己的App显示在系统的分享列表里:需要修改 *.plist 文件
Key为:CFBundleDocumentTypes
Value是:数组,可以包含n个字典,一般一个字典表示支持一种类型的文件
CFBundleTypeName // 文件类型名称(自己起个名)
LSHandlerRank // 包含Owner,Default,Alternate,None四个可选值
LSItemContentTypes // 数组类型,包含支持的文件类型
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>TextExtraction</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.word.doc</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.text</string>
</array>
</dict>
</array>
Supports opening documents in place
YES
image.png
二、SceneDelegate
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard TEShare.isLoginUserAndPresent == true else { return }
guard let context = URLContexts.first else { return }
tsPrint(context.url)
let urlString = context.url.absoluteString
if urlString.hasPrefix("file://") {
if urlString.hasSuffix("docx") || urlString.hasSuffix("doc") || urlString.hasSuffix("txt") || urlString.hasSuffix("mp3") || urlString.hasSuffix("m4a") || urlString.hasSuffix("pdf") {
let fileName = urlString.lastPathComponent
let path = urlString.urlDecoded
let aaPath = path.removingPrefix("file://")
let dict = ["fileName" : fileName, "filePath" : aaPath]
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute:{
///延迟执行的代码
NotificationCenter.default.post(name: .ReceiveFile, object: nil, userInfo: dict)
})
} else {
HUD.showError("该文件格式无法识别")
}
}
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
let url = connectionOptions.urlContexts.first?.url
tsPrint(url)
guard let urlString = url?.absoluteString else { return }
if urlString.hasPrefix("file://") {
guard TEShare.isLoginUserAndPresent == true else { return }
if urlString.hasSuffix("docx") || urlString.hasSuffix("doc") || urlString.hasSuffix("txt") || urlString.hasSuffix("mp3") || urlString.hasSuffix("m4a") || urlString.hasSuffix("pdf") {
let fileName = urlString.lastPathComponent
let path = urlString.urlDecoded
let aaPath = path.removingPrefix("file://")
let dict = ["fileName" : fileName, "filePath" : aaPath]
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute:{
///延迟执行的代码
NotificationCenter.default.post(name: .ReceiveFile, object: nil, userInfo: dict)
})
} else {
HUD.showError("该文件格式无法识别")
}
}
}
网友评论