在Swift中使用TWTRShareEmailViewController(Fabric Twitter SDK)询问用户的Twitter邮件

时间:2015-02-23 17:27:52

标签: ios email swift twitter twitter-fabric

我想请求用户的Twitter邮件。在https://dev.twitter.com/twitter-kit/ios/request-email我们可以看到Obj-C中的代码,但我需要它在Swift中,我无法翻译它。请问有谁知道吗?

这是Obj-C代码:

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 {
  // TODO: Handle user not signed in (e.g.
  // attempt to log in or show an alert)
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

这应该粗略地将上面的Objective-C翻译成Swift:

if (Twitter.sharedInstance().session() != nil) {
        if let shareEmailViewController = TWTRShareEmailViewController(completion: {
            (email: String!, error: NSError!) in
            if (email != nil) {
                print("\(email)")
            } else {
                print("\(error)")
            }
        }) {
            self.presentViewController(shareEmailViewController, animated: true, completion: nil)
        }
    } else {
        print("User not logged in")
    }