用自己的App打開其他App不支持的文件如下效果圖:
Paste_Image.png
上圖用到的界面用到的類是 UIDocumentInteractionController
iOS添加支持打開的文件類型步驟兩種方式為1馏谨、2如下圖
Paste_Image.png
Paste_Image.png
在自己App中獲取文件
如下代碼:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"url = %@",url);
if (url != nil) {
NSString *path = [url absoluteString];
path = [self URLDecodedString:path];
NSMutableString *string = [[NSMutableString alloc] initWithString:path];
if ([path hasPrefix:@"file://"]) {
[string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
}
//獲取在存儲在本地的文件路徑就可以在自己需要顯示的頁面加載文件顯示了
}
return YES;
}
//當文件名為中文是恕齐,解決url編碼問題
-(NSString *)URLDecodedString:(NSString *)str
{
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
NSLog(@"decodedString = %@",decodedString);
return decodedString;
}
官方文檔地地址及部分截圖
Paste_Image.png