比如微信有个文件,想要分享到自己的APP,如下图
image.png
步骤:
-
plist
文件新增key
:CFBundleDocumentTypes
image.png
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>LSItemContentTypes</key>
<array>
<string>public.text</string>
<string>public.data</string>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
</array>
</dict>
</array>
- 在
app delegate
代理方法中
如果有SceneDelegate
,在SceneDelegate
处理
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {
//<UIOpenURLContext: 0x281f7c100; URL: file:///private/var/mobile/Containers/Data/Application/132CBDFD-F268-4CBB-A4AD-686A98032E58/Documents/Inbox/newrecord-3.plist
NSLog(@"1 %@", URLContexts.allObjects.firstObject.URL);
}
如果没有SceneDelegate
,在AppDelegate
处理
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
//file:///private/var/mobile/Containers/Data/Application/00FF7BC3-24B3-4DCB-AF4F-587B619DE720/Documents/Inbox/newrecord-4.plist
NSLog(@"url:%@", url);
}
这里贴下用AppDelegate
处理的方式
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
//file:///private/var/mobile/Containers/Data/Application/00FF7BC3-24B3-4DCB-AF4F-587B619DE720/Documents/Inbox/newrecord-4.plist
NSLog(@"url:%@", url);
NSString *fileName = url.lastPathComponent; // 从路径中获得完整的文件名(带后缀)
// path 类似这种格式:file:///private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
NSString *path = url.absoluteString; // 完整的url字符串
path = [self URLDecodedString:path]; // 解决url编码问题
//复制文件
NSString *docFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:fileName];
NSLog(@"docFilePath:%@", docFilePath);
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:docFilePath]) {
NSLog(@"文件存在,先移除");
[fm removeItemAtPath:docFilePath error:NULL];
}
// 去除前缀 file:///private
NSString *tmpPath = [path stringByReplacingOccurrencesOfString:@"file:///private" withString:@""];
[fm copyItemAtPath:tmpPath toPath:docFilePath error:NULL];
NSMutableString *string = [[NSMutableString alloc] initWithString:path];
if ([path hasPrefix:@"file://"]) { // 通过前缀来判断是文件
// 去除前缀:/private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
[string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
// 此时获取到文件存储在本地的路径,就可以在自己需要使用的页面使用了
NSDictionary *dict = @{@"fileName":fileName,
@"filePath":string};
[[NSNotificationCenter defaultCenter] postNotificationName:@"" object:nil userInfo:dict];
return YES;
}
return YES;
}
// 当文件名为中文时,解决url编码问题
- (NSString *)URLDecodedString:(NSString *)str {
return [str stringByRemovingPercentEncoding];
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}