Call Facebook delegates in Swift

2019-02-15 05:40发布

问题:

How can I call the Facebook delegates using Swift? Xcode doesn't autocomplete and I don't know how use it.

import UIKit

class ViewController: UIViewController,FBLoginViewDelegate {

    @IBOutlet var fbLoginView : FBLoginView

    override func viewDidLoad() {
        super.viewDidLoad()

        self.fbLoginView.delegate = self
        self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Facebook Delegates



}

回答1:

I will give one example translation and explain it so that you should be able to translate the rest of the delegate methods:

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user: (id<FBGraphUser>)user;
  1. The method name starts with "loginViewFetchedUserInfo". That stays the same
  2. The parameter is a pointer of type "FDBLoginView" This will get translated to an optional of type FBLoginView because it can be nil. The convention is to make it an Implicitly Unwrapped Optional so it will become FBLoginView!
  3. Protocols are types in and of themselves in Swift, so the user parameter will become simply FBGraphUser.
  4. The first parameter in a swift method is assumed to be just an internal name so the parameter can be named anything but for consistency we will name it the same: "loginView"
  5. The second parameter in a swift method is assumed to be both internal and external. In the objective-c version they happen to be the same so we can simply use it once and Swift will make it both the internal and external name

This leaves us with this translation:

func loginViewFetchedUserInto(loginView : FBLoginView!, user: FBGraphUser)