(iOS 6.0 及更高版本)
我称这个函数为:
- (void)askUserToLoginToFacebookInSettings
{
if(![SLComposeViewController isAvailableForServiceType: SLServiceTypeFacebook])
{
// Facebook Service Type is Available
SLComposeViewController *slVC = [SLComposeViewController composeViewControllerForServiceType: SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler handler = ^(SLComposeViewControllerResult result)
{
[slVC dismissViewControllerAnimated:YES completion:Nil];
};
slVC.completionHandler = handler;
[slVC.view setHidden:YES];
[(MainViewController *)[(SettingsLayoutView *)delegate delegate] presentModalViewController:slVC animated:NO];
[slVC.view endEditing:YES];
}
else
{
NSLog(@"Service Unavailable!!!");
}
}
然后向用户显示警报,这在您提供的屏幕截图中可见。不会显示任何帖子表单。
但在此之前 - 我请求允许这样做:
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
{
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [accountStore accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierFacebook];
NSDictionary *options = @{
ACFacebookAppIdKey: @"xxxxxxxxxxx",
ACFacebookPermissionsKey: @[@"user_birthday"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:FBaccountType options:options
completion: ^(BOOL granted, NSError *e)
{
if (granted)
{
NSLog(@"access granted");
}
else
{
NSLog(@"error getting permission %@",e);
}
}];
}