像iOS SWIFT按钮一样添加Facebook

时间:2016-04-29 11:38:44

标签: ios swift facebook facebook-like

我正在尝试按照the Facebook tutorial

在我的iOS Swift应用中添加like按钮

我使用Pods和Bridging Header文件添加FB框架:

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

我使用App Facebook ID更新了plist

enter image description here

然后,在我的viewDidLoad中,我添加了:

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

显示类似按钮,但是当我按下它时,没有任何反应。 我不明白我想念的是什么

编辑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) 
}

1 个答案:

答案 0 :(得分:0)

在info.plist中:

  • FacebookAppID (而不是FacebookAppId)
  • 删除FacebookAppID字符串中的“fb”(仅使用数字设置字符串)

为了调试我的Facebook按钮,我不得不在我的视图控制器中添加一个登录Facebook按钮,让Facebook登录控制台。

添加:

#import <FBSDKLoginKit/FBSDKLoginKit.h>

并使用:

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

然后出现了我的错误的日志。

在纠正之后,我删除了Facebook登录按钮,而类似按钮效果很好!