我在iOS6
中使用新的Facebook集成,如下所示:
SLComposeViewController *fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(@"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(@"Posted....");
}
break;
}};
//[fbController addImage:[UIImage imageNamed:@"1.jpg"]];
[fbController setInitialText:@"Test message"];
[fbController addURL:[NSURL URLWithString:self.asset.url]];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
} else {
NSLog(@"no facebook setup");
}
这里的问题是,我正在测试它而没有登录Facebook
,我得到的只是日志消息。
**奇怪的是,我在模拟器中得到了对话框,但不是设备!**
如何向用户显示告知他们需要登录Facebook的提醒?我已经看过系统警报的屏幕截图,但由于某种原因我没有得到它。我做错了什么?
答案 0 :(得分:28)
删除对[SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]
的检查为我解决了问题。
答案 1 :(得分:7)
似乎[SLComposeViewController isAvailableForServiceType:]
在模拟器中返回true,即使您尚未在那里设置帐户。
答案 2 :(得分:1)
我认为你不会得到任何系统警报(我不确定,但基于Twitter经验)。虽然我在最近的一些博客/网站帖子中看过它,但它也不适合我。我建议在这种情况下你应该要求用户的FB凭证(自定义对话框或FBDialog)并在iPad中添加FB帐户。以下代码未经过测试,但您可以了解一下。我正在为Twitter&做类似的事情这在我的应用程序中工作正常。
ACAccountStore *store = [[ACAccountStore alloc] init] ;
ACAccountType *fbType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[store requestAccessToAccountsWithType:fbType options:[NSDictionary dictionaryWithObjectsAndKeys:kAppID,ACFacebookAppIdKey, nil] completion:^(BOOL granted, NSError *error) {
if(YES) {
ACAccount *fbAccount = [[ACAccount alloc] initWithAccountType:[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]];
ACAccountCredential *outhCredential = [[ACAccountCredential alloc] initWithOAuth2Token:[appDelegate.facebook accessToken] refreshToken:refesrhToken expiryDate:[appDelegate.facebook expirationDate]];
fbAccount.credential = outhCredential;
[store saveAccount:fbAccount withCompletionHandler:^(BOOL success, NSError *error) {
if(success)
{
[self performSelectorOnMainThread:@selector(showFBPostSheet) withObject:nil waitUntilDone:NO];
}
}];
[outhCredential release];
[fbAccount release];
[store release];
}
// Handle any error state here as you wish
}];