macOS端使用Passkeys登录遇AuthorizationError Code=1错误求助
解决macOS Passkeys登录报错Code=1的排查方案
1. 确认Presentation Anchor的有效性
虽然你已验证锚点可用,但macOS对授权窗口的锚点有更严格的要求:
- 必须使用当前活跃的key window作为锚点,而非任意窗口
- 若在SwiftUI模态sheet中调用,需确保锚点是sheet所在的窗口实例
示例代码:
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { guard let keyWindow = NSApplication.shared.windows.first(where: { $0.isKeyWindow && !$0.isSheet }) else { fatalError("Failed to get valid key window for presentation") } return keyWindow }
2. 检查App Sandbox权限配置
macOS下Passkeys需要网络权限与系统服务交互,确认App Sandbox中已开启:
Network Outgoing Connections (Client)- 若使用iCloud同步Passkeys,需开启
iCloud Documents或iCloud Key-Value Storage(根据你的配置)
3. 确保在主线程执行授权请求
ASAuthorizationController的performRequests()必须在主线程调用,异步操作后容易出现线程错误:
// 正确做法:切换到主线程执行 DispatchQueue.main.async { let authController = ASAuthorizationController(authorizationRequests: [yourRequest]) authController.delegate = self authController.presentationContextProvider = self authController.performRequests() }
4. 验证PublicKeyCredential请求参数
虽然共享代码库,但macOS对请求参数的隐性校验更严格:
- 确认
relyingPartyIdentifier与苹果开发者后台配置的完全一致(含域名后缀) - 检查challenge数据是否为原始二进制Data,未被错误编码/解码
5. 确认macOS部署版本
Passkeys仅支持macOS 12.0及以上版本,检查项目的Deployment Target是否符合要求,避免低版本系统的兼容性问题
内容的提问来源于stack exchange,提问作者Harsh
相关产品推荐
相关产品推荐

