【问题标题】:IOS import errorIOS导入错误
【发布时间】:2017-05-18 02:49:08
【问题描述】:

我是 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:

【问题讨论】:

    标签: ios facebook api


    【解决方案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 代码。

    【讨论】:

    • 似乎已修复,但在该行之后出现错误,问题随屏幕更新
    • 感谢您的解释!可悲的是又得到了一个,我正在尝试修复它....答案已更新,有什么提示吗?
    • @GMX 我已经更新了我的答案。但是,您似乎正在混合来自 Facebook 的 Swift SDK 和 iOS SDK 的代码。这可能会导致其他错误。由于您似乎使用的是 iOS SDK,那么您应该确保您的代码与该 SDK 一起使用。见developers.facebook.com/docs/ios
    • @GMX 如果您不特别关注哪个 SDK,那么使用 Swift SDK 可能更适合。见developers.facebook.com/docs/swift
    • 现在我知道为什么会出现这个错误了,我会尝试使用Swift SDK,谢谢,你解释得很好
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多