Not able to send a Tweet with TwitterKit on iOS 11

喜你入骨 提交于 2019-12-06 23:00:28

问题


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!


回答1:


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:

  • https://github.com/twitter/twitter-kit-ios/wiki/Migrating-from-iOS-Social-Framework
  • https://github.com/twitter/twitter-kit-ios/wiki/Compose-Tweets



回答2:


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



来源:https://stackoverflow.com/questions/45514354/not-able-to-send-a-tweet-with-twitterkit-on-ios-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!