2014-04-01 151 views
0

它已经两个星期了,我找不到有用的答案为我的查询,我试了很多代码没有为我工作,其实我试图建立一个日志在用户可以从他们的Facebook或Twitter登录/登录的页面中,它完全适用于Facebook,但与Twitter有关。登录/登录通过twitter iOS7

我试过下面这段代码,请指教和帮助,因为我仍然得到一个错误,错误的是(对类型的对象“视图控制器”找不到属性twitterHandle)

这里是我已经使用的代码。请注意,我使用为我的数据库解析

*我在叫Twitter的

-(IBAction)Twitter:(id)sender; 

*头创建的动作按钮,我不得不在M文件的代码如下显示:

- (IBAction)Twitter:(id)sender { 
{ 
    // borrowed from: http://eflorenzano.com/blog/2012/04/18/using-twitter-ios5-integration-single-sign-on/ 
    ACAccountStore *store = [[ACAccountStore alloc] init]; 
    ACAccountType *twitterType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [store requestAccessToAccountsWithType:twitterType options:nil completion:^(BOOL granted, NSError *error) 
    { 
     if(granted) { 
      // Access has been granted, now we can access the accounts 
      // Remember that twitterType was instantiated above 
      NSArray *twitterAccounts = [store accountsWithAccountType:twitterType]; 

      // If there are no accounts, we need to pop up an alert 
      if(twitterAccounts != nil && [twitterAccounts count] == 0) 
      { 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Twitter Accounts" 
                   message:@"There are no Twitter accounts configured. You must add or create a Twitter separately." 
                   delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
       [alert show]; 
      } else { 
       ACAccount *account = [twitterAccounts objectAtIndex:0]; 
       // Do something with their Twitter account 
       NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/account/verify_credentials.json"]; 
       SLRequest *req = [SLRequest requestForServiceType:SLServiceTypeTwitter 
                requestMethod:SLRequestMethodPOST 
                   URL:url 
                 parameters:nil]; 
       // Important: attach the user's Twitter ACAccount object to the request 
       req.account = account; 
       [req performRequestWithHandler:^(NSData *responseData, 
               NSHTTPURLResponse *urlResponse, 
               NSError *error) 
       { 
        // If there was an error making the request, display a message to the user 
        if(error != nil) { 
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter Error" 
                     message:@"There was an error talking to Twitter. Please try again later." 
                     delegate:nil 
                   cancelButtonTitle:@"OK" 
                   otherButtonTitles:nil]; 
         [alert show]; 
         return; 
        } 
        // Parse the JSON response 
        NSError *jsonError = nil; 
        id resp = [NSJSONSerialization JSONObjectWithData:responseData 
                   options:0 
                   error:&jsonError]; 
        // If there was an error decoding the JSON, display a message to the user 
        if(jsonError != nil) 
        { 
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter Error" 
                     message:@"Twitter is not acting properly right now. Please try again later." 
                     delegate:nil 
                   cancelButtonTitle:@"OK" 
                   otherButtonTitles:nil]; 
         [alert show]; 
         return; 
        } 

        NSString *screenName = [resp objectForKey:@"screen_name"]; 
        self.twitterHandle = screenName; 
        PFUser *currentUser = [PFUser currentUser]; 
        PFQuery *query = [PFQuery queryWithClassName:@"_User"]; 
        [query whereKey:@"username" equalTo:currentUser.username]; 
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
         if (!error) { 
          // Do something with the found objects 
          for (PFObject *object in objects) 
          { 
           object[@"TwitterHandle"] = self.twitterHandle; 
           [object saveInBackground]; 
          } 
         } else { 
          // Log details of the failure 
          NSLog(@"Error: %@ %@", error, [error userInfo]); 
         } 
        }]; 
       }]; 
      } 
     } 
    }]; 
} 
    } 

请请请帮助: (

回答

1
(property twitterHandle not found on object of type "ViewController") 

告诉你某处你试图访问一个名为的房产对ViewController类型的对象没有此类属性。

我怀疑的问题是在这条线:

self.twitterHandle = screenName; 

你只需要一个属性添加到您的视图控制器接口是这样的:

@property (nonatomic, strong) NSString *twitterHandle; 
+0

我在运行时出现此错误**终止应用程序,由于未捕获异常'NSInvalidArgumentException',原因:' - [ViewController TwitterButton:]:无法识别的选择器发送到实例** – Lolieta

+0

嗨!如果您的第一个问题得到解答,请标记答案并创建一个新问题,如果您有新问题。 – Dima