2014-04-01 150 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