像百度网盘等应用,里面的文件打开时,都能够通过以下应用再打开文件。以下红色框框内的我的jpg就是我做的一个样例。

由于样例没有提供Icon,所以显示的是默认icon。

 

以下就是这样例的主要步骤和代码。

 

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

 

1、在项目的**info.plist文件里加入:

 

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>icon@2x.png</string>
<string>icon.png</string>
</array>
<key>CFBundleTypeName</key>
<string>Molecules Structure File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.fzrong.jpg</string>
<string>org.gnu.gnu-zip-archive</string>
</array>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.plain-text</string>
<string>public.text</string>
</array>
<key>UTTypeDescription</key>
<string>Molecules Structure File</string>
<key>UTTypeIdentifier</key>
<string>com.fzrong.jpg</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>jpg</string>
<key>public.mime-type</key>
<string>image/jpg</string>
</dict>
</dict>
</array>

这就是告诉系统,你能打开 jpg这个文件类型。

 

 

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

在Appdelegate里加入代码例如以下:

 

- (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显示:

 

- (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; }

打开之后的效果是这种:

 

注意:这都是在真机上演示的。

 

这里样例咱们能够扩展,怎么打开网盘里的gif图片啊。还有其它自己自己定义的格式也能够。

 

项目完整代码已经上传到:http://download.csdn.net/detail/totogo2010/7460929
 

或者github: https://github.com/schelling/openFileType

 

參考:

https://developer.apple.com/library/ios/qa/qa1587/_index.html 

http://stackoverflow.com/questions/20869815/open-file-from-local-file-system-with-default-application-ios

iOS App让自己的应用在其它应用中打开列表中显示的更多相关文章

  1. 在app中打开appStore中其他app

    var str = "https://itunes.apple.com/cn/app/zhang-jiange-hao-tou-zi-ke/id402382976?mt=8"//这 ...

  2. Vim 写 iOS App

    Vim 写 iOS App 我们都知道 Vim 和 Emacs 都是文本编辑器中的上古神器,你也许用 ctags,cscopes 配合 Vim 完成过大型 C 或者 C++ 的开发,你也许配合过其他插 ...

  3. iOS app内存分析套路

    iOS app内存分析套路 Xcode下查看app内存使用情况有2中方法: Navigator导航栏中的Debug navigator中的Memory Instruments 一.Debug navi ...

  4. iOS App引导页功能实现

    一.写作原因 以前都没有想着来写点东西,今天遇到件事情让我决定每次还是要做记录.因为以前自己可以轻松的完成pod spec的配置,但是今天在做的时候还是忘了遇到了很多坑.pod spec配置遇到的坑不 ...

  5. iOS APP 如何做才安全

    本来 写了一篇<iOS 如何做才安全--逆向工程 - Reveal.IDA.Hopper.https抓包 等>,发现文章有点杂,并且“iOS 如何做才安全”这部分写的越来越多,觉得 分出来 ...

  6. Xcode7.1环境下上架iOS App到AppStore 流程② (Part 二)

    前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及概要文件安装的过程. 一.iOS App IDs 的创建 1)进入如图1所示界面点击右上角箭头所指的加号 进入iO ...

  7. iOS App上架流程(2016详细版)

    iOS App上架流程(2016详细版) 原文地址:http://www.jianshu.com/p/b1b77d804254 感谢大神整理的这么详细 一.前言: 作为一名iOSer,把开发出来的Ap ...

  8. 用Model-View-ViewModel构建iOS App(转)

    转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...

  9. 用Model-View-ViewModel构建iOS App

    如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller,即MVC.MVC是构建iOS App的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重温 ...

随机推荐

  1. bootstrap入门基础

    1.字体 text-left text-center text-right text-lowercase 小写 text-uppercase 大写 text-capitalize 首字母大写 2.表格 ...

  2. 理解PEP333-WSGI

    声明:这篇文章只是为了整体理解WSGI,会忽略很多细节,要详细了解请参看文后的参考资料 WSGI概述 WSGI全称是Python Web Server Gateway Interface(Python ...

  3. Office Web Apps Server

    Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...

  4. Zookeeper中Session Timeout的那些事

    前言: RDS系统致力于MySQL数据的高可用,高可靠,高性能以及在线扩展功能,实现这些特性的主要逻辑功能都运行在管理服务器上,一旦管理服务器宕机,数据库的在线扩展功能/备份功能/故障恢复功能等都无从 ...

  5. 有关 PHP 的 10 道问题

    1.简述面向对象的三大特性 答:封装 --  继承  --  多态 封装的目的:为了让类更安全 继承的概念:子类可以继承父类的一切 多态的概念:当父类引用指向子类实例,由于子类里面对父类的方法进行了重 ...

  6. javascript与java正则表达式写法的区别

    Js验证写法:(转义符\) var str = "待验证文本"; var regular = new RegExp(/这里是正则表达式/); if (regular.test(st ...

  7. 体验 ASP.NET Core 集成测试三剑客:xUnit.net、TestServer、EF Core InMemory

    这是昨天解决的一个问题,针对一个 web api 的客户端代理类写集成测试,既要测试 web api,又要测试 web api 客户端. 测试 web api,就要在运行测试时自动启动 web api ...

  8. [No0000188][VCB-Studio 科普教程 2.5] 基于 PotPlayer 和 madVR 的播放器教程(已更新 XySubFilter)

    Potplayer 是高清影视常用的播放器,界面简洁,功能齐全,比 MPC-HC 和 MPC-BE 更人性化:但其默认方案十分糟糕,预设过多错误,无法正确播放 10-bit 视频,一直饱受诟病.VCB ...

  9. [No0000EA]C# 可空类型(Nullable)

    C# 可空类型(Nullable) C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable& ...

  10. Dotnetbar中如何让LabelX自动换行

    把LableX标签控件的WordWrap属性设置为true. https://zhidao.baidu.com/question/207858423.html 另外lable控件可以设置AutoSiz ...