【问题标题】:Instagram Sharing button for iOS 9 & 10适用于 iOS 9 和 10 的 Instagram 分享按钮
【发布时间】:2017-03-11 06:43:54
【问题描述】:

我是编程新手,我已经编写了一个几乎完成的 iOS 应用程序,现在我想将内容(图像、文本和 URL)共享功能集成到我的应用程序中,我已经实现了 Facebook 和 Twitter 共享功能,但是两天以来我一直在使用 Instagram 共享功能。 我研究了很多材料(之前在这里问过问题StackOverFlow,也尝试过MGInstagram 和其他一些第三方API)但所有这些都已经过时并且不适用于iOS9和iOS10, 我试图从Instagram 网站获得一些帮助,但我也没有被清除。

任何人都可以定义一些简单的方法来将 Instagram 共享按钮代码集成到我的 iOS 应用程序中,还可以清楚地定义来自 Instagram 网站的身份验证过程,例如开发人员所需的令牌和其他权限。

谢谢

【问题讨论】:

  • 正在检查指定的链接,我将从 instagram 获取客户端 ID,但是当我按下按钮时,您能否分享我按下按钮事件的实际代码它为我的应用分享图片一些文本和 URL到 Instagram
  • 好吧,我曾做过一个示例项目,该项目从 instagram 获取 json 响应,并在 imageView 中显示用户的最新 instagram 照片,如果您愿意,我可以将其上传到 Github,您可以查看项目
  • 不,谢谢,这与我的问题无关。
  • 我仍在努力,希望能在接下来的几个小时或一天内得到我所需问题的答案,一旦完成,我会将我的代码上传到这里的答案部分。或者,如果您或其他任何人可以找到任何绝对解决方案,我们将不胜感激。谢谢

标签: ios objective-c instagram-api


【解决方案1】:

根据您对 iOS 9 和 10 的期望问题,所以我正在为两者进行解释。 我在 Instagram 使用 iPhone Hooks 中定义的 Instagram 标准文档 API

第一步:

打开您的 info.plist 文件并添加 LSApplicationQueriesSchemes 并添加您将在 canOpenURL 中调用的 URL 方案,如下所示。 它也是由 kitty scatterWWDC 2015 Session 703 中定义的。

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>instagram</string>
</array> 

它将打开安装在您设备中的 instargram 应用程序。

注意由于所需的应用程序不可用,它永远无法在您的模拟器中工作。

第二步:

@interface 行结束的.h 文件中,将此&lt;UIDocumentInteractionControllerDelegate&gt; 添加到同一行@interface

第三步:

在您的 Instagram 分享按钮的操作中添加以下代码以分享图片

// This is your Image you want to share. you can write 
 UIImage *image = imageView.image;

 //Add this line of code instead of doing step One if you are at early version of iOS9 till iOS 9.2.3  but if you are using iOS9.5 or above like as iOS10 not use this line and do step1 instead of writing this line of code   
  NSURL *instagram = [NSURL URLWithString:@"instagram://app"];



 if ([[UIApplication sharedApplication] canOpenURL:instagram]) {

    //convert image into .png format.
    NSData *imageData = UIImagePNGRepresentation(image);

    //create instance of NSFileManager
    NSFileManager *fileManager = [NSFileManager defaultManager];

    //create an array and store result of our search for the documents directory in it
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //create NSString object, that holds our exact path to the documents directory
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //add our image to the path
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]];

    //finally save the path (image)
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    NSString *fileNameToSave = [NSString stringWithFormat:@"Documents/insta.igo"];
    NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNameToSave];
    NSLog(@"jpg path %@",jpgPath);

    NSString *newJpgPath = [NSString stringWithFormat:@"file://%@",jpgPath];
    NSLog(@"with File path %@",newJpgPath);

   // NSURL *igImageHookFile = [[NSURL alloc]initFileURLWithPath:newJpgPath];

    NSURL *igImageHookFile = [NSURL URLWithString:newJpgPath];

    NSLog(@"url Path %@",igImageHookFile);

    UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
     documentController.delegate = self;
    [documentController setUTI:@"com.instagram.exclusivegram"];
    [documentController presentOpenInMenuFromRect:rect inView:self.view animated:YES];

} else {
   // NSLog (@"Instagram not found");
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Install Instagram"
                                                                   message:@" Before Sharing to Instagram, Please Install Instagram app to your Device. "
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Thanks" style:UIAlertActionStyleDefault
                                                          handler:nil];

    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];

}

最后说明: 如果您使用 iOS SDK 9.5 或更高版本(如 10),请按照第一步操作。并删除第三步中提到的第二行代码。如果您使用 iOS 9 到 9.2.3,则无需执行第一步,您应该遵循第三步的第二行代码。

希望它能像水流一样顺畅地工作。 . . :)

【讨论】:

  • 让我试试,我很快就会回来。
  • 我试图标记它,弹出消息告诉我我需要至少 15 个声望才能标记为标记。所以我简单地批准了接受。谢谢
  • @MDIbrahim_Hassan, canOpenUrl 永远不会在您的模拟器中运行,它只会在您的设备中运行,您是否配置了您的 plist 文件,正如我在上面提到的 step1 用于 iOS plist 文件配置。会有 url 字符串,但你必须为 instagram 添加那个字符串。请仔细阅读第1步。
【解决方案2】:

这应该会在设备上打开 Instagram 并将照片分享到 Instagram。

  - (IBAction)imageShareToInsta:(id)sender {
        NSString *imagePath = [NSString stringWithFormat:@"%@/image.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]];
        [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
        [UIImagePNGRepresentation(self.imageView.image) writeToFile:imagePath atomically:YES];
        UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
        docController.delegate=self;
        docController.UTI = @"com.instagram.exclusivegram";
        [docController presentOpenInMenuFromRect:self.view.frame inView:self.view  animated:YES];   
    }

在您的 ViewController.h 文件中声明 &lt;UIDocumentInteractionControllerDelegate&gt; 希望它可以帮助你。

【讨论】:

  • 但仅在设备上而非模拟器上,因为它需要安装 instagram。测试一下,然后接受我的回答
  • 你从哪里复制的?你能给我一些参考吗?
  • 我刚刚改造了我之前用于 Instagram api 的项目中的代码。好吧,这是来自实际设备的工作屏幕投射youtube.com/watch?v=5S4rNisN4tU&feature=youtu.be
  • 有两个错误。 [UIImagePNGRepresentation(self.imageView.image) writeToFile:imagePath atomically:YES]; 中的一个 self.imageView.image 应该是这样的 cell.imageView.image 我更正了。但是在下面的行中还有另一个问题 docController.delegate=self;从不兼容的类型 DetailTableView 分配给 'id _nulable' 其中 DetailTableView 是我的 viewController 的名称,所以请建议如何解决这个问题,我尽了最大努力但未能成功...... .
  • 通过在 .h 文件中声明 &lt;UIDocumentInteractionControllerDelegate&gt; 也消除了该问题。正在测试中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
相关资源
最近更新 更多