Not able to send a Tweet with TwitterKit on iOS 11
Asked Answered
D

2

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!

Desiraedesire answered 4/8, 2017 at 19:40 Comment(0)
S
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:

Sequacious answered 4/8, 2017 at 22:21 Comment(13)
Thank you for your answer! Just to clarify, in order to use TWTRComposer, it is necessary to call logIn() beforehand?Desiraedesire
Check with Twitter.sharedInstance().sessionStore.hasLoggedInUsers() if there's a logged in user before presenting it.Sequacious
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?Desiraedesire
The documented login flow is as follows: Twitter for iOS -> SFSafariViewController -> UIWebView. The choice is up to the library.Sequacious
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?Desiraedesire
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.Sequacious
I know, that's why I asked this question... It's very strange.Desiraedesire
Probably not updated. If you can, send an email to support so they can update it for future users.Sequacious
I've just stumbled across this: github.com/marcoarment/FCUtilities/blob/master/FCUtilities/… Twitter auth without Twitter Kit. Uses Twitter iOS app with SFAuthenticationSession as a fallbackSequacious
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.Desiraedesire
@MikiP I have error message saying that Value of type 'TWTRSessionStore' has no member 'hasLoggedInUsers'. Could you share how do you work around that?Ethnogeny
@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!Desiraedesire
@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.Timaru
C
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

Combust answered 21/11, 2018 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.