【问题标题】:Automatically select Instagram in UIDocumentInteractionController or UIActivityViewController在 UIDocumentInteractionController 或 UIActivityViewController 中自动选择 Instagram
【发布时间】:2016-07-20 18:28:43
【问题描述】:

UIDocumentInteractionController 和 UIActivityViewController 都提供用于将图像共享到其他网络的菜单。在点击 Instagram 后,您会看到一个不错的模式,允许您在不离开应用程序的情况下发布到 Instgram。我的问题是,如何在不显示菜单的情况下自动显示该模式?

这个名为 Sounds 的应用可以做到这一点,所以我知道这是可能的,但我在网上找不到任何关于它是如何完成的文档。这是显示菜单的当前代码:

    NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.ig"];
    NSData *imageData=UIImagePNGRepresentation(cardImage);
    [imageData writeToFile:saveImagePath atomically:YES];
    NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];

    UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
    docController.UTI = @"com.instagram.exclusivegram";
    docController = [self setupControllerWithURL:imageURL usingDelegate:self];
    [docController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:self.view animated:YES];

菜单如下:

这是我要自动显示的内容:

【问题讨论】:

  • 您找到解决问题的方法了吗?我也有同样的问题。

标签: ios objective-c instagram uiactivityviewcontroller uidocumentinteraction


【解决方案1】:

只要活动项中只有一个 UIImage,您就可以使用 UIActivityViewController 显示 Instagram。 如果您添加更多项目,如文本、网址等,它将不会显示。 这是 Instagram 的一个不好的限制。

像这样:

NSMutableArray *items = [NSMutableArray array];
[items addObject:[UIImage ...]];

// show view
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items
                                                                             applicationActivities:nil];
[vc presentViewController:activityVC animated:YES completion:nil];

【讨论】:

    【解决方案2】:

    我用来在 Instagram 上分享图片的代码

    import UIKit
    import Photos
    
    class SocialShare: NSObject {
        static let shared = SocialShare()
    
        func postImageToInstagram(image: UIImage) {
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
        }
        func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
            if error != nil {
                print(error)
            }
    
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
            let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
            if let lastAsset = fetchResult.firstObject as? PHAsset {
                let localIdentifier = lastAsset.localIdentifier
                let u = "instagram://library?LocalIdentifier=" + localIdentifier
                UIApplication.sharedApplication().openURL(NSURL(string: u)!)
            }
        }
    }
    

    没有任何你想使用的地方,只需调用

    SocialShare.shared.postImageToInstagram(UIImage(named: "1"))
    

    【讨论】:

      【解决方案3】:

      这是一个实现 Zuhair 答案的 Swift 4 类。

      import Foundation
      import Photos
      
      class InstagramSharer: NSObject {
          static let shared = InstagramSharer()
      
          func post(image: UIImage) {
              UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
          }
      
          @objc
          private func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
              guard error == nil else {
                  return
              }
      
              let fetchOptions = PHFetchOptions()
              fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
              let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
      
              if let lastAsset = fetchResult.firstObject {
                  let localIdentifier = lastAsset.localIdentifier
                  let url = "instagram://library?LocalIdentifier=" + localIdentifier
                  UIApplication.shared.openURL(URL(string: url)!)
              }
          }
      }
      

      用法

      InstagramSharer.shared.post(image: #YOUR_IMAGE#)
      

      【讨论】:

        【解决方案4】:

        要移除显示的其他应用程序,请更改以下代码行(只需翻转它们):

        docController.UTI = @"com.instagram.exclusivegram";
        docController = [self setupControllerWithURL:imageURL usingDelegate:self];
        

        这样在覆盖 docController 对象后设置 .UTI 变量。

        编辑:如果您尝试直接打开 Instagram 应用程序并填写信息并准备分享,请查看 iPhone 挂钩 here

        【讨论】:

        • 我知道这是一个非常具体的要求,但 iPhone 挂钩并不是我们真正想要的。我们不想离开应用程序,我们只想自动拉出该模式。这个应用程序听起来 (sounds.am) 做到了。您单击一个 Instagram 按钮,它会立即显示该模式。知道这是怎么做到的吗?
        • 这正是我过去 2 天一直在谷歌搜索的内容。你找到答案了吗?我见过应用程序这样做,所以我知道这是可能的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多