如何在我们的iOS APP中集成Instagram Direct功能?

时间:2014-01-16 09:27:50

标签: ios iphone objective-c instagram

我们可以在我们的iOS应用程序中集成Instagram Direct功能吗?有没有可用的API?

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码:(注意:如果安装在设备上,则会打开Instagram应用,并在Instagram应用中通过应用中的文字传递图片)。

// In .h
@property (strong,nonatomic) UIDocumentInteractionController *docController;

// in .m
// Share on instagram ************
- (void)shareToInstagram
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"image.ig" ];
    NSData* data = UIImagePNGRepresentation(yourImageView.image);
    [data writeToFile:path atomically:YES];

    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:
                           @"image.ig" ];

    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    self.docController.UTI = @"com.instagram.photo";
    self.docController = [self setupControllerWithURL:[NSURL fileURLWithPath:imagePath] usingDelegate:self];
    self.docController=[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
    NSString * msgBody = [NSString stringWithFormat:@"Your message"];
    self.docController.annotation = [NSDictionary dictionaryWithObject:msgBody forKey:@"InstagramCaption"];
    [self.docController presentOpenInMenuFromRect: rect inView: self.view animated: YES ];

}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;
    return interactionController;
}

- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {

}
// *********** ********* ***********