iOS中打开的文件如何用其他应用打开选择自己的app

效果如图:
用其他应用打开
选择某一应用
1、设置 Info.plist
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array>
            <string>96.png</string>
            <string>96@2x.png</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>com.myapp.common-data</string>
        <key>LSItemContentTypes</key>
        <array>
            <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>
            <string>public.text</string>
            <string>public.data</string>
        </array>
    </dict>
</array>
2、设置 AppDelegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (self.window) {
        if (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编码问题

            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:@"FileNotification" object:nil userInfo:dict];

                return YES;
            }
        }
    }
    return YES;
}

// 当文件名为中文时,解决url编码问题
- (NSString *)URLDecodedString:(NSString *)str {
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    DLog(@"decodedString = %@",decodedString);
    return decodedString;
}
3、接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileNotification:) name:@"FileNotification" object:nil];

- (void)fileNotification:(NSNotification *)notifcation {
    NSDictionary *info = notifcation.userInfo;
    // fileName是文件名称、filePath是文件存储在本地的路径
    // jfkdfj123a.pdf
    NSString *fileName = [info objectForKey:@"fileName"];
    // /private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
    NSString *filePath = [info objectForKey:@"filePath"];

    NSLog(@"fileName=%@---filePath=%@", fileName, filePath);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容