简体   繁体   English

适用于iOS 9和10的Instagram分享按钮

[英]Instagram Sharing button for iOS 9 & 10

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. 我是编程的新手,我已经编写了一个即将完成的iOS应用程序,现在我想将内容(图像,文本和URL)共享功能集成到我的应用程序中,我已经实现了Facebook和Twitter共享功能,但是我自两天以来一直坚持使用Instagram分享功能。 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. 我研究过很多材料(刚才问此问题的StackOverflow ,也试过MGInstagram和其他一些第三方的API),但所有这些都是过时的和不工作的iOS9&iOS10,我试图实现从一些帮助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应用程序中集成Instagram共享按钮代码的方法,也可以从Instagram网站明确定义身份验证过程,例如令牌和开发人员所需的其他权限。

Thanks 谢谢

As per your Desired question for iOS 9 and 10 So I am explaining for both. 根据您对iOS 9和iOS 10的期望问题,我为两者进行解释。 I am using Instagram,s Standard Documentation API as defined in iPhone Hooks at Instagram 我正在使用Instagram的iPhone挂钩中定义的Instagram标准文档API

Step1: 第1步:

open your info.plist file and add LSApplicationQueriesSchemes and add your URL schemes which you are going to call in your canOpenURL like this. 打开info.plist文件,然后添加LSApplicationQueriesSchemes并添加您要在canOpenURL调用的URL方案,如下所示。 It is also defined in WWDC 2015 Session 703 by kitty scatter . WWDC 2015 Session 703中kitty scatter也定义了它。

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

It will open instargram app installed in your device. 它将打开您设备中安装的instargram应用。

note It'll never work in your simulator because of unavailability of required app. 注意由于必需的应用程序不可用,它将永远无法在您的模拟器中工作。

Step2: 第2步:

in your .h file where your @interface line ends add this <UIDocumentInteractionControllerDelegate> to the same @interface line 在您的@interface行结尾的.h文件中,将此<UIDocumentInteractionControllerDelegate>添加到同一@interface

Step3: 第三步:

add following code for sharing image in your instagram sharing button's action 在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];

}

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. 最后说明:如果您使用的是iOS SDK 9.5或更高版本(如10), 请执行步骤1,并删除第三步中提到的第二行代码。 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. 如果您使用的是iOS 9至9.2.3,则无需执行第一步,则应遵循第三步的第二行代码。

Hope it'll work smoothly like water flow. 希望它能像水一样顺畅地工作。 . . :) :)

This should open instagram on a device and share the photo onto Instagram. 这应该在设备上打开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];   
    }

Declare <UIDocumentInteractionControllerDelegate> in your ViewController.h file Hope it helps you out. 在ViewController.h文件中声明<UIDocumentInteractionControllerDelegate>希望对您<UIDocumentInteractionControllerDelegate>帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM