ios-facebook access


title: ios-facebook接入
categories: Ios
tags: [ios, facebook]
date: 2021-02-13 23:31:18
comments: false
mathjax: true
toc: true

ios-facebook access


Prequel

  • official
    • GitHub - https://github.com/facebook/facebook-ios-sdk
    • Get Started - Facebook SDK for iOS - https://developers.facebook.com/docs/facebook-login/ios/v2.2?locale=zh_CN&sdk=cocoapods
      • Introduction of CocoaPods-https://developers.facebook.com/docs/ios/componentsdks#cocoapods, https://developers.facebook.com/docs/ios/use-cocoapods
    • Login-https://developers.facebook.com/docs/ios/use-facebook-login
    • User data-https://developers.facebook.com/docs/ios/get-user-data
    • Share-https://developers.facebook.com/docs/ios/share-photos

facebook access

  1. Configure the ios platform in the facebook background and get the fb appid

    • You can find an existing store id and fill it in.
  2. CocoaPods introduces several core libraries

    pod 'FBSDKCoreKit', '~> 9.0.1'
    pod 'FBSDKLoginKit', '~> 9.0.1'
    pod 'FBSDKShareKit', '~> 9.0.1'
    
    • pod project to build a static library thrown into /Users/XXX/Library/Developer/Xcode/DerivedData/Build/Productsthe corresponding directory under real machine / simulator, when the project can be compiled and then linked to a static library

  3. In info.plist files <dict>...</dict>added in the configuration

    <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>
    
    1. [APP_ID] Replace fb appid.
    2. [APP_NAME] is replaced with the app name.
  4. Code

    1. Initialize the SDK after the app is started

      // 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;
      }
      
    2. iOS 13 moved the function of opening URLs to 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];
      }
      
    3. Log in, log out, get user information, display the login page

      #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
      

Step on the pit

Compile error: swift related files could not be found

Error: Undefined symbol: __swift_FORCE_LOAD_$_

The reason is because the SDK uses swift, and the swift header file search path is not configured in the project configuration.

The solution is simple: just create a swift file, and xcode will prompt to create a bridging file Create Bridging Header

Reference: https://github.com/facebook/react-native-fbsdk/issues/794

Guess you like

Origin blog.csdn.net/yangxuan0261/article/details/113806161