从iOS中的Twitter获取用户个人资料详细信息(尤其是电子邮件地址)

问题描述 投票:3回答:5

我的目标是根据他/她的Twitter帐户获取用户的详细信息。首先,让我解释一下我想做什么。

在我的情况下,将向用户显示使用Twitter帐户注册的选项。因此,基于用户的Twitter帐户,我希望能够获取用户详细信息(例如电子邮件ID,姓名,个人资料图片,出生日期,性别等)并将这些详细信息保存在数据库中。现在,很多人可能会建议我使用ACAccountACAccountStore,这是一个提供访问,操作和存储帐户界面的类。但在我的情况下,即使用户尚未在iOS设置应用中为Twitter配置帐户,我也想要注册用户。我希望用户导航到Twitter的登录屏幕(在Safari或App本身,或使用任何其他替代方案)。

我还提到了具有API列表here的Twitter文档。但我很困惑应该如何向用户提供登录屏幕以登录Twitter帐户以及如何获取个人资料信息。我应该使用UIWebView,还是将用户重定向到Safari或采用其他方式?

ios objective-c twitter twitter-oauth acaccountstore
5个回答
3
投票

Twitter为此提供了一个漂亮的框架,您只需将其集成到您的应用程序中即可。

https://dev.twitter.com/twitter-kit/ios

它有一个简单的登录方法: -

// Objective-C
TWTRLogInButton* logInButton =  [TWTRLogInButton
                                     buttonWithLogInCompletion:
                                     ^(TWTRSession* session, NSError* error) {
    if (session) {
         NSLog(@"signed in as %@", [session userName]);
    } else {
         NSLog(@"error: %@", [error localizedDescription]);
    }
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];

这是获取用户个人资料信息的过程: -

/* Get user info */
        [[[Twitter sharedInstance] APIClient] loadUserWithID:[session userID]
                                                  completion:^(TWTRUser *user,
                                                               NSError *error)
        {
            // handle the response or error
            if (![error isEqual:nil]) {
                NSLog(@"Twitter info   -> user = %@ ",user);
                NSString *urlString = [[NSString alloc]initWithString:user.profileImageLargeURL];
                NSURL *url = [[NSURL alloc]initWithString:urlString];
                NSData *pullTwitterPP = [[NSData alloc]initWithContentsOfURL:url];

                UIImage *profImage = [UIImage imageWithData:pullTwitterPP];


            } else {
                NSLog(@"Twitter error getting profile : %@", [error localizedDescription]);
            }
        }];

我认为您可以从Twitter Kit Tutorial中找到休息,它还允许通过调用TwitterAuthClient#requestEmail方法请求用户的电子邮件,传入有效的TwitterSession和Callback。


5
投票

最后,在与[email protected]进行了长时间的对话之后,我将我的应用程序列入白名单。这是故事:

  • 发送邮件给[email protected],其中包含有关您的应用程序的一些详细信息,如消费者密钥,应用程序的App Store链接,隐私政策链接,元数据,如何登录我们的应用程序的说明。在邮件中提到您要访问应用程序内的用户电子邮件地址。
  • 他们将审核您的应用程序并在2-3个工作日内回复您。
  • 一旦他们说您的应用程序被列入白名单,请在Twitter开发人员门户中更新您的应用程序设置。登录apps.twitter.com并: 在“设置”标签上,添加服务条款和隐私权政策网址 在“权限”标签上,将令牌的范围更改为请求电子邮件。只有在您的应用程序列入白名单后,才会看到此选项。

Put your hands on code:

同意Vizllx的声明:“Twitter为此提供了一个漂亮的框架,你只需要将它集成到你的应用程序中。”

获取用户邮箱地址

-(void)requestUserEmail
    {
        if ([[Twitter sharedInstance] session]) {

            TWTRShareEmailViewController *shareEmailViewController =
            [[TWTRShareEmailViewController alloc]
             initWithCompletion:^(NSString *email, NSError *error) {
                 NSLog(@"Email %@ | Error: %@", email, error);
             }];

            [self presentViewController:shareEmailViewController
                               animated:YES
                             completion:nil];
        } else {
            // Handle user not signed in (e.g. attempt to log in or show an alert)
        }
    }

获取用户资料

-(void)usersShow:(NSString *)userID
{
    NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/users/show.json";
    NSDictionary *params = @{@"user_id": userID};

    NSError *clientError;
    NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
                             URLRequestWithMethod:@"GET"
                             URL:statusesShowEndpoint
                             parameters:params
                             error:&clientError];

    if (request) {
        [[[Twitter sharedInstance] APIClient]
         sendTwitterRequest:request
         completion:^(NSURLResponse *response,
                      NSData *data,
                      NSError *connectionError) {
             if (data) {
                 // handle the response data e.g.
                 NSError *jsonError;
                 NSDictionary *json = [NSJSONSerialization
                                       JSONObjectWithData:data
                                       options:0
                                       error:&jsonError];
                 NSLog(@"%@",[json description]);
             }
             else {
                 NSLog(@"Error code: %ld | Error description: %@", (long)[connectionError code], [connectionError localizedDescription]);
             }
         }];
    }
    else {
        NSLog(@"Error: %@", clientError);
    }
}

希望能帮助到你 !!!


4
投票

如何在Twitter中获取电子邮件ID?

第1步:到达https://apps.twitter.com/app/

第2步:点击您的应用>点击权限标签。

第3步:在这里查看邮箱

enter image description here


3
投票

在Twitter中,您只能获得user_nameuser_id。这是非常安全,你不能取email idbirth dategender等..,与Facebook相比,Twitter对提供数据非常保密。

需要参考:link1


1
投票

在Swift 4.2和Xcode 10.1中

它也收到了电子邮件。

import TwitterKit 


@IBAction func onClickTwitterSignin(_ sender: UIButton) {

    TWTRTwitter.sharedInstance().logIn { (session, error) in
    if (session != nil) {
        let name = session?.userName ?? ""
        print(name)
        print(session?.userID  ?? "")
        print(session?.authToken  ?? "")
        print(session?.authTokenSecret  ?? "")
        let client = TWTRAPIClient.withCurrentUser()
        client.requestEmail { email, error in
            if (email != nil) {
                let recivedEmailID = email ?? ""
                print(recivedEmailID)
            }else {
                print("error--: \(String(describing: error?.localizedDescription))");
            }
        }
            //To get profile image url and screen name
            let twitterClient = TWTRAPIClient(userID: session?.userID)
                twitterClient.loadUser(withID: session?.userID ?? "") {(user, error) in
                print(user?.profileImageURL ?? "")
                print(user?.profileImageLargeURL ?? "")
                print(user?.screenName ?? "")
            }
        let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as!   SecondViewController
        self.navigationController?.pushViewController(storyboard, animated: true)
    }else {
        print("error: \(String(describing: error?.localizedDescription))");
    }
    }
}

按照Harshil Kotecha的回答。

第1步:到达https://apps.twitter.com/app/

第2步:点击您的应用>点击权限标签。

第3步:在这里查看邮箱

enter image description here

如果要退出

let store = TWTRTwitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
    print(store.session()?.userID ?? "")
    store.logOutUserID(userID)
    print(store.session()?.userID ?? "")
    self.navigationController?.popToRootViewController(animated: true)
}
© www.soinside.com 2019 - 2024. All rights reserved.