2017-04-04 2 views
3

私はSwiftプロジェクトを引き継いで、Facebookのログイン機能を追加する必要があります。私はそれが主に動作するように取得していますが、ここでは、このサンプルコード(https://developers.facebook.com/docs/swift/graph)に問題が生じています:Facebook iOS SDKのスウィフトバージョンとデータへのアクセス方法

輸入FacebookCore

struct MyProfileRequest: GraphRequestProtocol { 
    struct Response: GraphResponseProtocol { 
    init(rawResponse: Any?) { 
     // Decode JSON from rawResponse into other properties here. 
    } 
    } 

    var graphPath = "/me" 
    var parameters: [String : Any]? = ["fields": "id, name"] 
    var accessToken = AccessToken.current 
    var httpMethod: GraphRequestHTTPMethod = .GET 
    var apiVersion: GraphAPIVersion = .defaultVersion 
} 


let connection = GraphRequestConnection() 
connection.add(MyProfileRequest()) { response, result in 
    switch result { 
    case .success(let response): 
    print("Custom Graph Request Succeeded: \(response)") 
    print("My facebook id is \(response.dictionaryValue?["id"])") 
    print("My name is \(response.dictionaryValue?["name"])") 
    case .failed(let error): 
    print("Custom Graph Request Failed: \(error)") 
    } 
} 
connection.start() 

私はdictionaryValueオプションで行をコンパイルするには、エラーを取得しています/Users/jt/a-dev/tabfb/tabfb/LoginViewController.swift:72:31: Value of type 'MyProfileRequest.Response' has no member 'dictionaryValue'と言っています。これを使用してユーザー名またはIDにアクセスするにはどうすればよいですか?

答えて

5

今日もこの問題に直面しました。私は.success(let response)場合

struct Response: GraphResponseProtocol { 

    var name: String? 
    var id: String? 
    var gender: String? 
    var email: String? 
    var profilePictureUrl: String? 

    init(rawResponse: Any?) { 
     // Decode JSON from rawResponse into other properties here. 
     guard let response = rawResponse as? Dictionary<String, Any> else { 
      return 
     } 

     if let name = response["name"] as? String { 
      self.name = name 
     } 

     if let id = response["id"] as? String { 
      self.id = id 
     } 

     if let gender = response["gender"] as? String { 
      self.gender = gender 
     } 

     if let email = response["email"] as? String { 
      self.email = email 
     } 

     if let picture = response["picture"] as? Dictionary<String, Any> { 

      if let data = picture["data"] as? Dictionary<String, Any> { 
       if let url = data["url"] as? String { 
        self.profilePictureUrl = url 
       } 
      } 
     } 
    } 
} 

の値を使用するには、このように私のコードを再設計し、成功の場合には、あなたは値を取得することができます。私はMyProfileRequest

struct Response: GraphResponseProtocol { 
    init(rawResponse: Any?) { 
     // Decode JSON from rawResponse into other properties here. 
     guard let response = rawResponse as? Dictionary<String, Any> else { 
      return 
     } 

     if let name = response["name"], 
      let id = response["id"] { 

      print(name) 
      print(id) 
     } 
    } 
} 

EDIT内部のユーザIDと名前を得ましたこのように:

let connection = GraphRequestConnection() 
connection.add(MyProfileRequest()) { response, result in 
    switch result { 
    case .success(let response): 
    print("My facebook id is \(response.id!)") //Make sure to safely unwrap these :) 
    print("My name is \(response.name!)") 
    case .failed(let error): 
    print("Custom Graph Request Failed: \(error)") 
} 
} 
connection.start() 
1
import FBSDKLoginKit //FBSDKLoginKit installs automatically when you install FacebookCore through CocoaPods 

///Inside your view controller 
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 
    /// DEFAULT 
    //fired when fb logged in through fb's default login btn 
    if error != nil { 
     print(error) 
     return 
    } 

    showDetails() 
} 

fileprivate func showDetails(){ 
    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, first_name, last_name, email, gender"]).start { (connection, result, err) in 
     ////use link for more fields:::https://developers.facebook.com/docs/graph-api/reference/user 
     if err != nil { 
      print("Failed to start graph request:", err ?? "") 
      return 
     } 

     let dict: NSMutableDictionary = result as! NSMutableDictionary 
     print("The result dict of fb profile::: \(dict)") 
     let email = dict["email"] as! String! 
     print("The result dict[email] of fb profile::: \(email)") 
     let userID = dict["id"] as! String 
     print("The result dict[id] of fb profile::: \(userID)") 

//   self.profileImage.image = UIImage(named: "profile") 
     let facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large" 

    } 
} 

//make sure you add read permissions for email and public profile 

override func viewDidLoad(){ 
    super.viewDidLoad() 
    loginButtonFromFB.delegate = self //inherit FBSDKLoginButtonDelegate to your class 
    loginButtonFromFB.readPermissions = ["email", "public_profile"] 
} 
関連する問題