我正在尝试在Flutter中实现原生的facebook登录。
到目前为止,我有一个MethodChannel:
const platform = const MethodChannel('com.app/facebook');
我有一个flutter按钮,它会启动登录序列:
onPressed: () async {
try {
final String fbButtonResult = await platform.invokeMethod('doFacebookLogin');
fbLoginResult = 'FB Button Result: $fbButtonResult';
} on PlatformException catch (e) {
fbLoginResult = "No FB button: '${e.message}'";
}
print("this never executes");
print(fbLoginResult);
}
我有一些iOS代码来执行本机登录:
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* facebookChannel = [FlutterMethodChannel
methodChannelWithName:@"com.app/facebook"
binaryMessenger:controller];
[facebookChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
// check which function was called
if ([@"doFacebookLogin" isEqualToString:call.method]) {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile", @"email"]
fromViewController:controller
handler:^(FBSDKLoginManagerLoginResult *fbResult, NSError *error) {
if (error) {
NSLog(@"Process error");
result(@"Process error");
} else if (fbResult.isCancelled) {
NSLog(@"Cancelled");
result(@"Cancelled");
} else {
NSLog(@"Logged in");
result(@"Logged in");
}
}];
} else {
result(FlutterMethodNotImplemented);
}
}];
当我们执行此操作时,FB本机登录功能运行良好并正确地运行流程。完成后,“Logged In”的NSLog显示在日志中,但结果似乎没有回到flutter应用程序,让我知道发生了什么。
我们需要以某种方式知道这是否成功,以便我们可以获取凭据并将视图推进到下一步。上面的代码中是否存在问题,或者我是否误解了MethodChannels的工作原理?或者我们是否需要做一些完全不同的事情,比如从iOS代码中调用一个invokeMethod,一旦流程完成后再回到颤动状态?或者是否有一些简单的方法可以知道焦点何时返回到我的颤动小部件,以便我可以检查登录是否成功?