我正在尝试在 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,一旦流程完成后再回到颤动状态?或者有一些简单的方法可以知道焦点何时返回到我的颤动窗口小部件,以便我可以检查登录是否成功?