Twitter Kit SDK iOS. Can't tweet from app. Returns to app after "Connect your account" page

ios

#1

I previously used iOS’s built in Social framework and now am trying to migrate my app to Twitter 3. My app is running iOS11 and written in Swift. I have tried following all the instructions online here: https://dev.twitter.com/twitterkit/ios/migrate-social-framework

When I run some basic things:

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

even though I am already logged in to my account on the Twitter app, it doesn’t seem to be connecting properly. The “Connect your account” page comes up. When I press the Connect button on this page, it just sends me back to my app. No Tweet composer shows up. If I do it again, same thing. This problem was brought up here as well, but no specific solution was given. https://stackoverflow.com/questions/46094360/cannot-compose-tweets-with-twitter-kit-3

What might cause this? I have made sure that my application on Twitter allows read and write permissions.


#2

Did you set URL Schemes for twitter in your application https://dev.twitter.com/twitterkit/ios/installation#url-scheme
and implemented codes to handle login redirect https://dev.twitter.com/twitterkit/ios/log-in-with-twitter#handling-log-in-redirect ?

Composing a tweet needs logged in session, and those processes are needed to make your app’s session logged in.


#4

Thanks so much for the reply.

I have this in my plist:

CFBundleURLTypes

<dict>

    <key>CFBundleURLSchemes</key>

    <array>

        <string>fbxxxxxxx</string>

        <string>twitterkit-xxxxxxxxx</string>

    </array>

</dict>
and

this in my app delegate

func application(_ application: UIApplication, open url: URL,
sourceApplication: String?, annotation: Any, options: [
UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { // added this

if Twitter.sharedInstance().application(application, open: url,

options: options) {

    return true

}

return IsReportingFBEvents ? FBSDKApplicationDelegate.sharedInstance

().application(application, open: url, sourceApplication:
sourceApplication, annotation: annotation) : true

}
I’m sure I’m just missing something idiotic – but sooo frustrating :slight_smile:


#5

The original poster of the SO question you referenced does answer his own question with a link to a Japanese language page. The solution there separates authentication into its own function which worked for me.

  • (void)tweetMain
    {
    // Twitter 認証トークンのチェック
    if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) { // 認証トークンあり
    [self tweet];

} else { // 認証トークンなし
// Twitterログイン
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session) { // ログイン OK
NSLog(@“signed in as %@”, [session userName]);

[self tweet];

} else { // ログイン キャンセル
NSLog(@“error: %@”, [error localizedDescription]);
}
}];
}
}

  • (void)tweet
    {
    // Twitter投稿データ編集
    TWTRComposer *composer = [[TWTRComposer alloc] init];
    [composer setText:@“hoge”];
    [composer setImage:[UIImage imageNamed:@“twitterkit”]];
    [composer setURL:[NSURL URLWithString:@“http://yahoo.co.jp”]];

// Twitter投稿画面表示
[composer showFromViewController:self completion:^(TWTRComposerResult result) {
if (result == TWTRComposerResultCancelled) { // キャンセル
NSLog(@“Tweet composition cancelled”);
}
else {
NSLog(@“Sending Tweet!”);
}
}];
}