如何在本地应用程序ios 9.2上分享Instagram上的图像?

时间:2016-03-02 10:45:30

标签: objective-c instagram ios9.2

我尝试过此代码,但显示错误

  

警告:尝试显示< _UIDocumentActivityViewController:   0x16acdc00>哪个已经   呈现< _UIDocumentActivityViewController:0x16ae4800>

如何解决这个问题?

-(IBAction)saveToInstagram:(id)sender {


        CGRect rect = CGRectMake(0 ,0 , 0, 0);
        NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.ig"];

        NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
        NSLog(@"JPG path %@", jpgPath);
        NSLog(@"URL Path %@", igImageHookFile);
        self.docFile.UTI = @"com.instagram.photo";
        self.docFile = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
        self.docFile=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
        [self.docFile presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];
        NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=MEDIA_ID"];
        if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
            [self.docFile presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];
        }
        else {
            NSLog(@"No Instagram Found");
        }



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

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

}

2 个答案:

答案 0 :(得分:2)

问题在于,在呈现第一个docfile时,您尝试呈现第二个

Accessing class/method/instances

我真的不明白为什么你需要两次使用相同的方法,但如果你真的这样做,那么第一次演示使用动画:NO。我会建议某种完成代码块,但似乎没有// First presentation [self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ]; NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=MEDIA_ID"]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { // Second presentation [self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ]; }

这样的方法

答案 1 :(得分:0)

Don't forget to add <string>instagram</string> in you plist array with key: LSApplicationQueriesSchemes.

    @property (nonatomic, strong) UIDocumentInteractionController *docIC;        

    - (void)sharePhoto:(UIImage *)image caption:(NSString *)caption
    {
        NSURL *instagramURL = [NSURL URLWithString:@"instagram://"];
        if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
        {
            NSString *imgPath = [TUtil documentPath:@"Image.igo"];
            NSData *data = UIImageJPEGRepresentation(image, 1.0);
            [data writeToFile:imgPath atomically:YES];


            NSURL *igImageHookFile = [NSURL fileURLWithPath:imgPath];
            self.docIC = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
            [self.docIC setDelegate:self];
            [self.docIC setUTI:@"com.instagram.exclusivegram"];

            // Subject
            [self.docIC setName:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];

            //http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing
            //self.docIC.annotation = [NSDictionary dictionaryWithObjectsAndKeys:caption, @"InstagramCaption", nil];

            [self.docIC presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
        }
        else
        {
            NSString *iTunesLink = @"itms-apps://itunes.apple.com/app/id389801252?mt=8";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
        }
    }