我对 IOS 非常陌生,刚刚开始使用 Facebook 的 API-Graph。
现在我想做“/me”查询,但收到此错误:
Use of unresolved identifier 'GraphRequest'
这是我的代码:
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
....
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
if result.grantedPermissions.contains("public_profile") && result.grantedPermissions.contains("user_friends") && result.grantedPermissions.contains("user_posts") && result.grantedPermissions.contains("user_photos")
{
//HERE IS MY QUERY
let params = ["fields" : "email, name"]
let graphRequest = GraphRequest(graphPath: "me", parameters: params) <-- HERE I GOT THE ERROR
graphRequest.start {
(urlResponse, requestResult) in
switch requestResult {
case .failed(let error):
print("error in graph request:", error)
break
case .success(let graphResponse):
if let responseDictionary = graphResponse.dictionaryValue {
print(responseDictionary)
print(responseDictionary["name"])
print(responseDictionary["email"])
}
}
}
guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: "01") else { return }
presentedController.modalTransitionStyle = UIModalTransitionStyle.partialCurl
self.present(presentedController, animated: true, completion: nil)
}
}
guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: "01") else { return }
presentedController.modalTransitionStyle = UIModalTransitionStyle.partialCurl
self.present(presentedController, animated: true, completion: nil)*/
}else{
//cambia stato bottone
let alert = UIAlertController(title: "Missing Permission", message: "We need all the permission for continue", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
我的导入和 FB 登录效果很好。我做错了什么? (我使用的是 Swift 3)
更新2:
最佳答案
GraphRequest
应该改为 FBSDKGraphRequest
。
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
注意:除了 iOS SDK 之外,还有一个 Swift 特定的 SDK。 https://developers.facebook.com/docs/swift
编辑:
根据您更新的问题,FBSDKGraphRequest
似乎可以返回一个可选的。可选意味着您将获得 FBSDKGraphRequest
或 nil
的实例。
您可以通过几种不同的方式处理可选项。
- 使用
守卫
结构 - 使用
if let
构造 - 使用
?
使用守卫
:
guard let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params) else {
// Handle the fact that graphRequest is nil
}
graphRequest.start { ... } // graphRequest is guaranteed to be not nil
使用if let
:
if let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params) {
graphRequest.start { ... } // graphRequest is guaranteed to be not
} else {
// Handle the fact that graphRequest is nil
}
使用 ?
:
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
graphRequest?.start { ... }
如果 graphRequest
为 nil
,这将不执行任何操作。
编辑 2:
您似乎是在使用 Facebook Swift SDK 方式而不是他们的 iOS SDK 方式调用 start
方法。
FBSDKGraphRequestHandler
是使用以下参数定义的 typedef
:
FBSDKGraphRequestConnection
连接id
结果NSError
错误
因此在调用start
时,您需要在闭包中考虑这些参数。
graphRequest.start { connection, result, error in
...
}
或者使用 _
作为您不感兴趣的参数。
graphRequest.start { _, result, _ in
...
}
注意:您的 switch
语句可能不适用于上面的代码。您将需要进行进一步的更改以使其使用提供的参数(连接、结果和错误)。同样,您可能会混合使用 Facebook 的 Swift SDK 代码和 iOS SDK 代码。
关于IOS导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41453204/