2012-03-01 60 views
0

我想通過我的應用程序將照片張貼到Instagram的。我使用UIDocumentInteractionController根據INstagram guidlines整合iPhone

在我.h文件中

UIDocumentInteractionController *documentInteractionController; 

在我的.m文件

NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"IMG_0192" ofType:@"jpg"]; 
fileToOpen = [fileToOpen substringToIndex:[fileToOpen length] - 3]; 
fileToOpen=[NSString stringWithFormat:@"%@ig",fileToOpen]; 
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; 
NSLog(@"%@",fileToOpen); 
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { 
    //imageToUpload is a file path with .ig file extension 
    /*UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"" 
          message:@"Can open app!" 
          delegate:self 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 
    */ 



    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]]; 
    self.documentInteractionController.UTI = @"com.instagram.photo"; 
    self.documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"Its a testing" forKey:@"InstagramCaption"]; 

是我缺少something.??it不工作。

+0

哎你這樣做呢?我需要你的幫助 – Hiren 2012-07-09 11:23:36

+0

使用此鏈接http://www.albinopixel.com/blog/posting-to-instagram-from-your-app/ – 2012-12-24 10:35:11

回答

1

我覺得你的照片的證件或文件的URL是不正確的位置。

在.h文件中

@property (nonatomic, retain) UIDocumentInteractionController *docDic; 

還添加以下委託:

UIDocumentInteractionControllerDelegate 

在.m文件:我用下面的代碼片段成功做到了這一點

  1. 合成docDic對象

  2. 寫下面的方法:

    -(UIDocumentInteractionController *)setupControllerWithURL:(NSURL*)fileURL usingDelegate:(id<UIDocumentInteractionControllerDelegate>)interactionDelegate{ 
        UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL]; 
        interactionController.delegate = interactionDelegate; 
        return interactionController; 
    } 
    
  3. 然後將下面的代碼添加到您的shareToInstagram功能:

    的NSString * fileToOpen = [[一個NSBundle mainBundle] pathForResource:@ 「IMG_0192」 ofType:@」 JPG「];

    fileToOpen = [fileToOpen substringToIndex:[fileToOpen長度] - 3];

    fileToOpen = [NSString的stringWithFormat:@ 「%@ IG」,fileToOpen];我們可以通過下面的方法來解決這個問題:NSURL * instagramURL = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@「file://%@」,fileToOpen]];

    如果([[UIApplication的sharedApplication] canOpenURL:instagramURL]){

    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]]; 
    

    self.documentInteractionController.UTI = @ 「com.instagram.photo」;

    [self.dic presentOpenInMenuFromRect:CGRectZero inView:self.view動畫:YES]; }

希望它能幫助你。祝你好運。 :-)