请注意,您应该登录您的 iOS 设备以使用此功能:
- (void)signIniwthTwitter
{
if ([TWTweetComposeViewController canSendTweet])
{
// Set up the built-in twitter composition view controller.
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Create the completion handler block.
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
[self dismissModalViewControllerAnimated:YES];
}];
// Present the tweet composition view controller modally.
[self presentModalViewController:tweetViewController animated:YES];
}
else
{
[self getTwitterAccountDetails];
}
}
- (void) getTwitterAccountDetails
{
[DejalBezelActivityView activityViewForView:self.navigationController.navigationBar.superview];
self.view.userInteractionEnabled = NO;
self.connectionstatusLabel.text = @"Getting user details....";
// 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) {
#pragma unused (error)
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"Account details %@",twitterAccount);
_userid = [[twitterAccount valueForKey:@"properties"] valueForKey:@"user_id"];
_screenName = [twitterAccount valueForKey:@"username"];
NSLog(@"user id %@",_userid);
[self getProfileDetailsFromTwitter];
}
}
}];
}
- (void) getProfileDetailsFromTwitter
{
self.connectionstatusLabel.text = @"Getting user profile details....";
NSURL *twitterURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/users/show.json?user_id=%@&include_entities=true",_userid]];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:twitterURL parameters:nil requestMethod:TWRequestMethodGET];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode] == 200) {
// Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization.
NSError *jsonParsingError = nil;
NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
NSLog(@"Twiiter Profile Deatils %@",publicTimeline);
_twitterUserProfileDetails = [[MobileYakUser alloc]init];
_twitterUserProfileDetails.firstName = [publicTimeline objectForKey:@"name"];
_twitterUserProfileDetails.lastName = [publicTimeline objectForKey:@"name"];
output = [NSString stringWithFormat:@"HTTP response status: %i\nPublic timeline:\n%@", [urlResponse statusCode], publicTimeline];
NSURL *url =
[NSURL URLWithString:@"http://api.twitter.com/1/users/profile_image/"];
NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
[params setValue:_screenName forKey:@"screen_name"];
[params setValue:@"original" forKey:@"size"];
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:
^(NSData *imageresponseData, NSHTTPURLResponse *imageFetchurlResponse, NSError *imageerror) {
#pragma unused (imageFetchurlResponse,imageerror)
if (imageresponseData) {
self.connectionstatusLabel.text = @"Getting user profile image....";
UIImage *image = [UIImage imageWithData:imageresponseData];
_twitterUserProfileDetails.profileImage = image;
self.connectionstatusLabel.text = @"Please fill up following fields for login";
self.view.userInteractionEnabled = YES;
[DejalActivityView removeView];
}
}];
}
else {
output = [NSString stringWithFormat:@"HTTP response status: %i\n", [urlResponse statusCode]];
}
}];
}