服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS接入facebook详解

IOS接入facebook详解

2021-06-06 17:23酷到被通缉_ IOS

这篇文章主要介绍了IOS接入facebook详解,对接入第三方SDK感兴趣的同学,可以参考下

facebook 接入

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

IOS接入facebook详解

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

2.CocoaPods 引入几个核心库

?
1
2
3
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详解

3.在 info.plist 文件的 <dict>...</dict> 内加入配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
// 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 中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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];
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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的资料请关注服务器之家其它相关文章!

延伸 · 阅读

精彩推荐
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19