iOS讓自己的App在其他應用中開啟列表中顯示
像百度網盤等應用,裡面的檔案開啟時,都可以通過以下應用再開啟檔案。下面紅色框框內的我的jpg就是我做的一個例子。因為例子沒有提供Icon,所以顯示的是預設icon。
下面就是這例子的主要步驟和代碼。<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Cjxicj4KPC9wPgo8cD4KwP3X08rH0ru49rTyv6pqcGfNvMass8zQ8qGjPC9wPgo8cD4KPGJyPgo8L3A+CjxwPgoxoaLU2s/uxL+1xCoqaW5mby5wbGlzdM7EvP7W0MztvNOjujwvcD4KPHA+CjwvcD4KCgoKPHN0cm9uZz5baHRtbF08L3N0cm9uZz4gdmlldwogcGxhaW5jb3B5PGltZyBzcmM9"http://www.bkjia.com/uploads/allimg/141104/0401514a5-1.png" width="12" height="12" alt="在CODE上查看代碼片">
- CFBundleDocumentTypes
-
-
- CFBundleTypeIconFiles
-
- icon@2x.png
- icon.png
-
- CFBundleTypeName
- Molecules Structure File
- CFBundleTypeRole
- Viewer
- LSHandlerRank
- Owner
- LSItemContentTypes
-
- com.fzrong.jpg
- org.gnu.gnu-zip-archive
-
-
-
- UTExportedTypeDeclarations
-
-
- UTTypeConformsTo
-
- public.plain-text
- public.text
-
- UTTypeDescription
- Molecules Structure File
- UTTypeIdentifier
- com.fzrong.jpg
- UTTypeTagSpecification
-
- public.filename-extension
- jpg
- public.mime-type
- image/jpg
-
-
- 這就是告訴系統,你能開啟 jpg這個檔案類型。
2、開啟到自己的app時,要截取到過來資源的檔案路徑:
在Appdelegate裡添加代碼如下:
[objc] view plaincopy
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- if (url != nil) {
- NSString *path = [url absoluteString];
- NSMutableString *string = [[NSMutableString alloc] initWithString:path];
- if ([path hasPrefix:@"file://"]) {
- [string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
- }
- [self.viewController openPng:string];
-
- }
-
- return YES;
- }
要去掉file://檔案路徑的頭,要不然找不到資源。
3、在自己的ViewController裡開啟jgp顯示:
[objc] view plaincopy
- - (void)openPng:(NSString*)string
- {
- UIImage *image = [[UIImage alloc] initWithContentsOfFile:string];
- float width = image.size.width;
- float height = image.size.height;
- if (width > 320) {
- height = (height/width) * 300;
- width = 300;
- }
-
- CGRect frame = CGRectMake(0, 20, width, height);
- imageView.frame = frame;
-
- imageView.image = image;
-
- }
開啟之後的效果是這樣的:html
http://stackoverflow.com/questions/20869815/open-file-from-local-file-system-with-default-application-ios