2016-09-18 59 views
6

Xin chào, tôi đang cố truy xuất email của mình từ facebook khi tôi đang phát sdk ios bằng cách sử dụng nhanh chóng. Nền tảng iOS là 10, nhanh chóng 3 và Xcode 8. Tôi đã làm theo hướng dẫn trực tuyến nhưng gặp sự cố khi truy xuất email.Không thể truy xuất email từ facebook bằng cách sử dụng nhanh 3 và ios 10

dưới đây là mã của tôi:

if FBSDKAccessToken.current() == nil { 
      print("I got token") 
      let fbButton = FBSDKLoginButton() 
      fbButton.readPermissions = ["public_profile", "email", "user_friends"] 
      view.addSubview(fbButton) 
      fbButton.center = view.center 
      fbButton.delegate = self 
      self.fetchprofile() 
     } 

     else { 
      print("Dont have token") 
      let loginView : FBSDKLoginButton = FBSDKLoginButton() 
      self.view.addSubview(loginView) 
      loginView.center = self.view.center 
      loginView.readPermissions = ["public_profile", "email", "user_friends"] 
      loginView.delegate = self 
     } 

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 
     if error != nil { 
      print(error.localizedDescription) 
      return 
     } 
     print("I'm in") 
     fetchprofile() 
    } 

func fetchprofile() { 

     print("Getting profile") 

     let parameters = ["fields": "email"] 

     let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters, httpMethod: "GET") 

     graphRequest.start(completionHandler: {(connection, result, error) -> Void in 

      if error != nil { 
       print("Error retrieving details: \(error?.localizedDescription)") 
       return 
      } 

      guard let result = result as? [String:[AnyObject]], let email = result["email"] else { 
       return 
      } 
      print("Email: \(email)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") 
      self.view.backgroundColor = UIColor.red 

     }) 
    } 

và trong tập tin appdelegate.swift tôi:

//have both google and facebook signin. Google works but facebook doesn't 
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 
     return GIDSignIn.sharedInstance().handle(url, 
                sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, 
                annotation: options[UIApplicationOpenURLOptionsKey.annotation]) || 
     FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) 
    } 

Tôi có thể đăng nhập và đăng xuất nhưng không thể lấy email.

CẬP NHẬT Thực ra khi tôi thực sự chuyển in (email), tôi có thể thấy nó trên bảng điều khiển dưới dạng câu lệnh tùy chọn. Tôi đang gặp rắc rối hiển thị nó mà không Statment tùy chọn

+0

Người dùng có cấp quyền email cho ứng dụng không? Người dùng có địa chỉ email đã xác minh không? – WizKid

+0

@WizKid thực sự tôi có thể thấy id email nhưng tùy chọn của nó Có vấn đề với tuyên bố bảo vệ – user3497437

+0

Tôi có cùng một vấn đề – Carlo

Trả lời

11

Tôi đã giải quyết vấn đề theo cách này:

func fetchProfile(){ 
    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields" : "email, name, id, gender"]) 
     .start(completionHandler: { (connection, result, error) in 
      guard let result = result as? NSDictionary, let email = result["email"] as? String, 
       let user_name = result["name"] as? String, 
       let user_gender = result["gender"] as? String, 
       let user_id_fb = result["id"] as? String else { 
        return 
      }   
     }) 

    } 
+1

xin lỗi vì sự chậm trễ Tôi đã được ra ngoài và về cho một vài ngày.Một lần nữa cảm ơn cho phản ứng.Ngoài ra rất mới để nhanh chóng và chọn lên rất nhiều – user3497437

+0

Không có vấn đề, nhờ – Carlo

+0

Điều này làm việc một cách hoàn hảo. nse một khi bạn đọc lên trên nó. Làm việc cho tôi trên Swift 3, Xcode 8. Cảm ơn rất nhiều. –

0

Giải pháp này làm việc tốt cho tôi mà không có "/" trong tham số graphPath!
FBSDKGraphRequest (graphPath: "me" .....

Các vấn đề liên quan