gpt4 book ai didi

android - 在 Android 上使用新的 AppID 更新 Facebook SDK session (使用 Cordova/PhoneGap)

转载 作者:行者123 更新时间:2023-11-30 02:21:16 35 4
gpt4 key购买 nike

我们有一个适用于 Android 的 Cordova/PhoneGap 应用程序,用户可以使用 PhoneGap Facebook Connect 插件 ( https://github.com/Wizcorp/phonegap-facebook-plugin ) 使用他们的 Facebook 帐户登录。

但是,每次用户在设置中更改国家/地区时,我们都需要使用新的 AppId/AppName 以编程方式更新 Facebook session ,因为我们为每个国家/地区提供不同的 Facebook 应用程序。

我们可以创建一个连接到 Phonegap Facebook Connect 插件的自定义插件,每当用户更改国家或尝试使用 Facebook 登录时调用该插件:

package com.OURAPP.facebook;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONException;

import android.app.Activity;

import com.facebook.Session;

public class FacebookConnectMultiPlugin extends CordovaPlugin {

private Activity context;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
context = cordova.getActivity();
}

@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
if (action.equals("changeAppId")) {
String appId = args.getString(0);
Session session = new Session.Builder(context).setApplicationId(appId).build();
Session.setActiveSession(session);
callbackContext.success();
return true;
} else {
return false;
}
}
}

然而,在将 Phonegap Facebook Connect 更新到最新版本后(现在它使用新的 Facebook SDK),我们的自定义插件停止工作,我们无法找出问题所在。

tl;dr

如果它有帮助,我们在 iOS 上遇到了同样的问题,但我们可以解决它。在那种情况下,我们没有更新 Facebook AppName,但是,我们找不到在 Android 上执行此操作的任何方法。

这是我们的旧代码:

#import "FacebookConnectMultiPlugin.h"
#import <FacebookSDK/FacebookSDK.h>

@interface FacebookConnectMultiPlugin ()

@property (strong, nonatomic) NSString *appid;

@end

@implementation FacebookConnectMultiPlugin

- (void)changeAppId:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* myarg = [command.arguments objectAtIndex:0];

NSLog(@"This is the app id: %@", myarg);

FBSession *session = [[FBSession alloc] initWithAppID:myarg
permissions:nil
defaultAudience:FBSessionDefaultAudienceFriends
urlSchemeSuffix:nil
tokenCacheStrategy:nil];

[FBSession setActiveSession:session];

if (myarg != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end

这是新代码:

#import "FacebookConnectMultiPlugin.h"
#import <FacebookSDK/FacebookSDK.h>

@interface FacebookConnectMultiPlugin ()

@property (strong, nonatomic) NSString *appid;

@end

@implementation FacebookConnectMultiPlugin

- (void)changeAppId:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* myarg = [command.arguments objectAtIndex:0];
NSString* myDisplayName = [command.arguments objectAtIndex:1];

NSLog(@"----------------------");
NSLog(@"This is the app id: %@", myarg);
NSLog(@"This is the display name: %@", myDisplayName);
NSLog(@"----------------------");

FBSession *session = [[FBSession alloc] initWithAppID:myarg
permissions:nil
defaultAudience:FBSessionDefaultAudienceFriends
urlSchemeSuffix:nil
tokenCacheStrategy:nil];

[FBSession setActiveSession:session];

[FBSettings setDefaultAppID:myarg];

[FBSettings setDefaultDisplayName:myDisplayName];

if (myarg != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end

最佳答案

如果您有不同的 Facebook 应用程序,用户需要切换到该应用程序,那么他们也需要授权该应用程序。如果他们已经登录,您需要通过调用 Session.getActiveSession().closeAndClearTokenInformation() 将他们注销。

然后您可以像使用 Session.Builder 一样构建 session ,但是您必须使用 OpenRequest 打开它。查看 session.openForRead 是如何调用的 here .

此外,您还必须使用 UiLifecycleHelper 并实现 onActivityResult,您可以在此 post 中查看更详细的解释.

关于android - 在 Android 上使用新的 AppID 更新 Facebook SDK session (使用 Cordova/PhoneGap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28515011/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com