IOS导入错误

时间:2017-01-03 22:11:18

标签: ios facebook api

我是IOS的新手,我刚刚开始使用Facebook的API-Graph。

现在我想做“/ me”查询,但是我收到了这个错误:

  

使用未解析的标识符'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:

error

1 个答案:

答案 0 :(得分:1)

GraphRequest应改为FBSDKGraphRequest

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

注意:除iOS SDK外,还有一个Swift特定的SDK。 https://developers.facebook.com/docs/swift

修改

根据您更新的问题,FBSDKGraphRequest似乎可以返回一个可选项。可选的意味着您将获得FBSDKGraphRequestnil的实例。

您可以通过几种不同的方式处理可选项。

  • 使用guard构造
  • 使用if let构造
  • 使用?

使用guard

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 { ... }

如果graphRequestnil,这将无声无效。

编辑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代码。