iOS App文件导入 可以接收的文件 设置
文件设置参考
https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
office文件的iOS-UTI支持
文件格式UTI Types
doc - com.microsoft.word.doc
docx - org.openxmlformats.wordprocessingml.document
ppt - com.microsoft.powerpoint.ppt
pptx - org.openxmlformats.presentationml.presentation
xls - com.microsoft.excel.xls
xlsx - org.openxmlformats.spreadsheetml.sheet
"com.microsoft.powerpoint.ppt",
"com.microsoft.word.doc",
"com.microsoft.excel.xls",
"com.microsoft.powerpoint.pptx",
"com.microsoft.word.docx",
"com.microsoft.excel.xlsx",
"public.avi",
"public.3gpp",
"public.mpeg-4",
"com.compuserve.gif",
"public.jpeg",
"public.png",
"public.plain-text",
"public.text"
"com.adobe.pdf"
OC 代码实现
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString*fileName = [urllastPathComponent];
NSString *documentPath1= [TSPathHelper documentDirectoryPathWithName:kWenDangFilesInput];
NSString*oriFileName1 = [documentPath1stringByAppendingPathComponent:fileName];
///首次创建本地目录
if([fileManagerfileExistsAtPath:documentPath1]) {
[fileManagerremoveItemAtPath:documentPath1error:nil];//删除是清除内容
[fileManagercreateDirectoryAtPath:documentPath1 withIntermediateDirectories:YES attributes:nil error:nil];
}
[fileManagercopyItemAtURL:urltoURL:[NSURLfileURLWithPath:oriFileName1]error:nil];
[TSFanyiFileVC openFanyiFileVC:[UIView getCurrentWindos].rootViewController];
});
return YES;
}
代码实现
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
//如果已存在,q先删除,否则拷贝不了
// let fileManager = FileManager.default
guard let url = URLContexts.first?.url else {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {[weak self] in
DispatchQueue.global().async {
//获取授权
let fileUrlAuthozied = url.startAccessingSecurityScopedResource();
if fileUrlAuthozied == true {
let err = NSErrorPointer.init(nilLiteral: ())
let fileCoordinator = NSFileCoordinator.init()
fileCoordinator.coordinate(readingItemAt: url, options: NSFileCoordinator.ReadingOptions.init(), error: err) { newUrl in
//读取文件
// let fileName = newUrl.lastPathComponent;
let fileName = String.udidFileName() + ".pdf";
let fileData = try? NSData.init(contentsOf: newUrl, options: NSData.ReadingOptions.mappedIfSafe)
let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last ?? ""
var inboxPath = dir.appendingPathComponent("inputPath")
// 创建文件夹
if FileManager.default.fileExists(atPath: inboxPath) != true {
try? FileManager.default.createDirectory(atPath: inboxPath, withIntermediateDirectories: true, attributes: nil)
}
inboxPath = inboxPath.appendingPathComponent(fileName)
try? fileData?.write(to: URL.init(fileURLWithPath: inboxPath), options: NSData.WritingOptions.atomicWrite)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {[weak self] in
self?.homeVC?.detectionInboFile();
}
}
url.stopAccessingSecurityScopedResource()
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {[weak self] in
self?.homeVC?.detectionInboFile();
}
}
}
}
}