zoukankan      html  css  js  c++  java
  • iOS App让自己的应用在其它应用中打开列表中显示

    像百度网盘等应用,里面的文件打开时,都能够通过以下应用再打开文件。以下红色框框内的我的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

查看全文
  • 相关阅读:
    C++中new申请动态数组
    iOS 9之3D Touch功能
    在xcode找不到发布证书
    iOS 企业版 打包
    iOS证书详解--转载
    Failed to load the JNI shared library jvm.dl
    Xcode7.3打包ipa文件 报错和解决
    更新mac系统和更新到Xcode7.3版本出现的: cannot create __weak reference in file using manual reference counting
    Sun jdk, Openjdk, Icedtea jdk关系
    terminal color
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10770507.html
  • Copyright © 2011-2022 走看看