【问题标题】:Accessing User's Twitter Account in iOS在 iOS 中访问用户的 Twitter 帐户
【发布时间】:2014-01-18 01:46:48
【问题描述】:

以下代码应该请求访问,然后检索用户的 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 将帐户放在模拟器上。

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

【问题讨论】:

  • 你在设备设置中设置了twitter账号吗?尝试在那里设置并检查。
  • 您是否尝试过查找崩溃和异常消息的位置??

标签: ios iphone twitter ios7


【解决方案1】:

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

您可以添加一个异常断点来找出崩溃的位置。在此处查看我的答案以找到崩溃位置。 Crash that not able to get

编辑:好的,我只是把你的代码和测试自己。您代码中的所有内容都有效,我能够检索到这一行的信息。

NSString *screenName = [resp objectForKey:@"screen_name"];
self.twitterHandle = screenName;

那是 twitter 正确返回数据,我 NSlogged 屏幕名称,我得到了它。

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

您已经创建了一些名为 PFUser 和 PFQuery 的对象,我无法得到它,可能您应该检查一下

【讨论】:

  • self.twitterHandle 是该视图控制器的一个属性,因此我可以保存它以供以后使用。 PFUser 和 PFQuery 是 Parse.com 上 Parse 框架的一部分。我在朋友的 iPhone 上试过这个,他在手机上登录了 Twitter 应用程序,所以他的信息不应该已经在“设置”中。即使他已经登录,应用程序也崩溃了。为什么设置中的信息不会随着应用程序中的信息更新?这似乎真的很不方便和烦人。
  • 这并不意味着如果他有 twitter 应用程序,那么他在设置中就有他的凭据。他可以选择不在设置中提供他的推特凭据
  • 那么我如何重定向用户以将他们的信息输入到设置中,以便他们可以输入他们的信息供我访问?有什么方法可以让我直接从 Twitter 应用程序而不是设置中访问它?
猜你喜欢
  • 2012-06-27
  • 2015-07-30
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2012-09-19
  • 2013-03-30
  • 2023-02-15
  • 2019-12-18
相关资源
最近更新 更多