15
votes

I am using the new Facebook integration in iOS6 like the following:

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");
}

The problem here is, I am testing it without being logged into Facebook and all I get is the log message.

** Strange thing is, I get the dialog in the Simulator, but NOT the device!**

How can I show the the user an alert that tells them that they need to log in to Facebook? I have seen screenshots of a system alert, but I am not getting that for some reason. What have I done wrong?

3

3 Answers

28
votes

Removing the check for [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] fixed the issue for me.

7
votes

It seems that [SLComposeViewController isAvailableForServiceType:] returns true in the simulator, even if you haven't set up an account there.

1
votes

I don't think you will get any system alert (I am not sure but based on Twitter experience). Although I have seen it in some recent blogs/web posts but it is not working for me too. I suggest in such case you should rather ask for user's FB credentials (custom dialog or FBDialog) and add FB account in iPad. The below code is not tested but you can get an idea. I am doing similar thing for Twitter & that is working fine in my apps.

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
    }];