在项目Info.plist文件中添加如图所示字段(红色框框里是表示所有文件类型都可拷贝)
具体文件类型参考:
到这一步还没完成:
文件拷贝到咱们App了,咱们需要处理一下(要么存本地,要么直接预览)我的处理是存本地.上代码:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths lastObject];
if (url != nil) {
NSString *path = [url absoluteString];
path = [path stringByRemovingPercentEncoding];
NSMutableString *string = [[NSMutableString alloc] initWithString:path];
if ([path hasPrefix:@"file:///private"]) {
[string replaceOccurrencesOfString:@"file:///private" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
}
NSArray *tempArray = [string componentsSeparatedByString:@"/"];
NSString *fileName = tempArray.lastObject;
NSString *sourceName = options[@"UIApplicationOpenURLOptionsSourceApplicationKey"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",sourceName,fileName]];
if ([fileManager fileExistsAtPath:filePath]) {
NSLog(@"文件已存在");
[SVProgressHUD showErrorWithStatus:@"文件已存在"];
return YES;
}
[MRTools creatFilePathInManager:sourceName];
BOOL isSuccess = [fileManager copyItemAtPath:string toPath:filePath error:nil];
if (isSuccess == YES) {
NSLog(@"拷贝成功");
[SVProgressHUD showSuccessWithStatus:@"文件拷贝成功"];
} else {
NSLog(@"拷贝失败");
[SVProgressHUD showErrorWithStatus:@"文件拷贝失败"];
}
}
NSLog(@"application:openURL:options:");
return YES;
}
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>com.myapp.common-data</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>public.item</string>
</array>
</dict>
</array>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>com.myapp.common-data</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.excel.xlsx</string>
<string>com.microsoft.excel.xls</string>
<string>com.microsoft.word.doc</string>
<string>com.microsoft.word.docx</string>
<string>com.adobe.pdf</string>
<string>com.adobe.ppt</string>
<string>com.adobe.pptx</string>
<string>public.zip</string>
<string>public.rar</string>
<string>public.csv</string>
<string>public.pps</string>
<string>public.ppsx</string>
<string>public.txt</string>
<string>public.md</string>
<string>public.markdown</string>
<string>public.7z</string>
<string>public.epub</string>
<string>public.mobi</string>
<string>public.audio</string>
<string>public.movie</string>
<string>public.image</string>
<string>public.mp3</string>
</array>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.xlsx</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>xlsx</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.jpeg</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>jpeg</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.doc</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>doc</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.zip</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>zip</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.rar</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>rar</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.docx</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>docx</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.xls</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>xls</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.ppt</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>ppt</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.pptx</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>pptx</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.csv</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>csv</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.pps</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>pps</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.ppsx</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>ppsx</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.txt</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>txt</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.md</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>md</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.markdown</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>markdown</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.7z</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>7z</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.epub</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>epub</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.mobi</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>mobi</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.mp3</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>mp3</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.m4a</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>m4a</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.wav</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>wav</string>
</dict>
</dict>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>Owner</string>
</array>
<key>UTTypeIdentifier</key>
<string>saru2020.pdf</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>pdf</string>
</dict>
</dict>
</array>