How can I change the default design of FBSDKLoginButton iOS 8.3/Swift 1.2?

前端 未结 2 1349
不思量自难忘°
不思量自难忘° 2020-12-05 03:39

I\'m working on App currently and I need to change the default design of Facebook button which is provided by Facebook SDK. I succeeded to let the provided button be transpa

2条回答
  •  孤街浪徒
    2020-12-05 04:41

    You can use a default UIButton, and use FBSDKManager instead of using Facebook SDK/FBSDKLoginButton to log in. You can set highlighted state color, etc on the UIButton.

    Call method fbLoginInitiate from IBAction of your custom FB Button.

    func fbLoginInitiate() {
        let loginManager = FBSDKLoginManager()
        loginManager.logInWithReadPermissions(["public_profile", "email"], handler: {(result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
            if (error != nil) {
                // Process error
                self.removeFbData()
            } else if result.isCancelled {
                // User Cancellation
                self.removeFbData()
            } else {
                //Success
                if result.grantedPermissions.contains("email") && result.grantedPermissions.contains("public_profile") {
                    //Do work
                    self.fetchFacebookProfile()
                } else {
                    //Handle error
                }
            }
        })
    }
    
    func removeFbData() {
        //Remove FB Data
        let fbManager = FBSDKLoginManager()
        fbManager.logOut()
        FBSDKAccessToken.setCurrentAccessToken(nil)
    }
    
    func fetchFacebookProfile()
    {
        if FBSDKAccessToken.currentAccessToken() != nil {
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
            graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
    
                if ((error) != nil) {
                    //Handle error
                } else {
                    //Handle Profile Photo URL String                        
                    let userId =  result["id"] as! String
                    let profilePictureUrl = "https://graph.facebook.com/\(id)/picture?type=large"
    
                    let accessToken = FBSDKAccessToken.currentAccessToken().tokenString                        
                    let fbUser = ["accessToken": accessToken, "user": result]
                }
            })
        }
    }
    

提交回复
热议问题