Search code examples
iosobjective-cshareinstagramsocial-media

objective-c share image to Instagram from my iOS application


I've spent a long time searching how to share photo from iOS application to Instagram. Other social media like Twitter are working fine. I have found a lot of resources on Google but it's not working. Please I need some help to add Instagram share button to the UIActivityViewController that already has Twitter share button. Here is my code:

- (IBAction)shareBtnClicked {
    NSString *textToShare = @"Share \r";
    NSURL *url = newsC.URL;
    NSArray *objectsToShare = @[textToShare, url];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    //this array should add the activities that I don’t want
    NSArray *excludeActivities = @[ UIActivityTypePrint,
                                    UIActivityTypeAssignToContact,
                                    UIActivityTypePostToFlickr,
                                    UIActivityTypePostToVimeo
                                    ];

    activityVC.excludedActivityTypes = excludeActivities;
    [self presentViewController:activityVC animated:YES completion:nil];

Thanks in advance


Solution

  • You need to use below method:

     -(void)instaPostImage
        {
            NSURL *myURL = [NSURL URLWithString:@"Your image url"];
            NSData * imageData = [[NSData alloc] initWithContentsOfURL:myURL];
            UIImage *imgShare = [[UIImage alloc] initWithData:imageData];
    
            NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
    
            if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
            {
                UIImage *imageToUse = imgShare;
                NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
                NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.png"];
                NSData *imageData=UIImagePNGRepresentation(imageToUse);
                [imageData writeToFile:saveImagePath atomically:YES];
                NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
                self.documentController=[[UIDocumentInteractionController alloc]init];
                self.documentController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
                self.documentController.delegate = self;
                self.documentController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Testing"], @"InstagramCaption", nil];
                self.documentController.UTI = @"com.instagram.exclusivegram";
                UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
                [self.documentController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:vc.view animated:YES];
        }
        else {
            DisplayAlertWithTitle(@"Instagram not found", @"")
        }
    }