访问用户';iOS中的Twitter帐户

访问用户';iOS中的Twitter帐户,ios,iphone,twitter,ios7,Ios,Iphone,Twitter,Ios7,下面的代码应该请求访问,然后检索用户的twitter帐户,以便将该帐户与iOS应用程序同步。下面是我尝试做的代码 - (IBAction)connectToTwitter:(id)sender { // borrowed from: http://eflorenzano.com/blog/2012/04/18/using-twitter-ios5-integration-single-sign-on/ ACAccountStore *store = [[ACAccountStore a

下面的代码应该请求访问,然后检索用户的twitter帐户,以便将该帐户与iOS应用程序同步。下面是我尝试做的代码

- (IBAction)connectToTwitter:(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]);
}
}];
}];
}
}
}];
}
目前,该代码在模拟器中工作,因为它检测到没有要同步的
Twitter
帐户。但是,在实际设备上,当Twitter帐户登录到手机上的Twitter应用程序时,该应用程序在用户允许访问后崩溃。我不知道我的代码哪里出了问题。这是相当困难的,因为它在模拟器中运行良好,但您无法下载Twitter以在模拟器上放置帐户


首先,我的代码有什么问题,如何修复?第二,我如何在模拟器中测试这一点,这样我就不必每次进行简单更改并想测试时都将应用程序重新下载到我的设备上?

iOS默认检查iOS设置中的Twitter帐户,而不是通过Twitter应用程序。在那里检查

您可以添加一个异常断点来找出崩溃的位置。在这里检查我的答案,找到坠机地点。

编辑:好的,我只是把你的代码放进去,自己测试一下。您的代码中的所有内容都正常工作,我能够检索到这一行的信息

NSString *screenName = [resp objectForKey:@"screen_name"];
self.twitterHandle = screenName;
也就是说twitter正确地返回了数据,我记录了屏幕名,我得到了它

但是verifyCredentials服务只是一个小小的更改,它是一个GET方法,只需将SLRequest-requestMethod更改为SLRequestMethodGET


您已经创建了一个名为PFUser和PFQuery的对象,我无法获取该对象,您可能应该检查是否在设备设置中设置了twitter帐户?尝试在那里设置并检查。您是否尝试查找它崩溃的位置和异常消息??self.twitterHandle是该视图控制器的属性,以便我可以保存它以供以后使用。PFUser和PFQuery是Parse.com解析框架的一部分。我在一个朋友的iPhone上试过,他在手机上登录了Twitter应用程序,所以他的信息不应该已经在“设置”中了吗。尽管他已经登录,但应用程序还是崩溃了。为什么设置中的信息不会与应用中的信息一起更新?这看起来真的很不方便也很烦人。这并不意味着如果他有twitter应用程序,那么他在设置中就有他的凭证。他可以选择不在设置中提供他的twitter凭据。那么,我如何重定向用户以将其信息输入到设置中,以便他们可以输入其信息供我访问?有没有什么方法可以直接从Twitter应用程序而不是通过设置访问它?