IOS接入facebook详解

facebook 接入

1.在 facebook 后台 配置 ios 平台, 获取 fb appid

IOS接入facebook详解_第1张图片

store id 可以先随便找一个已存在的填入进去.

2.CocoaPods 引入几个核心库

pod 'FBSDKCoreKit', '~> 9.0.1'
pod 'FBSDKLoginKit', '~> 9.0.1'
pod 'FBSDKShareKit', '~> 9.0.1'

pod 工程构建出 静态库丢到 /Users/XXX/Library/Developer/Xcode/DerivedData/Build/Products 对应 真机/模拟器 的目录下, 然项目工程编译时可以链接到静态库

IOS接入facebook详解_第2张图片

3.在 info.plist 文件的 ... 内加入配置

CFBundleURLTypes
 
 CFBundleURLSchemes  fb[APP_ID]  
 

FacebookAppID
[APP_ID]

FacebookDisplayName
[APP_NAME]

LSApplicationQueriesSchemes
 fbapi fbapi20130214 fbapi20130410 fbapi20130702 fbapi20131010 fbapi20131219 fbapi20140410 fbapi20140116 fbapi20150313 fbapi20150629 fbapi20160328 fbauth fb-messenger-share-api fbauth2 fbshareextension

[APP_ID] 替换 fb appid.

[APP_NAME] 替换为 app 名.

4.代码

在 app 启动完后初始化 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 *)options{
  [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options];
  return YES;
}

iOS 13 将打开网址的功能移到了 SceneDelegate 中

// SceneDelegate.m
#import 

@import FacebookCore;

@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];
}

登录, 登出, 获取用户信息, 展示 登录页面

#import "FBHelper.h"

#import 
#import 

@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

踩坑

编译报错: 找不到 swift 相关文件

报错: Undefined symbol: __swift_FORCE_LOAD_$_

原因是因为 sdk 使用了 swift, 而工程配置里没有配置 swift 头文件搜索的路径.

解决办法很简单: 创建一个 swift 文件即可, xcode 会提示创建桥接文件 Create Bridging Header

以上就是IOS接入facebook详解的详细内容,更多关于IOS接入facebook的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(IOS接入facebook详解)