cmi cmi - 7 months ago 119
Swift Question

Add Facebook like button iOS SWIFT

I'm trying to add the like button in my iOS Swift app, following the Facebook tutorial

I use Pods and a Bridging Header file to add the FB framework:

pod 'FBSDKCoreKit', '~> 4.11'
pod 'FBSDKShareKit'


Bridging_Header.h

#ifndef isam_Bridging_Header_h
#define isam_Bridging_Header_h

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
#endif


I updated the plist with the App Facebook ID

enter image description here

Then, in my viewDidLoad I added:

let likeButton:FBSDKLikeControl = FBSDKLikeControl()
likeButton.objectID = "https://www.facebook.com/cestunmac/"
likeButton.frame = mailButton.frame
likeButton.likeControlStyle = .BoxCount
self.addSubview(likeButton)


The like button is displayed but when I press on it, nothing happens.
I don't understand what I miss

EDIT 1

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

//Do stuff
//...


FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}


func application(application: UIApplication,openURL url: NSURL,sourceApplication: String?,annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application,openURL: url,sourceApplication: sourceApplication, annotation: annotation)
}


func applicationDidBecomeActive(application: UIApplication) {

//Do stuff
//...

FBSDKAppEvents.activateApp()
}


MyViewController.swift

override func viewDidLoad() {

//Do stuff
//...

let likeButton:FBSDKLikeControl = FBSDKLikeControl()
likeButton.objectID = "https://www.facebook.com/cestunmac/"
likeButton.likeControlStyle = .BoxCount
self.shareAndCommentView.addSubview(likeButton)
}

cmi cmi
Answer

In the info.plist :

  • it's FacebookAppID (and not FacebookAppId)
  • remove the "fb" in the FacebookAppID string (set the string just with the numbers)

To debug my Facebook like button, I had to add a login Facebook button in my view controller to have the Facebook logs in the console.

Adding:

#import <FBSDKLoginKit/FBSDKLoginKit.h>

and using:

let loginButton:FBSDKLoginButton = FBSDKLoginButton()
loginButton.center = self.view.center
self.view.addSubview(loginButton)

Then the logs with my mistakes appeared.

After correcting them, I removed the Facebook login button, and the like button works well!

Comments