電子の海をたゆたう

iOS初心者がXcodeでアプリ開発を学んでいく日記です。

【iOS】アプリからインスタグラムに投稿する

 最近、急に寒くなって朝が辛くなってきました。

東京の寒さと九州の寒さはやはりちょっと違うと改めて感じたこの頃です。

 

 

今回、作成しているアプリからInstagramに投稿する機能を実装してみました。

 

アプリからハッシュタグを送りたいなと思っていたので、

Instagramハッシュタグを送るコードを試しに入れてみたのですが、

何度試しても、Instagramに反映されない・・・

 

なんでや・・・・と思って調べた結果、

Instagram公式にこのようなことが書かれていました。

http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing

英語があまり得意じゃないので、わかるのに時間がかかったのですが

どうやらFacebookなどと同じようにアプリからハッシュタグを送れな異様になったとか。

 

仕方なしに、ハッシュタグなしで実装した結果、

UIDocumentInteractionControllerを使うといいとのことなので

やってみました。

 

-----------------------------------------------------------------------------------------------------

Instagram.h

Objective-C

@interface SnapshotView : UIView<UIDocumentInteractionControllerDelegate>{}

 

@property (strong) UIImageView *imgView;

@property(nonatomic,retain)UIDocumentInteractionController *interactionController;

-----------------------------------------------------------------------------------------------------

Instagram.mm

Objective-C

- (void)openInstagram{

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];

    if(![[UIApplication sharedApplication] canOpenURL:instagramURL]){

        NSLog(@"Not instle Instagram");

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message"

                                                                                   message:@"Not install Instagram"

                                                                                    delegate:nil

                                                                      cancelButtonTitle:@"OK"

                                                                      otherButtonTitles:nil];

        [alertView show];

        return;

    }

    

    NSData *imageData = UIImagePNGRepresentation(_imgView.image);

    NSString *filePath =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.igo"];  

    [imageData writeToFile:filePath atomically:YES];

    NSURL *fileURL = [NSURL fileURLWithPath:filePath];

 

    self.interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];

    self.interactionController.UTI = @"com.instagram.exclusivegram";

    self.interactionController.delegate = self;

 

    BOOL present = [self.interactionController presentOpenInMenuFromRect:self.frame

                                                                                                               inView:self

                                                                                                           animated:YES];

      if(!present){

          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message"

                                                                                     message:@"miss"

                                                                                      delegate:nil

                                                                        cancelButtonTitle:@"OK"

                                                                        otherButtonTitles:nil];

          [alertView show];

      }

 } 

 

#pragma mark - UIDocumentInteractionControllerDelegate

 

- (void)documentInteractionController:(UIDocumentInteractionController *)controller

        willBeginSendingToApplication:(NSString *)application

{

    

}

 

- (void)documentInteractionController:(UIDocumentInteractionController *)controller

           didEndSendingToApplication:(NSString *)application

{

    //[self closeView];

}

 

- (void) documentInteractionControllerDidDismissOpenInMenu: (UIDocumentInteractionController *) controller

{

    // キャンセルで閉じたとき

    //[self closeView];

}

-----------------------------------------------------------------------------------------------------

 

これでなんとかInstagramに画像を送ることができました。 

おかしい箇所などがありましたら、コメントをいただけると幸いです。