简体   繁体   中英

Not able to send a Tweet with TwitterKit on iOS 11

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!

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM