ios App 让自己的应用在其他应用打开列表显示

像百度网盘等应用,里面的文件打开时,都可以通过以下应用再打开文件。下面红色框框内的我的jpg就是我做的一个例子。因为例子没有提供Icon,所以显示的是默认icon。


下面就是这例子的主要步骤和代码。


例子是一个打开jpg图片程序。


1、在项目的**info.plist文件中添加:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <key>CFBundleDocumentTypes</key>  
  2.     <array>  
  3.         <dict>  
  4.             <key>CFBundleTypeIconFiles</key>  
  5.             <array>  
  6.                 <string>icon@2x.png</string>  
  7.                 <string>icon.png</string>  
  8.             </array>  
  9.             <key>CFBundleTypeName</key>  
  10.             <string>Molecules Structure File</string>  
  11.             <key>CFBundleTypeRole</key>  
  12.             <string>Viewer</string>  
  13.             <key>LSHandlerRank</key>  
  14.             <string>Owner</string>  
  15.             <key>LSItemContentTypes</key>  
  16.             <array>  
  17.                 <string>com.fzrong.jpg</string>  
  18.                 <string>org.gnu.gnu-zip-archive</string>  
  19.             </array>  
  20.         </dict>  
  21.     </array>  
  22.     <key>UTExportedTypeDeclarations</key>  
  23.     <array>  
  24.         <dict>  
  25.             <key>UTTypeConformsTo</key>  
  26.             <array>  
  27.                 <string>public.plain-text</string>  
  28.                 <string>public.text</string>  
  29.             </array>  
  30.             <key>UTTypeDescription</key>  
  31.             <string>Molecules Structure File</string>  
  32.             <key>UTTypeIdentifier</key>  
  33.             <string>com.fzrong.jpg</string>  
  34.             <key>UTTypeTagSpecification</key>  
  35.             <dict>  
  36.                 <key>public.filename-extension</key>  
  37.                 <string>jpg</string>  
  38.                 <key>public.mime-type</key>  
  39.                 <string>image/jpg</string>  
  40.             </dict>  
  41.         </dict>  
  42.     </array>  
这就是告诉系统,你能打开 jpg这个文件类型。


2、打开到自己的app时,要截取到过来资源的文件路径:

在Appdelegate里添加代码如下:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation  
  2. {  
  3.     if (url != nil) {  
  4.         NSString *path = [url absoluteString];  
  5.         NSMutableString *string = [[NSMutableString alloc] initWithString:path];  
  6.         if ([path hasPrefix:@"file://"]) {  
  7.             [string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch  range:NSMakeRange(0, path.length)];  
  8.         }  
  9.         [self.viewController openPng:string];  
  10.           
  11.     }  
  12.       
  13.     return YES;  
  14. }  

要去掉file://文件路径的头,要不然找不到资源。

3、在自己的ViewController里打开jgp显示:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)openPng:(NSString*)string  
  2. {  
  3.     UIImage *image = [[UIImage alloc] initWithContentsOfFile:string];  
  4.     float width = image.size.width;  
  5.     float height = image.size.height;  
  6.     if (width > 320) {  
  7.         height = (height/width) * 300;  
  8.         width = 300;  
  9.     }  
  10.       
  11.     CGRect frame = CGRectMake(020, width, height);  
  12.     imageView.frame = frame;  
  13.       
  14.     imageView.image = image;  
  15.       
  16. }  

打开之后的效果是这样的:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值