gpt4 book ai didi

ios - 在 iOS 中访问用户的 Twitter 帐户

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:41 27 4
gpt4 key购买 nike

以下代码应该请求访问权限,然后检索用户的 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 帐户时,应用程序会在用户允许访问后崩溃。我不知道我的代码问题出在哪里。比较难,因为在模拟器上运行的很好,但是不能下载推特在模拟器上放账号。

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

最佳答案

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

enter image description here enter image description here

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

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

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

那是 twitter 正确返回数据,我 NSlogged 屏幕名称,我知道了。

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

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

关于ios - 在 iOS 中访问用户的 Twitter 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20852000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com