Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS6 Facebook: What to do if user has not configured Facebook?

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?

like image 226
Nic Hubbard Avatar asked Sep 21 '12 18:09

Nic Hubbard


3 Answers

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

like image 105
Nic Hubbard Avatar answered Oct 22 '22 10:10

Nic Hubbard


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

like image 35
EPage_Ed Avatar answered Oct 22 '22 08:10

EPage_Ed


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
    }];
like image 26
msk Avatar answered Oct 22 '22 09:10

msk