iOS App让自己的应用在其他应用中打开列表中显示、iOS把自己的应用添加到”活动“、将PDF文件Open In MyApp
2017-03-23 09:44
861 查看
像百度网盘等应用,里面的文件打开时,都可以通过以下应用再打开文件。下面红色框框内的我的jpg就是我做的一个例子。因为例子没有提供Icon,所以显示的是默认icon。
下面就是这例子的主要步骤和代码。
例子是一个打开jpg图片程序。
1、在项目的**info.plist文件中添加:
[html] view
plain copy
<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里添加代码如下:
[objc] view
plain copy
- (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
plain copy
- (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
容芳志 (http://blog.csdn.net/totogo2010)
本文遵循“署名-非商业用途-保持一致”创作公用协议
写了一个很好的PDF阅读软件,那么怎么让用户根据提示打开我们的应用浏览阅读,提高程序的使用率呢?本文就是针对这个问题而来,方法:修改-Info.plist文件。
1.在plist文件中添加一个URLTypes字段,该字段指定程序的对外接口:
这部分可以参考我之前写的博客:iOS调用外部程序和系统程序。
2.另外添加一个Documents Type字段,该字段指定与程序关联的文件类型,详情参考System-Declared
Uniform Type Identifiers。
其中CFBundleTypeExtensions指定文件类型,例如pdf,doc,这个是不能随便填的。
CFBundleTypeIconFiles指定用UIActionSheet向用户提示打开应用时显示的图标。
DocumentTypeName可以自定,对应文件类型名。
Document Content Type UTIs指定官方指定的文件类型,UTIs即Uniform Type Identifiers。
如果要关联多个文件类型可以在Document Types中设置多个Item,这里我设置的关联类型包括pdf,doc,ppt。
接下来上代码。
AppDelegate类的代码:
[objc] view
plain copy
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// return YES;
//}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url && [url isFileURL]) {
DocumentViewController *documentViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
[documentViewController handleDocumentOpenURL:url];
return YES;
}
return NO;
}
其中handleOpenURL确定是否处理外部程序的调用请求,后者则是打开文件的URL。
再看看DocumentViewController中的处理方法:
[objc] view
plain copy
- (void)handleDocumentOpenURL:(NSURL *)url {
[self displayAlert:[url absoluteString]];
}
-(void) displayAlert:(NSString *) str {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:str
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
Run:
打开邮箱客户端,随便打开一封邮件中的附件,如pdf,doc格式,选择用其它应用打开(建议真机调试):
上面图标P对应的程序就是我们的Demo。
点击该Demo,我们的程序将被启动,并调用openURL方法做出处理,结果如下:
AlertView显示的内容就是下载的文件的fileURL。
如果在Mac上运行,打开App的文件夹可以看到对应下载的文件:
该文件在App的Documents/Inbox/目录下。
好吧,将接收到的PDF,DOC,PPT等格式的文件与程序关联的工作已经完成,源文件就在我们下载的目录中,接下来想怎么折腾这个文件都可以了。
下面就是这例子的主要步骤和代码。
例子是一个打开jpg图片程序。
1、在项目的**info.plist文件中添加:
[html] view
plain copy
<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里添加代码如下:
[objc] view
plain copy
- (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
plain copy
- (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
容芳志 (http://blog.csdn.net/totogo2010)
本文遵循“署名-非商业用途-保持一致”创作公用协议
写了一个很好的PDF阅读软件,那么怎么让用户根据提示打开我们的应用浏览阅读,提高程序的使用率呢?本文就是针对这个问题而来,方法:修改-Info.plist文件。
1.在plist文件中添加一个URLTypes字段,该字段指定程序的对外接口:
这部分可以参考我之前写的博客:iOS调用外部程序和系统程序。
2.另外添加一个Documents Type字段,该字段指定与程序关联的文件类型,详情参考System-Declared
Uniform Type Identifiers。
其中CFBundleTypeExtensions指定文件类型,例如pdf,doc,这个是不能随便填的。
CFBundleTypeIconFiles指定用UIActionSheet向用户提示打开应用时显示的图标。
DocumentTypeName可以自定,对应文件类型名。
Document Content Type UTIs指定官方指定的文件类型,UTIs即Uniform Type Identifiers。
如果要关联多个文件类型可以在Document Types中设置多个Item,这里我设置的关联类型包括pdf,doc,ppt。
接下来上代码。
AppDelegate类的代码:
[objc] view
plain copy
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// return YES;
//}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url && [url isFileURL]) {
DocumentViewController *documentViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
[documentViewController handleDocumentOpenURL:url];
return YES;
}
return NO;
}
其中handleOpenURL确定是否处理外部程序的调用请求,后者则是打开文件的URL。
再看看DocumentViewController中的处理方法:
[objc] view
plain copy
- (void)handleDocumentOpenURL:(NSURL *)url {
[self displayAlert:[url absoluteString]];
}
-(void) displayAlert:(NSString *) str {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:str
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
Run:
打开邮箱客户端,随便打开一封邮件中的附件,如pdf,doc格式,选择用其它应用打开(建议真机调试):
上面图标P对应的程序就是我们的Demo。
点击该Demo,我们的程序将被启动,并调用openURL方法做出处理,结果如下:
AlertView显示的内容就是下载的文件的fileURL。
如果在Mac上运行,打开App的文件夹可以看到对应下载的文件:
该文件在App的Documents/Inbox/目录下。
好吧,将接收到的PDF,DOC,PPT等格式的文件与程序关联的工作已经完成,源文件就在我们下载的目录中,接下来想怎么折腾这个文件都可以了。
相关文章推荐
- iOS App让自己的应用在其他应用中打开列表中显示、iOS把自己的应用添加到”活动“、将PDF文件Open In MyApp
- iOS让自己的App在其他应用中打开列表中显示
- iOS App让自己的应用在其他应用中打开列表中显示
- ios App 让自己的应用在其他应用打开列表显示
- iOS App让自己的应用在其他应用中打开列表中显示
- iOS App让自己的应用在其他应用中打开列表中显示
- iOS PDF之旅(二)将PDF文件Open In MyApp
- [置顶] iOS将本应用添加在‘其他方式打开’的列表中
- iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls
- 将PDF文件Open In MyApp
- 实现让你的应用在其他应用中文件的打开方式列表中显示
- android开发仿照第三方app打开方式,如何将你的app显示在打开列表,并且将文件复制到你的app应用内
- 让自己的应用在其他应用中打开列表中显示
- 让自己的应用在其他应用中打开列表中显示
- 通过手机其他iOS应用打开此文件
- 【iOS_GitHub】文档/文件查看器(支持本地或者其他app分享过来的word、excel、pdf、rtf等格式文件)
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
- 调用已经安装的APP来打开自己应用中的文件
- iOS 让自己的应用 在其他应用中打开
- 由文件的路径通过其他应用打开图片,PPT,Word,excel,PDF