做之前我也在网上查过,很多有用的方法,也做了参考。首先当然是要下载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)
}