1.准备工作
1.1facebook平台相关应用信息
APP-ID:819403999420334
CLIENT-TOKEN:89546876e99c34a8270d5653541c84b0
2.使用cocos creator3.6.0导出ios原生工程
3.使用cocosPod集成facebook 登陆sdk
3.1切换到原生工程根目录
cd /Users/shefeng/Documents/workspace/cocos/rummy-Mahjong/build/ios/proj
3.2生成Podfile文件
pod init
3.3打开Podfile修改平台为ios 11.0并增加facebook登陆sdk
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
target 'boost_container' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for boost_container
end
target 'cocos_engine' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for cocos_engine
end
target 'rummy-Mahjong-mobile' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for rummy-Mahjong-mobile
pod 'FBSDKLoginKit'
end
pod install
4.打开Info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb819403999420334</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>819403999420334</string>
<key>FacebookClientToken</key>
<string>89546876e99c34a8270d5653541c84b0</string>
<key>FacebookDisplayName</key>
<string>rummy-Mahjong</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
</array>
5.添加Keychain Sharing功能
6.添加相关代码
6.1修改AppDelegate.mm文件
#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//添加如下代码
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary *)options {
[[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options];
return YES;
}
7.添加登陆业务逻辑代码
+ (void)facebookLogin{
NSLog(@"native facebookLogin");
FBSDKAccessToken *accessToken = FBSDKAccessToken.currentAccessToken;
if(!accessToken || !accessToken.isExpired){
//过期
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithPermissions:@[@"public_profile"] fromViewController:[self topViewController] handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Logged in");
}
}];
}else{
//未过期
}
}
8.js调用oc
jsb.reflection.callStaticMethod("SFNativeClass","facebookLogin");
9.oc调用js
jsb.reflection.callStaticMethod("SFNativeClass","facebookLogin");
网友评论