1

任何人如何使用 api 在 IOS 5 中获取 Twitter 直接消息。

4

1 回答 1

-1

使用他们的 id 作为参数向您的关注者发送直接消息:

ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    // Did user allow us access?
    if (granted == YES)
    {
        // Populate array with all available Twitter accounts
        NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

        // Sanity check
        if ([arrayOfAccounts count] > 0) 
        {
            // Keep it simple, use the first account available
            ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

            // Build a twitter request
            NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/direct_messages/new.json"];

            NSMutableDictionary *params;

            for (int i =0 ; i <[friends_SelectedIDArray count]; i++) {

                params = [[NSMutableDictionary alloc] init];
                //[params setObject:@"@meenukatal" forKey:@"screen_name"];
                [params setObject:[friends_SelectedIDArray objectAtIndex:i] forKey:@"user_id"];
                //need to change sendimg message every time do not send same message //

                [params setObject:text_Field.text  forKey:@"text"];

                TWRequest *postRequest = [[TWRequest alloc]
                                          initWithURL:   url
                                          parameters:    params
                                          requestMethod: TWRequestMethodPOST
                                          ];

                // Post the request
                [postRequest setAccount:acct];

                // Block handler to manage the response
                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                    NSLog(@"Response Data\n%@", responseData);

                    if ([urlResponse statusCode] == 200) {

                        UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Message" message:@"You have successfully posted the message! " delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                        [alertmessage show];

                    }

                    else {

                        UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"There might be some problem please try later! " delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                        [alertmessage show];

                    }


                    if (!error)
                        NSLog(@"%@", [error description]);
                }];
            }
        }
    }
}];
于 2012-11-21T09:50:09.057 回答