首頁 > 軟體

Facebook iOS SDK程式設計入門

2019-12-26 03:38:18

社交軟體是移動開發必不可少的一個話題,本文介紹了Facebook SDK 4.0 的程式設計入門經驗,即通過Facebook登入的方法,已經獲取使用者的使用者名稱,email的方法。

1

去Facebook開發者網頁,My App -> Add New App


2

 選擇平台:iOS


3

建立新App ID



4

輸入Bundle Id,必須與Xcode中iOS專案的Bundle id一致


5

注意此時app還不能使用,必須在Status & Review中允許才行



1

建立一個新專案 FacebookDemo,選Swift,然後關閉Xcode

2

在Xcode專案檔案的目錄下,建立一個Podfile,加入:pod?'FBSDKCoreKit'pod?'FBSDKLoginKit'pod?'FBSDKShareKit'

3

命令列執行: pod install

4

開啟? FacebookDemo.xcworkspace

5

建立一個標頭檔案:FB_ObjectiveC_Bridging.h,加入:#import <FBSDKCoreKit/FBSDKCoreKit.h>#import <FBSDKLoginKit/FBSDKLoginKit.h>#import <FBSDKShareKit/FBSDKShareKit.h>

6

將此.h加入Xcode中


7

設定.plist檔案,加入FacebookAppID 和 FacebookDisplayName(注意圖中的FacebookId應為FacebookAppID)


8

AppDelegate.swift中的程式碼:func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {??????? // Override point for customization after application launch.??????? return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchi
ngWithOptions: launchOptions)??? }?????? 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) {??????? // 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()??? }
, sourceApplication: String?, annotation: AnyObject?) -> Bool {??????????? return FBSDKApplicationDelegate.sharedInstance().application(??????????????? application,??????????????? openURL: url,??????????????? sourceApplication: sourceApplication,??????????????? annotation: annotation)??? }?????? 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()??? }

1

在Storyboard中增加一個按鈕UIButton和兩個UILabel(用於使用者名稱和電郵的顯示),並且連結到程式碼中


2

設定Facebook登入按鈕的代理以及許可權:? ? ? ??self.fbLoginButton.readPermissions = ["public_profile", "email", "user_friends"]? ? ? ? self.fbLoginButton.delegate = self注意:要獲取電郵資訊,必須設定「email」許可權

3

登入代理:

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {                

        if ((error) != nil)   {

            // Process error

        }

        else if result.isCancelled {

            // Handle cancellations

        }

        else {

            if result.grantedPermissions.contains("email")   {

                if FBSDKAccessToken.currentAccessToken() != nil {

                    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)

                   graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in           

                       if ((error) != nil) {

                          println("Error: (error)")

                       }

                       else {

                            println("fetched user: (result)")                

                            self.lblEmail.text = result["email"] as? String

                            self.lblUserName.text = result["name"] as? String

                      }

               })                    

                }

            }

        }

    }

至此使用者就可以登入並獲取使用者資訊了。值得注意的是此登入過程就是獲取oauth2中獲取access_token的方法,是其他任何Facebook操作的基礎。



IT145.com E-mail:sddin#qq.com