2015-06-08 90 views
-1

我希望用戶能夠通過Twitter登錄而不是填寫註冊表單,但是當用戶點擊「通過Twitter登錄」時,我想從Twitter獲取所需的基本信息API。通過twitter登錄ios7

+0

什麼問題? – Hazok

+0

笏你的意思wats問題 –

+0

不幸的是,我們不能爲你寫代碼。 – wkcamp

回答

0

雖然我不會爲你寫的代碼,你可以踏上你的旅程在這裏: https://dev.twitter.com/twitter-kit/ios/twitter-login

Twitter提供使用其API的一步一步的方式。如果你有任何問題,那麼你可以隨時在這裏問我們。

+0

謝謝你威爾坎貝爾 –

+0

是啊,如果你需要任何其他的API,這一切都在這裏:https://dev.twitter.com/overview/documentation – wkcamp

1

你可以使用默認的社會框架,

  • 加入社會工作框架到您的項目。
  • 然後爲您的代碼文件添加標頭Accounts/Accounts.h & Social/Social.h標頭。

  • 然後加入下面的方法&後,在需要時調用它。

代碼:

- (void) getInfo 
    { 
     ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
     ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

     [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){ 
      if (granted) { 

      NSArray *accounts = [accountStore accountsWithAccountType:accountType]; 

      // Check if the users has setup at least one Twitter account 

      if (accounts.count > 0) 
      { 
       ACAccount *twitterAccount = [accounts objectAtIndex:0]; 

       // Creating a request to get the info about a user on Twitter 

       SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"] parameters:[NSDictionary dictionaryWithObject:username forKey:@"screen_name"]]; 
       [twitterInfoRequest setAccount:twitterAccount]; 

       // Making the request 

       [twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
        dispatch_async(dispatch_get_main_queue(), ^{ 

         // Check if we reached the reate limit 

         if ([urlResponse statusCode] == 429) { 
          NSLog(@"Rate limit reached"); 
          return; 
         } 

         // Check if there was an error 

         if (error) { 
          NSLog(@"Error: %@", error.localizedDescription); 
          return; 
         } 

         // Check if there is some response data 

         if (responseData) { 

          NSError *error = nil; 
          NSArray *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; 


          // Filter the preferred data 

          NSString *screen_name = [(NSDictionary *)TWData objectForKey:@"screen_name"]; 
          NSString *name = [(NSDictionary *)TWData objectForKey:@"name"]; 

          int followers = [[(NSDictionary *)TWData objectForKey:@"followers_count"] integerValue]; 
          int following = [[(NSDictionary *)TWData objectForKey:@"friends_count"] integerValue]; 
          int tweets = [[(NSDictionary *)TWData objectForKey:@"statuses_count"] integerValue]; 

          NSString *profileImageStringURL = [(NSDictionary *)TWData objectForKey:@"profile_image_url_https"]; 
          NSString *bannerImageStringURL =[(NSDictionary *)TWData objectForKey:@"profile_banner_url"]; 


          // Update the interface with the loaded data 

          nameLabel.text = name; 
          usernameLabel.text= [NSString stringWithFormat:@"@%@",screen_name]; 

          tweetsLabel.text = [NSString stringWithFormat:@"%i", tweets]; 
          followingLabel.text= [NSString stringWithFormat:@"%i", following]; 
          followersLabel.text = [NSString stringWithFormat:@"%i", followers]; 

          NSString *lastTweet = [[(NSDictionary *)TWData objectForKey:@"status"] objectForKey:@"text"]; 
          lastTweetTextView.text= lastTweet; 



          // Get the profile image in the original resolution 

          profileImageStringURL = [profileImageStringURL stringByReplacingOccurrencesOfString:@"_normal" withString:@""]; 
          [self getProfileImageForURLString:profileImageStringURL]; 


          // Get the banner image, if the user has one 

          if (bannerImageStringURL) { 
           NSString *bannerURLString = [NSString stringWithFormat:@"%@/mobile_retina", bannerImageStringURL]; 
           [self getBannerImageForURLString:bannerURLString]; 
          } else { 
           bannerImageView.backgroundColor = [UIColor underPageBackgroundColor]; 
          } 
         } 
        }); 
       }]; 
      } 
     } else { 
      NSLog(@"No access granted"); 
     } 
    }]; 
} 
+0

非常感謝你mucj先生尼勒什帕特爾。你從哪裏來?? –

+0

印度,它在你的最後? –

+0

實際上我想通過twitter登錄。你讓我? –

1

你可以演示代碼

https://github.com/nst/STTwitter 

如果您使用的吊艙然後在POD文件中使用寫 「STTwitter」,並更新你吊艙。

+0

我不使用pod –

+0

你可以下載sdk手動並在代碼中使用 – kb920