简体   繁体   中英

Ask for user Twitter mail with TWTRShareEmailViewController (Fabric Twitter SDK) in Swift

I'd like to request the user's Twitter mail. On https://dev.twitter.com/twitter-kit/ios/request-email we can see the code in Obj-C but I need it in Swift and I can't translate it. Does anyone know how, please?

Here's the Obj-C code:

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)
}

Thanks for the help.

This should roughly translate the above Objective-C to 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")
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM