62
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Instagramへの画像投稿

Posted at

アプリからInstagramへ画像を投稿します。サービスへ直接投稿するためのAPIは用意されていないようなので、Instagramアプリに画像を渡します。アプリ間の画像の受け渡しに、UIDocumentInteractionController を使います。

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

// 2.
self.interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
self.interactionController.UTI = @"com.instagram.exclusivegram";

// 3.
BOOL result = [self.interactionController presentOpenInMenuFromRect:self.view.frame
														 inView:self.view
													   animated:YES];
if (!result) {
	// ファイルを開けるアプリがない
}

解説

  • 1.Instagram用の投稿画像を作る

拡張子".igo"の画像をファイルのNSURLが必要なので、Documentディレクトリに保存しておきます。Instagramアプリは正方形以外の画像も受け取りますが、正方形に収まるように強制リサイズされます。画像の保存は簡単に、

NSData *imageData = UIImageJPEGRepresentation(image, 0.9);
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.igo"];
[imageData writeToFile:filePath atomically:YES];

こんな感じで。

  • 2.UIDocumentInteractionController を準備する

開きたい画像ファイルのNSURLを使って、UIDocumentInteractionControllerを作ります。

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

はInstagramのみを対象としたい場合の設定です。

  • 3.開きます

Instagramのアイコンが表示されるので、タップすると、画像がInstagramに渡されます。

以上で投稿終わりです。

サンプル実装

例えば、以下のようなクラスを作っておくと便利だと思います。

PostInstagram.h

#import <Foundation/Foundation.h>

@interface PostInstagram : UIViewController <UIDocumentInteractionControllerDelegate>
{
	UIDocumentInteractionController *interactionCotroller;
}

+ (BOOL)canInstagramOpen;
- (void)setImage:(UIImage *)image;

@property (nonatomic,retain) UIDocumentInteractionController *interactionController;

@end

PostInstagram.m

#import "PostInstagram.h"

@interface PostInstagram()
- (void)closeView;
@end

@implementation PostInstagram

+ (BOOL)canInstagramOpen{
	NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
	if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
		return YES;
	}
	return NO;
}

- (void)openInstagram {
		
	NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.igo"];
	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.view.frame
															 inView:self.view
														   animated:YES];
	if (!present) {
		[self closeView];
	}
}

- (void)setImage:(UIImage *)image
{
	NSData *imageData = UIImageJPEGRepresentation(image, 0.75f);
	NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.igo"];
	[imageData writeToFile:filePath atomically:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
	[self openInstagram];
}
- (void)closeView
{
	[self.view removeFromSuperview];
	[self removeFromParentViewController];
	self.interactionController.delegate = nil;
}

#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];
}
@end

上記サンプルの使い方

#import "PostInstagram.h"

// ViewContollerからの呼び出しを想定
if ([PostInstagram canInstagramOpen]) {
	
	UIImage *image; // <- 投稿したい画像を準備しておく

	PostInstagram *instagramViewController = [[PostInstagram alloc] init];
	[instagramViewController setImage:image];
	[self.view addSubview:instagramViewController.view];
	[self addChildViewController:instagramViewController];

} else {
	// Instagramがインストールされていない
}

参考

62
61
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?