当前位置 : 主页 > 手机开发 > 其它 >

Swift版本的iOS iOS SDK以及如何访问Response中的数据

来源:互联网 收集:自由互联 发布时间:2021-06-11
我接管了一个 Swift项目,需要添加Facebook登录功能.我得到它主要工作,但这里的示例代码有问题( https://developers.facebook.com/docs/swift/graph): 导入FacebookCore struct MyProfileRequest: GraphRequestProtoco
我接管了一个 Swift项目,需要添加Facebook登录功能.我得到它主要工作,但这里的示例代码有问题( https://developers.facebook.com/docs/swift/graph):

导入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编译行时遇到错误:’MyProfileRequest.Response’类型的值没有成员’ dictionaryValue’.我如何使用它来访问用户名或ID?

我今天也遇到了这个问题.我在MyProfileRequest中获得了用户ID和名称

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

编辑:我重新设计了这样的代码,以使用.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
                }
            }
        }
    }
}

在成功案例中,您可以获得如下值:

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()
网友评论