1、?登錄
注冊Facebook開發(fā)者賬號,登錄Facebook開發(fā)者平臺
https://www.facebook.com/login/
2、?創(chuàng)建應(yīng)用
https://developers.facebook.com/docs/app-events/getting-started-app-events-ios#disable-auto-events官網(wǎng)鏈接
(1)、在App Dashboard界面,點擊“My Apps”,?然后創(chuàng)建一個新應(yīng)用程序(如果您還沒有的話)。然后點擊左側(cè)的“Settings(設(shè)置)”?>“基本(Basic)”以查看具有您的App ID,您的App Secret和有關(guān)您的應(yīng)用程序的其他詳細信息的“應(yīng)用程序詳細信息面板”?。
(2)、向下滾動到頁面底部,然后點擊添加平臺。選擇iOS,添加您的應(yīng)用程序詳細信息,然后保存更改。
通過添加以下詳細信息來設(shè)置廣告應(yīng)用程序:
應(yīng)用程序域(App Domains?)-提供您的應(yīng)用程序的Apple App Store URL。
隱私權(quán)政策網(wǎng)址(Privacy Policy URL)-提供隱私權(quán)政策網(wǎng)址。必須公開您的應(yīng)用。
服務(wù)條款URL(Terms of Service URL)-提供服務(wù)條款URL。
平臺(Platform?)-滾動到“設(shè)置”面板的底部以添加iOS平臺。
3、?設(shè)置Xcode開發(fā)環(huán)境
使用?Cocoapod
1、將下列代碼添加到?Podfile?中:
pod 'FBSDKLoginKit'
2、在終端窗口的項目根目錄中運行以下命令:
$ pod install
4、?配置應(yīng)用
1、添加Bundle Identifier
2、啟用單一登錄
5、?配置項目
配置?Info.plist?文件。
右鍵點擊?Info.plist,然后選擇Open As(打開方式)??Source Code(源代碼)或者文本。
將下列?XML?代碼片段復(fù)制并粘貼到文件正文中?(<dict>...</dict>)。
<key>CFBundleURLTypes</key>?
<array>?
????<dict>?
????????<key>CFBundleURLSchemes</key>?
????????????<array>
?????????????????<string>fb[APP_ID]</string>
?????????????</array>?
? ? ? ?</dict>
?</array>
?<key>FacebookAppID</key>?
<string>[APP_ID]</string>
?<key>FacebookDisplayName</key>
?<string>[APP_NAME]</string>
在?[CFBundleURLSchemes]?鍵內(nèi)的?<array><string>?中,將[APP_ID]替換為應(yīng)用編號。
在?FacebookAppID?鍵內(nèi)的?<string>?中,將[APP_ID]替換為應(yīng)用編號。
在?FacebookDisplayName?鍵內(nèi)的?<string>?中,將[APP_NAME]替換為應(yīng)用名稱。
如要使用任何?Facebook?對話框(如登錄、分享、應(yīng)用邀請等)以將您的應(yīng)用切換至?Facebook?應(yīng)用,您應(yīng)用程序的?Info.plist?中還需包含:<dict>...</dict>。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>?
<string>fbapi20130410</string>
<string>fbapi20130702</string>?
<string>fbapi20131010</string>
<string>fbapi20131219</string>?
<string>fbapi20140410</string>
<string>fbapi20140116</string>
?<string>fbapi20150313</string>
<string>fbapi20150629</string>?
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>?
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
若是多個應(yīng)用使用同一Facebook應(yīng)用編號
?(1)、添加網(wǎng)址格式后綴(URL Scheme Suffix)
(2)、在添加的兩個后綴的應(yīng)用的.plist添加參數(shù)FacebookUrlSchemeSuffix
(3)、修改.plist?文件的URL types?下的Facebook里?URL Schemes
6、?配置App Delegate and Scene Delegate
在?AppDelegate?方法中添加以下代碼。此代碼會在啟動應(yīng)用時初始化?SDK,并在您執(zhí)行登錄或分享操作時,允許?SDK?處理原生?Facebook?應(yīng)用產(chǎn)生的結(jié)果。
Swift
// AppDelegate.swift
import UIKit
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate:UIResponder, UIApplicationDelegate {
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { ApplicationDelegate.shared.application( application, didFinishLaunchingWithOptions: launchOptions )
return true
}
func application( _ app:UIApplication, open url:URL, options: [UIApplication.OpenURLOptionsKey :Any] = [:] ) -> Bool {
ApplicationDelegate.shared.application( app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation] )
}
}???
Objective C
// AppDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h>
@import FacebookCore;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary *)options { [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options]; return YES;
}
iOS 13?將功能移到了?SceneDelegate?中。如果您使用的是?iOS 13,請將以下方法添加到?SceneDelegate?中,以便登錄或分享等功能的操作可按照預(yù)期運作(若Xcode中刪除了SceneDelegate?可不用配置):
Swift
// SceneDelegate.swift
import FBSDKCoreKit
...
func scene(_ scene:UIScene, openURLContexts URLContexts:Set) {
guard let url = URLContexts.first?.url else { return } ApplicationDelegate.shared.application( UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation] )
}???
Objective C
// SceneDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h>
@import FacebookCore;
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {
UIOpenURLContext *context = URLContexts.allObjects.firstObject; [FBSDKApplicationDelegate.sharedInstance application:UIApplication.sharedApplication openURL: context.URL sourceApplication:context.options.sourceApplication annotation:context.options.annotation];
} ?
7、?將登錄功能添加到代碼里
1、?在您的應(yīng)用中使用?Facebook?登錄按鈕
將?Facebook?登錄功能添加到代碼中
要向應(yīng)用添加具有?Facebook?品牌標(biāo)識“登錄”按鈕,請將下列代碼片段添加至視圖控制器。
// Swift //
// 將下列代碼添加到文件的頭文件中,例如:在 ViewController.swift 中導(dǎo)入FBSDKLoginKit
// 將下列代碼添加到正文類
ViewController:UIViewController {
?????? override func viewDidLoad() {
??????????? super.viewDidLoad()
???????????? let loginButton = FBLoginButton()
???????????? loginButton.center = view.center
?????????? view.addSubview(loginButton)
????? }
}
// Objective-C //
// 將下列代碼添加到文件的頭文件中,例如:在 ViewController.m 中
// 在 #import "ViewController.h" 之后
#import?<FBSDKCoreKit/FBSDKCoreKit.h>
#import?<FBSDKLoginKit/FBSDKLoginKit.h>
// 將下列代碼添加到正文:
@implementation ViewController
- (void)viewDidLoad {
?????? [super viewDidLoad];
?????? FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
????? // Optional: Place the button in the center of your
?????? view. loginButton.center = self.view.center; [self.view addSubview:loginButton];
}
@end
此時,您應(yīng)該能運行應(yīng)用并使用?Facebook“登錄”按鈕登錄。
2、?自定義“按鈕”登錄
使用登錄管理工具類?(FBSDKLoginManager)?和自定義按鈕?(UIButton)調(diào)用“登錄”對話框
//Swift
let loginManager? = LoginManager()
loginManager.logIn(permissions: ["public_profile"], from: self) { (result, error) in
guard error == nil else { return }
if result?.isCancelled == true {
return GoosPKHUD.shared().showInfoMessage(message: "用戶取消了登錄".localized)
}
// Objective-C //
?FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
? [login
??? logInWithReadPermissions: @[@"public_profile"]
????????? fromViewController:self
???????????????????? handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
??? if (error) {
??????NSLog(@"Process error");
??? } else if (result.isCancelled) {
????? NSLog(@"Cancelled");
??? } else {
????? NSLog(@"Logged in");
??? }
? }];