适用于iOS 9及更高版本的Instagram分享按钮10 [英] Instagram Sharing button for iOS 9 & 10

查看:155
本文介绍了适用于iOS 9及更高版本的Instagram分享按钮10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是程序设计的新手,我已经编写了一个即将完成的iOS应用程序,现在我想将内容(图像,文本和URL)共享功能集成到我的应用程序中,我已经实现了Facebook和Twitter共享功能,但自两天以来,我一直坚持使用Instagram共享功能. 我已经研究了许多材料(此处先前的问题 StackOverFlow ,也尝试了 MGInstagram 和其他一些第三方API,但所有这些均已过时&在iOS9和更高版本上无法正常工作iOS10, 我试图从 Instagram 网站获得一些帮助,但我也有没有清除.

I am new to Programing, I've programed an iOS app which is almost completed, Now I want to integrate contents(Image, Text and URL) sharing capability to my app, I've achieved Facebook and Twitter Sharing functionality, But I am stuck at Instagram sharing functionality since two days. I've studied lots of material(earlier asked question here StackOverFlow, also tried MGInstagram and few other third party API's) but all of those are outdated & not working on iOS9 & iOS10, I tried to achieve some help from Instagram site but there too I am not cleared.

任何人都可以定义一些简单的方法来在我的iOS应用程序中集成Instagram共享按钮代码,也可以从Instagram站点定义身份验证过程吗,就像令牌&开发人员需要的其他权限.

Can Anyone Define some easy way to integrate Instagram Sharing button code in my iOS app also to define authentication process from Instagram site clearly like as tokens & other permissions required for developers.

谢谢

推荐答案

根据您对iOS 9和10的期望问题,我将同时为这两者进行解释. 我使用的是 iPhone挂钩中定义的Instagram标准文档API.在Instagram上

As per your Desired question for iOS 9 and 10 So I am explaining for both. I am using Instagram,s Standard Documentation API as defined in iPhone Hooks at Instagram

第一步:

打开info.plist文件,然后添加LSApplicationQueriesSchemes并添加要在canOpenURL中调用的URL方案,如下所示. kitty scatter WWDC 2015会议703 中也定义了它. /em>.

open your info.plist file and add LSApplicationQueriesSchemes and add your URL schemes which you are going to call in your canOpenURL like this. It is also defined in WWDC 2015 Session 703 by kitty scatter.

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

它将打开您设备中安装的instargram应用.

It will open instargram app installed in your device.

注释,因为必需的应用程序不可用,因此它将永远无法在您的模拟器中使用.

note It'll never work in your simulator because of unavailability of required app.

第二步:

将此<UIDocumentInteractionControllerDelegate>添加到同一@interface

第3步:

在instagram共享按钮的操作中添加以下代码以共享图像

add following code for sharing image in your instagram sharing button's action

// 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),请按照步骤1进行操作,并删除第三步中提到的第二行代码.如果您使用的是iOS 9至9.2.3,则无需执行第一步,则应遵循第三步的第二行代码.

Final Note: Follow Step one if you are using iOS SDK 9.5 or above like 10. and to remove second line of code mentioned in third step. and in case you are using iOS 9 till 9.2.3 then no needs to do first step, you should follow second line of code of third step.

希望它会像水流一样平稳运行. . . :)

Hope it'll work smoothly like water flow. . . :)

这篇关于适用于iOS 9及更高版本的Instagram分享按钮10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆