Facebook第三方登录和分享

做之前我也在网上查过,很多有用的方法,也做了参考。首先当然是要下载facebook的SDK,然后按开发文档在Info.plist中添加必要的信息。

登录:

func loginWithFacebook(){
    let faceBookLoginManger = FBSDKLoginManager()
    faceBookLoginManger.logInWithReadPermissions(["public_profile", "email", "user_friends"], fromViewController: self) { (result, error) -> Void in
        if (error != nil){
            print("error is \(error)")
        }else if (result.isCancelled){
                
        }else{
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"])
            graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in
                if ((error) != nil){
                    print("Error: \(error)")
                }else{
                    print("\(FBSDKAccessToken.currentAccessToken().tokenString)")
                    print("\(result.valueForKey("id"))")
		    print("\(result.valueForKey("name"))")
            	}
            }
        }
    }
}
分享:

我需要分享的内容包括NSURL类型的URL,imageURL,String类型的标题和描述,定义一个分享的类Share

var share:Share?
share = Share.contentShare("Title", imgUrl: NSURL(string: "http://"), shareUrl: NSURL(string: "http://"), desc: "description") as? Share
    func shareOnFacebook(){
        let facebookShareDialog = self.getShareDialogWithContentURL(share!)
        if facebookShareDialog.canShow(){
            self.shareFacebookWithContent(self.share!)
        }
    }
    func getShareDialogWithContentURL(share:Share)->FBSDKShareDialog {
        
        let shareDialog = FBSDKShareDialog()
        shareDialog.shareContent = self.getShareLinkContentWithContentURL(share)
        return shareDialog
    }
    func getShareLinkContentWithContentURL(share:Share)->FBSDKShareLinkContent {
        
        let fbContent = FBSDKShareLinkContent()
        fbContent.contentURL = share.shareUrl
        fbContent.contentTitle = share.title
        fbContent.contentDescription = share.desc
        fbContent.imageURL = share.imgUrl
        return fbContent 
    }
    func shareFacebookWithContent(share:Share){
        
        FBSDKShareDialog.showFromViewController(self.parentViewController, withContent: self.getShareLinkContentWithContentURL(share), delegate: nil)
        
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值