1

我按照https://dev.twitter.com/twitterkit/ios/compose-tweets的指示通过我的 iOS 11 的 Object C 应用程序撰写推文

// Objective-C
TWTRComposer *composer = [[TWTRComposer alloc] init];

[composer setText:@"just setting up my Twitter Kit"];
[composer setImage:[UIImage imageNamed:@"twitterkit"]];

// Called from a UIViewController
[composer showFromViewController:self completion:^(TWTRComposerResult result) {
    if (result == TWTRComposerResultCancelled) {
        NSLog(@"Tweet composition cancelled");
    }
    else {
        NSLog(@"Sending Tweet!");
    }
}];

但是,在[连接您的帐户]屏幕上单击连接按钮后,它在没有撰写推文的情况下返回应用程序(Twitter已登录)。似乎 showFromViewController 没有运行。有人有同样的问题吗?

4

1 回答 1

0

首先在 Twitter 上为您的应用程序获得读/写权限。

    if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
  NSString *twitterLoggedInInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"TwitterLoggedInInfo"];

  if (![twitterLoggedInInfo isEqualToString:@"v3"]) {
    NSString *currentUserID = [Twitter sharedInstance].sessionStore.session.userID;
    [[Twitter sharedInstance].sessionStore logOutUserID:currentUserID];

    [[NSUserDefaults standardUserDefaults] setObject:@"v3" forKey:@"TwitterLoggedInInfo"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
      if (session) {
        TWTRComposerViewController *composer = [[TWTRComposerViewController alloc] initWithInitialText:tweetText image:tweetImage videoURL:nil];
        [self presentViewController:composer animated:YES completion:nil];
      }
    }];
  } else {
    if (![twitterLoggedInInfo isEqualToString:@"v3"]) {
      [[NSUserDefaults standardUserDefaults] setObject:@"v3" forKey:@"TwitterLoggedInInfo"];
      [[NSUserDefaults standardUserDefaults] synchronize];
    }

    TWTRComposerViewController *composer = [[TWTRComposerViewController alloc] initWithInitialText:tweetText image:tweetImage videoURL:nil];
    [self presentViewController:composer animated:YES completion:nil];
  }
  } else {
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
      if (session) {
        TWTRComposerViewController *composer = [[TWTRComposerViewController alloc] initWithInitialText:tweetText image:tweetImage videoURL:nil];
        [self presentViewController:composer animated:YES completion:nil];
      }
    }];
  }

TwitterLoggedInInfo键检查您是否存在只读权限用户,我的意思是使用此键是可选的。

于 2017-09-29T16:28:35.743 回答