You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB Atlas App Services苹果登录部分设备/模拟器失败

问题描述

应用采用MongoDB Atlas App Services的Sign In with Apple认证服务,多数实体设备及适配iPad的Mac运行正常,但一台实体设备和所有模拟器登录失败,报错信息如下:

Error Domain=io.realm.app Code=-1 “[json.exception.parse_error.101]
parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: ‘<’” 
UserInfo={Error Code=-1, Server Log URL=, Error Name=MalformedJson, NSLocalizedDescription=[json.exception.parse_error.101] 
parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: ‘<’}

调试发现登录请求返回的response.body是HTML文档而非预期的JSON,片段如下:

<!DOCTYPE html>
<html>
  <head>
      <title>App Services</title>
      <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon" />
      <style>
        #app { display: none; }
      </style>
      <script>
        var settings = {"accountUIBaseUrl":"https://account.mongodb.com","adminUrl":"https://realm.mongodb.com","apiUrl":"https://realm.mongodb.com","bugsnagToken":"d93dd442ef3db183db76cc9ded3bc109","chartsUIBaseUrl":"https://charts.mongodb.com","cloudUIBaseUrl":"https://cloud.mongodb.com","endpointAPIUrl":"https://data.mongodb-api.com","endpointAPIUrlsByProviderRegion":{"aws-ap-south-1":"https://ap-south-1.aws.data.mongodb-api.com","aws-ap-southeast-1":"https://ap-southeast-1.aws.data.mongodb-api.com","aws-ap-southeast-2":"https://ap-southeast-2.aws.data.mongodb-api.com","aws-eu-central-1":"https://eu-central-1.aws.data.mongodb-api.com","aws-eu-west-1":"https://eu-west-1.aws.data.mongodb-api.com","aws-eu-west-2":"https://eu-west-2.aws.data.mongodb-api.com","aws-sa-east-1":"https://sa-east"...

登录代码与官方文档一致:

func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    guard
        let credential = authorization.credential as? ASAuthorizationAppleIDCredential,
        let token = credential.identityToken,
        let tokenString = String(data: token, encoding: .utf8)
    else {
        struct NoCredentialsError: Error { }
        presentError(title: "Login Error", error: NoCredentialsError())
        return
    }

    Task { @MainActor in
        do {
            let user = try await realmApp.login(credentials: .apple(idToken: tokenString))
            delegate?.loginViewControllerDidLogin(with: user, name: credential.fullName)
        } catch {
            print(String(describing: error))
            presentError(title: "Login Error", error: error)
        }
    }
}

已确认tokenString格式有效,尝试过重装Realm、升级至最新版本、清理SwiftPM缓存等操作,但模拟器问题自3月起未解决。


可能的原因及解决办法

1. 检查App Services的Apple认证配置

  • 确认Sign In with Apple提供商的「Client ID」与Xcode中配置的Bundle Identifier完全一致(含大小写)。
  • 检查是否开启了「Allow Email Scope」和「Allow Full Name Scope」,若应用请求了这些权限但配置未开启,可能触发异常跳转返回HTML。
  • 验证「Redirect URI」是否正确设置为mongodb-realm://<你的应用ID>/ios/callback,并确认Xcode的URL Scheme中已添加mongodb-realm。

2. 网络与区域限制排查

  • 故障设备/模拟器可能处于受限网络环境(如公司内网代理、防火墙),拦截了MongoDB API请求导致返回HTML拦截页。切换至手机热点或不受限网络测试。
  • 确认设备/模拟器的Apple ID区域与App Services部署区域匹配,区域限制可能导致认证请求被重定向到Web页面。

3. 手动指定Realm API端点

避免SDK自动解析端点错误,初始化RealmApp时显式设置区域端点:

let appConfig = AppConfiguration(baseURL: URL(string: "https://realm.mongodb.com")!,
                                 appID: "你的应用ID")
let realmApp = App(configuration: appConfig)

若应用部署在非美东区域,需替换为对应区域的URL(如欧盟区域用https://eu-west-1.aws.realm.mongodb.com)。

4. 重置设备/模拟器环境

  • 模拟器:执行「Hardware > Erase All Content and Settings」,彻底重置后重新安装应用。
  • 故障实体设备:删除应用、重启设备后重新安装测试。

5. 检查Apple Developer账号配置

  • 登录Apple Developer后台,确认「Sign In with Apple」服务已为当前Bundle ID启用,且「Primary App ID」配置正确。
  • 验证证书/配置文件是否过期,重新生成Provisioning Profile后安装到设备/模拟器。

内容的提问来源于stack exchange,提问作者Oscar Apeland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 00:13:10