Search code examples
iosobjective-ctwitterslcomposeviewcontroller

SLComposeViewController: Different behavior of "No Twitter Accounts" alert depending on whether or not Twitter app is installed


I use the following code for Twitter sharing in my app:

SLComposeViewController *twitter = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitter setInitialText:@"some text"];

twitter.completionHandler = ^(SLComposeViewControllerResult result)
{
    // do something...
    [topViewController dismissViewControllerAnimated:YES completion:nil];
};
[topViewController presentViewController:twitter animated:YES completion:nil];

Now, if there are no Twitter accounts set up and if the Twitter app is not installed, I get the following alert:

enter image description here

It does exactly what it should, pressing "Cancel" closes both the alert view and the Twitter compose view. Pressing "Settings" also closes both views and opens the settings. Nice.

Now, if there are no Twitter accounts set up, but the Twitter app is installed, I get the following alert:

enter image description here

Note that there is no "Cancel" and no "Settings" button, just an "OK" button. If pressed, the alert view disappears, but the Twitter compose view stays there with the "Post" button grayed out. So the user is left to press "Cancel" again to close the compose view and to go to the settings herself. Not so nice.

Please note that it doesn't make a difference whether or not I check for service availability before presenting the SLComposeViewController:

[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];

Do you know, why there is this difference in behavior and if there is a way to get the same behavior, no matter whether or not the Twitter app is installed?

Tested on iOS 9.2.1, iPad Air (2nd gen) & iPhone 6 Plus.


Solution

  • Check [this] (Opening the Settings app from another app) out. Scroll to the bottom and there is an answer that hasn't been upvoted (bizarrely) that contains images. The link there shows you how to navigate to settings/twitter form your app. The next step is to look into using ACAccountStore, checking if there are no users logged in via an array.

    let accountStore = ACAccountStore()
    let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
    
    accountStore.requestAccessToAccountsWithType(accountType, options: nil, completion: { (granted, error) in
    if (granted) {
             let arrayOfAccts = accountStore.accountsWithAccountType(accountType)
             if (arrayOfAccts.count > 0) {
             //regular function of twitter
             }
             //else is when there are no accounts logged into twitter
             else {
             //bring up a custom UIAlertAction with two options; Cancel and Settings.
             //settings should use the openURL to prefs:root=TWITTER
             } 
    }
    

    Which will mimic the way it works when the user is not signed in to twitter and the app is not installed giving you Cancel and Settings.

    As to why you get different alert settings is beyond me, it's probably an iOS apple thing.

    This line:

    [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];
    

    Checks to see if the Twitter app is installed. Hope this helps.