8

I am trying to use the TwitterKit to compose a tweet, via the TWTRComposer class provided. This is the function I call:

-(void) tweet:(UIViewController *) root {
    TWTRComposer *composer = [[TWTRComposer alloc] init];

    [composer setText:@"just setting up my Twitter Kit"];

    // Called from a UIViewController
    [composer showFromViewController:root completion:^(TWTRComposerResult result) {
        if (result == TWTRComposerResultCancelled) {
            NSLog(@"Tweet composition cancelled");
        }
        else {
            NSLog(@"Sending Tweet!");
        }
    }];
}

There are two problems with this:

  1. When the Twitter app is installed on my device, the TWTRComposer dialog is presented, but shortly after, another alert is presented on top, that that has the title: "No Twitter Account", and the following message: "There are no Twitter accounts configured. You can add or create a Twitter account in Settings." I can dismiss this dialog, and then send the tweet (successfully), but this is obviously very bad UX. Also, this error dialog is strange, as iOS 11 doesn't have Twitter in Settings anymore.
  2. When the Twitter app is not installed on my device, the TWTRComposer dialog is not presented at all. Instead, the completion block in the showFromViewController method is called immediately, with a result of type TWTRComposerResultCancelled.

I have a feeling this may be somehow related to login issues with Twitter. as The app I'm working on does not include Signup/Login with Twitter. However, I was under the impression the TWTRComposer handles all of the logging in.

Any help is truly appreciated, and thank you for reading!

Miki P
  • 652
  • 1
  • 9
  • 22

2 Answers2

7

You are correct: due to the changes in iOS 11, you need to login before calling TWTRComposer.

iOS 11 no longer supports using Twitter through the built-in social framework. Instead, you can use Twitter Kit 3 to Tweet, log in users, and use the Twitter API. The following guide shows how to migrate your old code.

Login (with the following order, if possible, Twitter for iOS / SFSafariViewController / UIWebView. Check prerequisites) and then compose:

ObjC:

// Check if current session has users logged in
if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
    TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
    [fromController presentViewController:composer animated:YES completion:nil];
} else {
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
        if (session) {
            TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
            [fromController presentViewController:composer animated:YES completion:nil];
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No Twitter Accounts Available" message:@"You must log in before presenting a composer." preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

Swift:

if Twitter.sharedInstance().sessionStore.hasLoggedInUsers() {
    // App must have at least one logged-in user to compose a Tweet
    let composer = TWTRComposerViewController.emptyComposer()
    present(composer, animated: true, completion: nil)
} else {
    // Log in, and then check again
    Twitter.sharedInstance().logIn { session, error in
        if session != nil { // Log in succeeded
            let composer = TWTRComposerViewController.emptyComposer()
            self.present(composer, animated: true, completion: nil)
        } else {
            let alert = UIAlertController(title: "No Twitter Accounts Available", message: "You must log in before presenting a composer.", preferredStyle: .alert)
            self.present(alert, animated: false, completion: nil)
        }
    }
}

Docs:

nathan
  • 9,329
  • 4
  • 37
  • 51
  • 1
    Thank you for your answer! Just to clarify, in order to use TWTRComposer, it is necessary to call logIn() beforehand? – Miki P Aug 04 '17 at 22:33
  • Check with `Twitter.sharedInstance().sessionStore.hasLoggedInUsers()` if there's a logged in user before presenting it. – nathan Aug 04 '17 at 22:35
  • So would the above logIn method open up the app/Safari, or will I have to take care of that? It's unclear from the documentation. Also, am I required to allow users to sign up/log in to my app through Twitter? Or can users simply sign in to Twitter through my app? – Miki P Aug 04 '17 at 22:41
  • The documented login flow is as follows: Twitter for iOS -> SFSafariViewController -> UIWebView. The choice is up to the library. – nathan Aug 04 '17 at 22:42
  • I see, so the library handles it. Final question (I hope), do I have to enable sign/up login via Twitter into my app for any of this to work? – Miki P Aug 04 '17 at 22:45
  • 3
    Check the prereqs. Weird thing is, the docs specify the following: `TWTRComposer > This class automatically handles presenting a log in controller if there are no logged in sessions.` – nathan Aug 04 '17 at 22:46
  • 1
    I know, that's why I asked this question... It's very strange. – Miki P Aug 04 '17 at 22:48
  • Probably not updated. If you can, send an email to support so they can update it for future users. – nathan Aug 04 '17 at 22:49
  • I've just stumbled across this: https://github.com/marcoarment/FCUtilities/blob/master/FCUtilities/FCTwitterAuthorization.m Twitter auth without Twitter Kit. Uses Twitter iOS app with SFAuthenticationSession as a fallback – nathan Aug 05 '17 at 00:25
  • It seems that the docs are very outdated. Methods like emptyComposer(), and hasLoggedInUsers() don't appear in the header file anymore. However, I made it work with the methods that are available. – Miki P Aug 07 '17 at 20:04
  • @MikiP I have error message saying that `Value of type 'TWTRSessionStore' has no member 'hasLoggedInUsers'`. Could you share how do you work around that? – hientrq Nov 17 '17 at 00:48
  • 1
    @HienQuangTran I've finished working on the app that this question concerns, and haven't done iOS development since then, so my memory in this area isn't great. However, I don't think I encountered the issue you are encountering, so I can't be of much help. Sorry! – Miki P Nov 17 '17 at 01:27
  • @HienQuangTran First check you minimum deployment iOS version. If it's iOS9+ only then you'll be able to install TwitterKit v3 where the `hasLoggedInUsers` present. Otherwise TwitterKit v2.8.1 will be installed. Cheers. – SoftDesigner Jan 22 '18 at 19:47
0

From June 12th 2018 callback locking will no longer be optional. The correct callback format for iOS apps is:

twitterkit-MY_CONSUMER_KEY://

https://developer.twitter.com/en/docs/basics/callback_url.html

shahil
  • 941
  • 9
  • 20