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);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容