Integration new facebook SDK by swift

前端 未结 11 1467
情书的邮戳
情书的邮戳 2020-12-23 09:59

Today i tried to integrate facebook SDK to my Swift app but in the quick start on facebook guide page looks a bit different than my old code. How can i convert OBJ-C code be

11条回答
  •  梦毁少年i
    2020-12-23 10:30

    Swift 3 with Swift FacebookSDK

    Podfile

    pod 'FacebookCore'
    pod 'FacebookLogin'
    

    info.plist

    no changes from old sdk

    CFBundleURLTypes
    
        
            CFBundleURLSchemes
            
                fb$(YOUR_FB_APP_ID)
            
        
    
    LSApplicationQueriesSchemes
    
        fbapi
        fb-messenger-api
        fbauth2
        fbshareextension
    
    FacebookAppID
    $(YOUR_FB_APP_ID)
    FacebookDisplayName
    $(YOUR_APP_NAME)
    

    AppDelegate.swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
        ...
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        AppEventsLogger.activate(application)
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let appId = SDKSettings.appId
        if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host ==  "authorize" { // facebook
            return SDKApplicationDelegate.shared.application(app, open: url, options: options)
        }
        return false
    }
    
    ...
    

    LoginManager (custom login, see docs for default/button implementation)

    contains custom code but you get the point

    let facebookManager = LoginManager(loginBehavior: .systemAccount, defaultAudience: .everyone)
    func loginWithFacebook() {
        self.facebookManager.logIn(HAAccountManager.shared.facebookPermissions, viewController: self) { (result) in
            switch result {
            case .failed(let error):
                self.showAlert(forError: error as NSError)
            case .cancelled:
                print("FB login cancelled")
            case .success(let grantedPermissions, let deniedPermissions, let accessToken):
                if grantedPermissions.contains(Permission(name: "email")) == true {
                    ApiClient.shared.facebookSignIn(authToken: accessToken.authenticationToken, completion: { (err, user) in
                        if err == nil {
                            // success
                        }
                        else {
                            self.showAlert(forError: err!)
                        }
                    })
                }
                else {
                    self.showAlert(forError: HAError(title: String(format: String.somethingError, String.signIn), message: grantedPermissions.contains(Permission(name: "email")) == true ? String.noAccountFound : String.missingEmailForSignUp))
                }
            }
        }
    }
    

    Analytics

    // custom ex
    AppEventsLogger.log(AppEvent(name: "open_app", parameters: ["logged_in": NSNumber(value: HAAccountManager.shared.isUserLoggedIn())], valueToSum: nil))
    
    // purchase ex
    AppEventsLogger.log(
        AppEvent.purchased(
            amount: Double(revenue),
            currency: currency,
            extraParameters: [
                AppEventParameterName.contentId : orderId,
                AppEventParameterName.itemCount : order.orderItems.count.nsNumber()
            ])
    )
    

提交回复
热议问题