iOS 5的Twitter框架:Tweeting没有用户input和确认(模态视图控制器)

基本上我想要的是,应用程序,一旦用户允许访问他们的Twitter帐户,能够鸣叫无论用户在UITableViewselect。 理想情况下,我想使用iOS 5中的Twitter框架,但是我遇到的主要问题是用于推文的模式视图控制器。 这是可选的吗? 没有它可以推特,如果没有,你build议我做什么?

谢谢!

如果没有它,推特一定是可以的,下面是生产iOS 5应用程序。 如果用户没有注册帐户,它甚至会将用户带到必要的偏好部分。

 - (void)postToTwitter { // Create an account store object. ACAccountStore *accountStore = [[ACAccountStore alloc] init]; // Create an account type that ensures Twitter accounts are retrieved. ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; // Request access from the user to use their Twitter accounts. [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { if(granted) { // Get the list of Twitter accounts. NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; if ([accountsArray count] > 0) { // Grab the initial Twitter account to tweet from. ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; TWRequest *postRequest = nil; postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:[self stringToPost] forKey:@"status"] requestMethod:TWRequestMethodPOST]; // Set the account used to post the tweet. [postRequest setAccount:twitterAccount]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^(void) { if ([urlResponse statusCode] == 200) { Alert(0, nil, @"Tweet Successful", @"Ok", nil); }else { Alert(0, nil, @"Tweet failed", @"Ok", nil); } }); }]; }); } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]]; } } }]; } 

这将是使用SLRequest而不是TWRequest的更新版本,在iOS 6中已弃用TWRequest。请注意,这需要将Social和Accounts框架添加到您的项目中。

 - (void) postToTwitterInBackground { // Create an account store object. ACAccountStore *accountStore = [[ACAccountStore alloc] init]; // Create an account type that ensures Twitter accounts are retrieved. ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; // Request access from the user to use their Twitter accounts. [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if(granted) { // Get the list of Twitter accounts. NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; if ([accountsArray count] > 0) { // Grab the initial Twitter account to tweet from. ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; SLRequest *postRequest = nil; // Post Text NSDictionary *message = @{@"status": @"Tweeting from my iOS app!"}; // URL NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"]; // Request postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message]; // Set Account postRequest.account = twitterAccount; // Post [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"Twitter HTTP response: %i", [urlResponse statusCode]); }]; } } }]; } 

更新:Twitter中的TwitterKit是相当方便的,如果你的目标是从你的Twitter应用程序发布时,当用户尝试在您的应用程序中鸣叫,那么它可能是一个不错的select考虑。

(是的,这种方法将允许您发布到Twitter没有任何对话框或确认)。

TwitterKit将处理权限部分,并使用TWTRAPIClient通过Twitter rest API执行tweet。

  //Needs to performed once in order to get permissions from the user to post via your twitter app. [[Twitter sharedInstance]logInWithCompletion:^(TWTRSession *session, NSError *error) { //Session details can be obtained here //Get an instance of the TWTRAPIClient from the Twitter shared instance. (This is created using the credentials which was used to initialize twitter, the first time) TWTRAPIClient *client = [[Twitter sharedInstance]APIClient]; //Build the request that you want to launch using the API and the text to be tweeted. NSURLRequest *tweetRequest = [client URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:[NSDictionary dictionaryWithObjectsAndKeys:@"TEXT TO BE TWEETED", @"status", nil] error:&error]; //Perform this whenever you need to perform the tweet (REST API call) [client sendTwitterRequest:tweetRequest completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //Check for the response and update UI according if necessary. }]; }]; 

希望这可以帮助。

接受的答案由于一些变化而不再有效。 这一个适用于iOS 10,Swift 3和Twitter的API 1.1版本。

** 更新 **

这个答案已经更新,因为上一个依赖于不推荐的Twitter端点。

 import Social import Accounts func postToTwitter() { let accountStore = ACAccountStore() let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) accountStore.requestAccessToAccounts(with: accountType, options: nil) { (granted, error) in if granted, let accounts = accountStore.accounts(with: accountType) { // This will default to the first account if they have more than one if let account = accounts.first as? ACAccount { let requestURL = URL(string: "https://api.twitter.com/1.1/statuses/update.json") let parameters = ["status" : "Tweet tweet"] guard let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, url: requestURL, parameters: parameters) else { return } request.account = account request.perform(handler: { (data, response, error) in // Check to see if tweet was successful }) } else { // User does not have an available Twitter account } } } } 

这是正在使用的API 。