iOS 6 ソーシャル フレームワークが設定に移動しないか、アラートが表示されないのと同様ですが、SLRequest を使用しようとしています。
ユーザーが設定で Facebook にログインしていないときに、「Facebook アカウントがありません」というアラートを表示しようとしています。if ステートメント内ではなく、SLComposeViewController を提示した後にアラートが表示されることがわかりました。
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
//setup controller and callback code
[self presentViewController:controller animated:YES completion:nil];
}
ただし、SLRequest を使用しようとしていますが、SLComposeViewController を表示したくありませんが、アカウントを確認した後、アラートをポップアップ表示します。私のコードはここにあります:
- (void)postImageFB
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSLog(@"can post");
} else {
NSLog(@"cant post");
}
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(@"accounts: %i", [accountsArray count]);
// Is it possible to popup the alert here if accounts = 0?
NSDictionary *options = @{ ACFacebookAppIdKey: @"123456789", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
NSDictionary *parameters = @{@"message": @"testing"};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
parameters:parameters];
[facebookRequest addMultipartData: [self getImageDataFromPlistWithFilename:@"image1.png"]
withName:@"source"
type:@"multipart/form-data"
filename:@"TestImage"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (error) {
NSLog(@"%@",error.description);
} else {
NSLog(@"responedata:%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
}];
}
}
}];
}