效果如圖:
1凝赛、設(shè)置 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躲履、設(shè)置 AppDelegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (self.window) {
if (url) {
NSString *fileName = url.lastPathComponent; // 從路徑中獲得完整的文件名(帶后綴)
// path 類(lèi)似這種格式: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編碼問(wèn)題
NSMutableString *string = [[NSMutableString alloc] initWithString:path];
if ([path hasPrefix:@"file://"]) { // 通過(guò)前綴來(lái)判斷是文件
// 去除前綴:/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)];
// 此時(shí)獲取到文件存儲(chǔ)在本地的路徑炼幔,就可以在自己需要使用的頁(yè)面使用了
NSDictionary *dict = @{@"fileName":fileName,
@"filePath":string};
[[NSNotificationCenter defaultCenter] postNotificationName:@"FileNotification" object:nil userInfo:dict];
return YES;
}
}
}
return YES;
}
// 當(dāng)文件名為中文時(shí)鸽心,解決url編碼問(wèn)題
- (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是文件名稱(chēng)、filePath是文件存儲(chǔ)在本地的路徑
// 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);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者