#import <Social/Social.h>
NSString *serviceType;
if (type == POSTING_FACEBOOK) {
serviceType = SLServiceTypeFacebook;
}else{
serviceType = SLServiceTypeTwitter;
}
if ([SLComposeViewController isAvailableForServiceType:serviceType]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:serviceType];
[controller setCompletionHandler:^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultDone) {
// 投稿画面が閉じるのを待ってから、アラート表示を行う
[NSThread sleepForTimeInterval:0.5];
dispatch_async(dispatch_get_main_queue(), ^{
//投稿成功時の処理
NSString *message;
if (type == POSTING_FACEBOOK) {
message = @"facebookに投稿しました";
}else{
message = @"twitterに投稿しました";
}
id alertView = [ScreenStatusChk uialertViewInit:@""
massageString:message
cancelString:@"OK"
controller:nil];
[ScreenStatusChk uialertViewOpen:alertView controller:self];
});
}
}];
//保存する画像を取得
[self getImageData:&image];
NSString *string = [NSString stringWithFormat:@"追加イメージ"];
[controller setInitialText:string];
[controller addImage:image];
[self presentViewController:controller
animated:NO
completion:NULL];
}
#import "Reachability.h"そして、先ほどのソースの前に、下記のソースを追加して、投稿処理の前に、それぞれのサーバーとの接続状態のチェックを行う。
if (type == POSTING_FACEBOOK) {
reachability = [Reachability reachabilityWithHostName:@"www.facebook.com"];
}else{
reachability = [Reachability reachabilityWithHostName:@"twitter.com"];
}
NetworkStatus status = [reachability currentReachabilityStatus];
if (status == ReachableViaWiFi) {
// wifi接続時
} else if (status == ReachableViaWWAN) {
// 3G接続時
} else if (status == NotReachable) {
// 接続不可
id alertView = [ScreenStatusChk uialertViewInit:@"接続エラー"
massageString:@"電波状況の良いところでやり直してください"
cancelString:@"OK"
controller:nil];
[ScreenStatusChk uialertViewOpen:alertView controller:self];
return;
}