Failed to send Tweet from TwitterKit on iOS 11

I am trying to use TwitterKit to compose a tweet through the provided TWTRComposer class. 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:

  • When the Twitter application is installed on my device, the TWTRComposer dialog box appears, but soon after it another warning appears that has the name: “No Twitter account” and the following message: “There are no Twitter accounts configured. You can add or create an account Twitter in Settings. " I can reject this dialog and then send a tweet (successfully), but this is obviously very bad UX. In addition, this error dialog is strange, since iOS 11 no longer has Twitter in the settings.
  • When the Twitter application is not installed on my device, the TWTRComposer dialog is not presented at all. Instead, the termination block in the showFromViewController method showFromViewController called immediately, with a result of type TWTRComposerResultCancelled.

I have a feeling that this could be due to problems logging into Twitter from Twitter. since the application I'm working on does not include signing up / logging in using Twitter. However, I had the impression that TWTRComposer was processing all the records.

Any help is really appreciated and thanks for reading!

+5
source share
1 answer

You are right: due to changes in iOS 11 you need to log in before calling TWTRComposer .

iOS 11 no longer supports the use of Twitter through the integrated social framework. Instead, you can use Twitter Kit 3 to read, log in, and use the Twitter API. The following guide shows how to migrate old code.

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

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:

+4
source

Source: https://habr.com/ru/post/1270600/


All Articles