2

I am trying to share an image on the Instagram application through my application.

Please check my code below and please let me know where I have gone wrong.

_instagramURL = [NSURL URLWithString:[NSString stringWithFormat: @"instagram://media?id=%@",[SingletoneClass sharedSingleTone].imageId]];

if ([[UIApplication sharedApplication] canOpenURL:_instagramURL]) {
    self.dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[SingletoneClass sharedSingleTone].imagePath]];
    self.dic.delegate = self;
    self.dic.UTI = @"com.instagram.exclusivegram";
    self.dic.annotation = [NSDictionary dictionaryWithObject:[SingletoneClass sharedSingleTone].captionStr forKey:@"InstagramCaption"];
    [self.dic presentOpenInMenuFromRect: CGRectZero inView:self.view animated:YES ];
}
else {
    UIAlertController *alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@"" message:@"Instagram is not present in your device" andCallback:^(id actionResponse) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }];
    [self presentViewController:alert animated:YES completion:nil];
}

If above code is correct, then please let me know, from where I can find the imageId?

Mr.Kushwaha
  • 491
  • 5
  • 14
Sambit Prakash
  • 341
  • 4
  • 15
  • Can you add description about SingletonClass file. What object does it return. – Anil Gupta Jan 02 '17 at 18:29
  • @AnilGupta are you asking about, UIAlertController *alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@"" message:@"Instagram is not present in your device" andCallback:^(id actionResponse) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; – Sambit Prakash Jan 02 '17 at 18:30
  • If you are sharing image from Photo Gallary or Images assets than simply pass the image object path into "_instagramURL". You can find imges ID from https://www.instagram.com/developer/ by registering you applcation. But for sharing you dont need to register you application. – Anil Gupta Jan 02 '17 at 18:43
  • you find link for sharing image from iOS application: http://stackoverflow.com/questions/11393071/how-to-share-an-image-on-instagram-in-ios – Anil Gupta Jan 02 '17 at 18:44
  • @AnilGupta, i have already gone through this link. I got the action sheet, where i choose copy to Instagram. after that nothing happened. May be i am missing something. – Sambit Prakash Jan 03 '17 at 05:18
  • from where did u get this above reference code? can you share Singlton Class file. – Anil Gupta Jan 03 '17 at 12:17
  • For the swift you can follow link. Its work for me. https://stackoverflow.com/questions/31337490/how-to-share-image-in-instagramswift – Mr.Javed Multani Nov 24 '17 at 06:00

2 Answers2

2

Finally, I got the solution. We need to save the image in Document directory. Instagram app can retrieve the image from Document directory.

NSData *imagedata = UIImageJPEGRepresentation(image, 0.5);
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager

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

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

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imagedata attributes:nil]; //finally save the path (image)

Then by using UIDocumentInteractionController, we can share the image in Instagram app. Before sharing the image, you need to be sure that Instagram app is present in the device.

- (void) shareImageToInstagram {
    _instagramURL = [NSURL URLWithString:[NSString stringWithFormat: @"instagram://app"]];

    if ([[UIApplication sharedApplication] canOpenURL:_instagramURL])   
    {
        self.dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[SingletoneClass sharedSingleTone].imagePath]];
        self.dic.delegate = self;
        self.dic.UTI = @"com.instagram.photo";
        self.dic.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[SingletoneClass sharedSingleTone].captionStr,@"InstagramCaption", nil];
        [self.dic presentOpenInMenuFromRect: CGRectZero inView:self.view animated:YES ];
    }
    else 
    {
        UIAlertController *alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@"" message:@"Instagram is not present in your device" andCallback:^(id actionResponse) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

You can refer the following links for more information. https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ and Share photo to Instagram from my iOS App

Community
  • 1
  • 1
Sambit Prakash
  • 341
  • 4
  • 15
  • Can you please tell me what is inside singleton class? Please upload complete code. – Abha Jan 24 '18 at 13:55
0

Check Below code for share image on Instagram , its working fine

import

-(void)shareImageOnInstagram:(UIImage *)image { if(![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://"]]) { //Your device doesn't have Instagram app. return; }

    //__weak typeof(self) weakSelf = self;
    __block PHFetchResult *photosAsset;
    __block PHObjectPlaceholder *placeholder;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        placeholder = [createAssetRequest placeholderForCreatedAsset];
        photosAsset = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier] options:nil];

    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
            requestOptions.synchronous = YES;
            PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
            allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

            NSString *identifier = placeholder.localIdentifier;
            dispatch_async(dispatch_get_main_queue(), ^{

            NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@",identifier]];

            if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
                [[UIApplication sharedApplication] openURL:instagramURL options:@{} completionHandler:^(BOOL success) {
                }];
            }
            });
        }
    }];

}

Tajinder singh
  • 738
  • 1
  • 9
  • 18