以下代码应该要求访问权限,然后检索用户的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以在模拟器上放置帐户。
首先,我的代码有什么问题,我该如何解决?其次,我如何在模拟器中对此进行测试,以便每次进行简单的更改并想要测试时,我都不必将应用程序重新下载到我的设备上?
答案 0 :(得分:2)
iOS默认检查iOS设置中的Twitter帐户,而不是Twitter应用程序。检查那里
您可以添加例外断点以找出崩溃的位置。在这里查看我的答案,找到崩溃位置。 Crash that not able to get
编辑:好的,我只是把你的代码自己测试一下。代码中的所有内容都可以检索到此行的信息。
NSString *screenName = [resp objectForKey:@"screen_name"];
self.twitterHandle = screenName;
这就是twitter正确返回数据并且我记录了屏幕名称,我得到了它。
但只有一个小改动verifyCredentials服务是一个GET方法,只需将SLRequest requestMethod更改为SLRequestMethodGET
你已经创建了一个名为PFUser的对象和PFQuery我无法得到那个可能你应该检查