iOS讓自己的App在其他應用中開啟列表中顯示

來源:互聯網
上載者:User

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上查看代碼片">

  1. CFBundleDocumentTypes
  2. CFBundleTypeIconFiles
  3. icon@2x.png
  4. icon.png
  5. CFBundleTypeName
  6. Molecules Structure File
  7. CFBundleTypeRole
  8. Viewer
  9. LSHandlerRank
  10. Owner
  11. LSItemContentTypes
  12. com.fzrong.jpg
  13. org.gnu.gnu-zip-archive
  14. UTExportedTypeDeclarations
  15. UTTypeConformsTo
  16. public.plain-text
  17. public.text
  18. UTTypeDescription
  19. Molecules Structure File
  20. UTTypeIdentifier
  21. com.fzrong.jpg
  22. UTTypeTagSpecification
  23. public.filename-extension
  24. jpg
  25. public.mime-type
  26. image/jpg
  27. 這就是告訴系統,你能開啟 jpg這個檔案類型。


    2、開啟到自己的app時,要截取到過來資源的檔案路徑:

    在Appdelegate裡添加代碼如下:

    [objc] view plaincopy
    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. return YES;
    12. }

      要去掉file://檔案路徑的頭,要不然找不到資源。

      3、在自己的ViewController裡開啟jgp顯示:

      [objc] view plaincopy
      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. CGRect frame = CGRectMake(0, 20, width, height);
      11. imageView.frame = frame;
      12. imageView.image = image;
      13. }
        開啟之後的效果是這樣的:

        html

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.