IOS接入facebook详解

 更新时间:2021年04月13日 09:37:33   作者:酷到被通缉_  
这篇文章主要介绍了IOS接入facebook详解,对接入第三方SDK感兴趣的同学,可以参考下

facebook 接入

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

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 对应 真机/模拟器 的目录下, 然项目工程编译时可以链接到静态库

3.在 info.plist 文件的 <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>

<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 启动完后初始化 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 将打开网址的功能移到了 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];
}

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

#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

踩坑

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

报错: Undefined symbol: __swift_FORCE_LOAD_$_

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

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

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

相关文章

  • Objective-C实现无限循环轮播器

    Objective-C实现无限循环轮播器

    这篇文章主要介绍了Objective-C实现无限循环轮播器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • iOS使用Charts框架绘制饼状图

    iOS使用Charts框架绘制饼状图

    本文主要介绍了iOS使用Charts框架绘制饼状图的方法,具有一定的参考价值,下面跟着小编一起来看下吧
    2016-12-12
  • iOS Label实现文字渐变色效果

    iOS Label实现文字渐变色效果

    文字渐变色可以使整体的效果更上一个档次,最近在开发中就遇到了这个需求,所以整理出来,下面这篇文章主要给大家介绍了关于iOS Label实现文字渐变色效果的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • iOS block循环引用详解及常见误区

    iOS block循环引用详解及常见误区

    这篇文章主要介绍了iOS block循环引用详解和应用,常见误区详解,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • Objective-C基础 自定义对象归档详解及简单实例

    Objective-C基础 自定义对象归档详解及简单实例

    这篇文章主要介绍了Objective-C基础 自定义对象归档详解及简单实例的相关资料,需要的朋友可以参考下
    2017-04-04
  • 详解在iOS11下app图标变空白的问题解决方法

    详解在iOS11下app图标变空白的问题解决方法

    本篇文章主要介绍了详解在iOS11下app图标变空白的问题解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • iOS开发TableView网络请求及展示预加载实现示例

    iOS开发TableView网络请求及展示预加载实现示例

    这篇文章主要为大家介绍了iOS开发TableView网络请求及展示预加载实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 利用iOS手势与scrollView代理实现图片的放大缩小

    利用iOS手势与scrollView代理实现图片的放大缩小

    这篇文章主要介绍了利用iOS的手势、scrollView代理来实现图片放大缩小的方法,文中通过示例代码介绍的很详细,相信对各位iOS开发者们来说具有一定的参考借鉴价值,有需要的朋友们下面来一起学习学习吧。
    2017-01-01
  • iOS开发技巧之自定义相机

    iOS开发技巧之自定义相机

    这篇文章主要为大家详细介绍了iOS开发技巧之自定义相机,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • iOS App设计模式开发中对迭代器模式的使用示例

    iOS App设计模式开发中对迭代器模式的使用示例

    这篇文章主要介绍了iOS App设计模式开发中对迭代器模式的使用示例,示例代码为传统的Objective-C,需要的朋友可以参考下
    2016-03-03

最新评论