Social.hをインポートする。
・Twitter投稿の場合
SLComposeViewControllerでTwitter投稿画面を表示する。
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
// CompletionHandlerを設定
[composeVC setCompletionHandler:^(SLComposeViewControllerResult result) {
[self dismissViewControllerAnimated:YES completion:nil];
if (result == SLComposeViewControllerResultDone) {
NSLog(@"sendTwitter success");
}
}];
// POSTするテキストの初期設定
NSString* message = [NSString stringWithFormat:@"XXXXXXXXXX"];
[composeVC setInitialText:message];
// URLをPOSTする場合
[composeVC addURL:[NSURL URLWithString:@"http://XXXXXXXX"]];
// 画像をPOSTする場合
[composeVC addImage:[UIImage imageNamed:@"XXXXX"]];
// SLComposeViewController表示
[self presentViewController:composeVC animated:YES completion:nil];
}
CompletionHandlerに入れたdismissViewControllerAnimatedはiOS7.Xではなくても動いたが、iOS6.Xでは入れないと投稿後の画面操作ができなくなる。
・Facebook投稿の場合
SLComposeViewControllerでFacebook投稿画面を表示する。
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[composeVC setCompletionHandler:^(SLComposeViewControllerResult result) {
[self dismissViewControllerAnimated:YES completion:nil];
if (result == SLComposeViewControllerResultDone) {
DEBUG_LOG(@"sendFacebook success");
}
}];
// POSTするテキストの初期設定
NSString* message = [NSString stringWithFormat:@"XXXXXXXXXX"];
[composeVC setInitialText:message];
// URLをPOSTする場合
[composeVC addURL:[NSURL URLWithString:@"http://XXXXXXXX"]];
// 画像をPOSTする場合
[composeVC addImage:[UIImage imageNamed:@"XXXXX"]];
// SLComposeViewController表示
[self presentViewController:composeVC animated:YES completion:nil];
}
CompletionHandlerに入れたdismissViewControllerAnimatedはなくても動く。
0 件のコメント:
コメントを投稿