iOS 5 Twitter框架:没有用户输入和确认的推文(模态视图控制器)

soo*_*per 6 twitter cocoa-touch ios

基本上我想要的是应用程序,一旦用户允许访问他们的Twitter帐户,就能够发布用户在a中选择的任何内容UITableView.理想情况下,我想在iOS 5中使用Twitter框架,但我遇到的主要问题是用于推文的模态视图控制器.这是可选的吗?是否可以在没有它的情况下发推文,如果没有,你建议我做什么?

谢谢!

jam*_*ack 11

如果没有它,绝对可以推文,以下是生产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"]];
            }
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)


jes*_*.tt 6

这将是使用SLRequest而不是TWRequest的更新版本,在iOS 6中已弃用.请注意,这需要将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]);
                 }];

            }
        }
    }];

}
Run Code Online (Sandbox Code Playgroud)


Vij*_*adi 5

更新:Twitter中的TwitterKit是非常方便的,如果您的目标是在用户尝试在您的应用中发推文时从您的Twitter应用发布,那么它可能是一个很好的选择.

(是的,此方法允许您在没有任何对话框或确认的情况下发布到Twitter).

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

 //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.            
   }];
}];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.