skip to Main Content

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.

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.

Thanks

2

Answers


  1. This should open instagram on a device and share the photo onto 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.

    Login or Signup to reply.
  2. 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

    Step1:

    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> 
    

    It will open instargram app installed in your device.

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

    Step2:

    in your .h file where your @interface line ends add this <UIDocumentInteractionControllerDelegate> to the same @interface line

    Step3:

    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];
    
    }
    

    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. . . 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search