Flutter的oauth_webauth包能否检测导航请求URL以打开Authenticator?
关于oauth_webauth包拦截导航请求URL的方案
- oauth_webauth包本身支持拦截导航请求,你可以通过
OAuthWebAuth.authorize方法中的navigationDelegate参数,实现类似webview_flutter的URL检测与拦截逻辑。 - 具体实现步骤:
- 调用
OAuthWebAuth.authorize时,传入自定义的NavigationDelegate,重写onNavigationRequest方法。 - 在
onNavigationRequest中判断请求URL是否为Microsoft Authenticator的自定义scheme(通常以msauth://开头)。 - 若是自定义scheme,用
url_launcher包唤起原生应用,同时返回NavigationDecision.prevent阻止WebView加载该URL,避免触发ERR_UNKNOWN_URL_SCHEME错误。
- 调用
示例代码:
import 'package:oauth_webauth/oauth_webauth.dart'; import 'package:url_launcher/url_launcher.dart'; final authResult = await OAuthWebAuth.authorize( url: '你的Azure AD B2C授权URL', redirectUrl: '你的回调URL', clientId: '你的客户端ID', navigationDelegate: (NavigationRequest request) async { // 检测Authenticator的自定义scheme if (request.url.startsWith('msauth://')) { if (await canLaunchUrl(Uri.parse(request.url))) { await launchUrl(Uri.parse(request.url)); } // 阻止WebView加载该URL return NavigationDecision.prevent; } // 其他URL允许正常加载 return NavigationDecision.navigate; }, );
- 额外配置要求:
- Android:在
AndroidManifest.xml中添加对应scheme的<intent-filter>,允许应用处理msauth://链接。 - iOS:在
Info.plist中添加LSApplicationQueriesSchemes数组并包含msauth,同时配置对应的CFBundleURLSchemes。
- Android:在
内容的提问来源于stack exchange,提问作者Naveen N
相关产品推荐
相关产品推荐

