二、Xcode開新專案
編輯專案下的info.plist
新增以下設定
Facebook developer key 記得先去申請 Facebook Developer
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb{your-app-id}</string> </array> </dict> </array> <key>FacebookAppID</key> <string>{your-app-id}</string> <key>FacebookDisplayName</key> <string>{your-app-name}</string>
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>facebook.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> <key>fbcdn.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> <key>akamaihd.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict>
三、將.framework檔放入
先new Group,名稱為Frameworks
在將FBSDKCoreKit.framework,FBSDKLoginKit.framework,FBSDKShareKit.framework放入Frameworks裡
四、寫程式
1.AppDelegate.swift
import UIKit import CoreData import FBSDKCoreKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application( application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) } ..... }2.ViewController.swift
import UIKit import FBSDKCoreKit import FBSDKShareKit import FBSDKLoginKit class ViewController: UIViewController { let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func loginAction(sender: AnyObject) { fbLoginManager.logInWithReadPermissions(["email"],fromViewController: self.parentViewController, handler: { (result, error) -> Void in if (error == nil){ let fbloginresult : FBSDKLoginManagerLoginResult = result if(fbloginresult.grantedPermissions.contains("email")) { self.getFBUserData() self.fbLoginManager.logOut() } } }) } func getFBUserData(){ if((FBSDKAccessToken.currentAccessToken()) != nil){ FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in if (error == nil){ print(result) } }) } } @IBAction func logoutAction(sender: AnyObject) { fbLoginManager.logOut() } }
打完收功...