2017-06-03 103 views
0

試圖讓Facebook連接工作從一個迅速的項目。swift 3 facebook整合

一直試圖沿着以下YouTube視頻如下:https://www.youtube.com/watch?v=I6rTmfLp9aY

不幸,我是在德國。

所以這是我到目前爲止有:

  1. 我有我的IOS的Facebook應用程序啓用啓用和我種我的bundleID那裏。

  2. 下載最新的iOS框架,並添加到項目

到AppDelegate的文件,我說:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    FBSDKApplicationDelegate.sharedInstance() 
    return true 
} 

func applicationDidBecomeActive(_ application: UIApplication) { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    FBSDKAppEvents.activateApp() 
} 

這就是視圖控制器文件

import UIKit 
import FBSDKCoreKit 
import FBSDKLoginKit 
import FBSDKShareKit 




class ViewController: UIViewController, FBSDKAppInviteDialogDelegate, FBSDKLoginButtonDelegate{ 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

       if (FBSDKAccessToken.current() != nil) 
     { 
      let content = FBSDKAppInviteContent() 
      content.appLinkURL = NSURL(string: "{Facebook link to app}") as URL! 
      FBSDKAppInviteDialog.show(from: self, with: content, delegate: self) 
     } 
     else 
     { 
      let loginView : FBSDKLoginButton = FBSDKLoginButton() 
      self.view.addSubview(loginView) 
      loginView.center = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY + 100) 
      loginView.readPermissions = ["public_profile", "email"] 
     } 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 




func appInviteDialog (_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!) 
{ 

} 


func appInviteDialog (_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) { 
    print("Error took place in appInviteDialog \(error)") 
} 


func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 

    if ((error) != nil) 
    { 
     //process error 
    } 
    else if result.isCancelled { 
     //handle cancelation 
    } 
    else { 

     let content = FBSDKAppInviteContent() 
     content.appLinkURL = NSURL(string: "{Facebook link to app}") as URL! 
     FBSDKAppInviteDialog.show(from: self, with: content, delegate: self) 

     if result.grantedPermissions.contains("email") 
     { 
      //do work 
     } 
    } 
} 

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) { 

} 


} 

沒有錯誤,沒有警報。當我運行模擬器時,我得到一個空的屏幕。必須做一些正確的事情,因爲我得到了以下信息:

SystemGroup/systemgroup.com.apple.configurationprofiles 2017年6月4日00:42:02.351876 + 0300 facebook_login [4569:144075] [MC]從私人閱讀有效的用戶設置。

也是,如果我只是在viewDidLoad中粘貼在以下幾行代碼:

  let loginView : FBSDKLoginButton = FBSDKLoginButton() 
      self.view.addSubview(loginView) 
      loginView.center = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY + 100) 
      loginView.readPermissions = ["public_profile", "email"] 

我在模擬器得到一個漂亮的Facebook按鈕當然死機當我按下它。

任何幫助的工作將是極大的讚賞

回答

1

Facebook擁有迅捷的SDK您可能會發現更容易比Objective-C的一個(它們只是調用的iOS)使用。嘗試在這裏尋找文檔各地:

https://developers.facebook.com/docs/swift

而且,遵循(其他)的iOS SDK中描述的步驟開始:

https://developers.facebook.com/docs/ios/getting-started/

這是最小的應用程序委託我可以開始工作(注意,斯威夫特SDK缺少存在於iOS的SDK中的FB前綴):

import UIKit 
import FacebookCore 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 
     SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) 
     return true 
    } 

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 
     return SDKApplicationDelegate.shared.application(app, open: url, options: options) 
    } 

} 

而且是務必在您的Info.plist中添加所有必需的密鑰,否則您將無法獲得身份驗證。

+0

我知道Facebook的swift sdk不支持當前的swift和Xcode –

+0

我正在使用Xcode和Swift的最新版本,僅僅是建立框架的一些小警告。無論如何,他們沒有那麼不同,我敢肯定,如果你更喜歡其他SDK,你可以用它們完成同樣的事情。只要確保遵循文檔中的所有步驟。注意App Delegate和Info.plist。代碼現在可以使用 –

+0

。我忘記了更新Info.plist。謝謝 –