首先在 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键检查您是否存在只读权限用户,我的意思是使用此键是可选的。