IOS接入facebook詳解
facebook 接入
1.在 facebook 后臺(tái) 配置 ios 平臺(tái), 獲取 fb appid
store id 可以先隨便找一個(gè)已存在的填入進(jìn)去.
2.CocoaPods 引入幾個(gè)核心庫(kù)
pod 'FBSDKCoreKit', '~> 9.0.1' pod 'FBSDKLoginKit', '~> 9.0.1' pod 'FBSDKShareKit', '~> 9.0.1'
pod 工程構(gòu)建出 靜態(tài)庫(kù)丟到 /Users/XXX/Library/Developer/Xcode/DerivedData/Build/Products
對(duì)應(yīng) 真機(jī)/模擬器 的目錄下, 然項(xiàng)目工程編譯時(shí)可以鏈接到靜態(tài)庫(kù)
3.在 info.plist 文件的 <dict>...</dict>
內(nèi)加入配置
<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> <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>
[APP_ID] 替換 fb appid.
[APP_NAME] 替換為 app 名.
4.代碼
在 app 啟動(dòng)完后初始化 sdk
// AppDelegate.m @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<UIApplicationOpenURLOptionsKey, id> *)options{ [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options]; return YES; }
iOS 13 將打開網(wǎng)址的功能移到了 SceneDelegate 中
// SceneDelegate.m #import <FBSDKCoreKit/FBSDKCoreKit.h> @import FacebookCore; @implementation SceneDelegate - (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts{ UIOpenURLContext *context = URLContexts.allObjects.firstObject; [FBSDKApplicationDelegate.sharedInstance application:UIApplication.sharedApplication openURL:context.URL sourceApplication:context.options.sourceApplication annotation:context.options.annotation]; }
登錄, 登出, 獲取用戶信息, 展示 登錄頁(yè)面
#import "FBHelper.h" #import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKLoginKit/FBSDKLoginKit.h> @implementation FBHelper static FBHelper *_sharedIns = nil; +(instancetype) shareInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedIns = [[self alloc] init] ; }) ; return _sharedIns ; } -(void)showLoginBtn:(UIView*)view{ FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init]; loginButton.center = view.center; [view addSubview:loginButton]; } -(void)login:(UIViewController*)vc{ FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logInWithPermissions:@[@"publish_actions"] fromViewController:vc handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { NSLog(@"--- login fail, err: %@", error); return; } FBSDKAccessToken* accessToken = [FBSDKAccessToken currentAccessToken]; if (accessToken) { NSLog(@"--- login success, userId: %@, token: %@", accessToken.userID, accessToken.tokenString); return; } NSLog(@"--- login cancel"); }]; } -(void)logout{ if ([FBSDKAccessToken currentAccessToken]) { NSLog(@"--- has accessToken"); FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logOut]; } else { NSLog(@"--- no accessToken"); } } -(void)getUserData{ if ([FBSDKAccessToken currentAccessToken]) { if ([FBSDKAccessToken currentAccessToken]) { [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me?fields=id,name,token_for_business" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { NSLog(@"--- fetched success, result: %@", result); } else { NSLog(@"--- fetched error: %@", error); } }]; } } else { NSLog(@"--- no accessToken"); } } @end
踩坑
編譯報(bào)錯(cuò): 找不到 swift 相關(guān)文件
報(bào)錯(cuò): Undefined symbol: __swift_FORCE_LOAD_$_
原因是因?yàn)?sdk 使用了 swift, 而工程配置里沒(méi)有配置 swift 頭文件搜索的路徑.
解決辦法很簡(jiǎn)單: 創(chuàng)建一個(gè) swift 文件即可, xcode 會(huì)提示創(chuàng)建橋接文件 Create Bridging Header
以上就是IOS接入facebook詳解的詳細(xì)內(nèi)容,更多關(guān)于IOS接入facebook的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS Xcode升級(jí)Xcode15報(bào)錯(cuò)SDK does not contain
這篇文章主要為大家介紹了iOS Xcode 升級(jí)Xcode15報(bào)錯(cuò): SDK does not contain 'libarclite'解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Objective-C中NSNumber與NSDictionary的用法簡(jiǎn)介
這篇文章主要介紹了Objective-C中NSNumber與NSDictionary的用法簡(jiǎn)介,雖然Objective-C即將不再是iOS的主流開發(fā)語(yǔ)言...well,需要的朋友可以參考下2015-09-09iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法
這篇文章給大家分享了iOS如何設(shè)置UIButton的文字顯示位置和字體的大小、顏色,文中給出了示例代碼,相信對(duì)大家的學(xué)習(xí)和理解很有幫助,有需要的朋友們下面來(lái)一起看看吧。2016-09-09Xcode 9下適配iPhoneX導(dǎo)致iOS 10不兼容問(wèn)題的解決方法
這篇文章主要給大家介紹了關(guān)于Xcode 9下適配iPhoneX導(dǎo)致iOS 10不兼容問(wèn)題的解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04iOS利用UITableView設(shè)置全屏分隔線的3種方法總結(jié)
這篇文章主要介紹了關(guān)于iOS利用UITableView設(shè)置全屏分隔線的幾種方法的相關(guān)對(duì)比,分析這三種的各自優(yōu)缺點(diǎn),并且分享了設(shè)置UITableView的單元格分割線離屏幕左右的距離為0的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起看看吧。2017-11-11詳解Obejective-C中將JSON數(shù)據(jù)轉(zhuǎn)為模型的方法
這篇文章主要介紹了Obejective-C中JSON數(shù)據(jù)轉(zhuǎn)為模型的方法,同時(shí)介紹了使用jastor庫(kù)的方法,需要的朋友可以參考下2016-03-03iOS判斷運(yùn)營(yíng)商類型的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇iOS判斷運(yùn)營(yíng)商類型的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04