flutter如何实现苹果登录

在Flutter中实现苹果登录时,调用signInWithApple方法总是返回错误码1000。按照官方文档集成了apple_sign_in插件,配置了Info.plist的URL Scheme和后台服务ID,但调试时依然提示"Authorization failed"。想请教:

  1. 是否需要额外配置iOS项目的Entitlements文件?
  2. 测试时发现沙盒账户无法触发登录弹窗,这是设备问题还是代码逻辑缺陷?
  3. 有没有完整的Flutter+Firebase实现苹果登录的代码示例?
2 回复

Flutter实现苹果登录可使用sign_in_with_apple插件。步骤如下:

  1. 配置iOS项目:在Xcode中添加Sign in with Apple能力。
  2. 安装插件:flutter pub add sign_in_with_apple
  3. 调用登录方法:
final credential = await SignInWithApple.getAppleIDCredential(
  scopes: [AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName],
);
  1. 将返回的credential发送至后端验证。

更多关于flutter如何实现苹果登录的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中实现苹果登录,可以通过以下步骤完成:

1. 添加依赖

pubspec.yaml 中添加:

dependencies:
  sign_in_with_apple: ^5.0.0

2. iOS配置

  • ios/Runner/Info.plist 中添加:
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLName</key>
    <string>AppleID</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_BUNDLE_ID</string>
    </array>
  </dict>
</array>
  • 在Xcode中启用 Sign In with Apple Capability

3. 实现登录代码

import 'package:sign_in_with_apple/sign_in_with_apple.dart';

Future<void> appleSignIn() async {
  try {
    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
    );
    
    // 获取到的用户信息
    print('User Identifier: ${credential.userIdentifier}');
    print('Email: ${credential.email}');
    print('Given Name: ${credential.givenName}');
    print('Family Name: ${credential.familyName}');
    
    // 这里可以将credential发送到你的后端服务器进行验证
    // 后端需要使用identityToken进行验证
    
  } catch (error) {
    print('Apple Sign In Failed: $error');
  }
}

4. 调用登录

ElevatedButton(
  onPressed: appleSignIn,
  child: Text('Sign in with Apple'),
)

注意事项:

  1. 仅支持iOS 13.0+和macOS 10.15+
  2. 在真实设备上测试,模拟器不支持
  3. 需要在Apple开发者账号中配置App ID和Service ID
  4. 首次登录会返回用户姓名和邮箱,后续登录只返回userIdentifier

后端验证:

后端需要使用收到的identityToken向Apple服务器验证用户身份,具体可参考Apple的官方文档。

这样就完成了Flutter中苹果登录的基本实现。

回到顶部