{"id":1160,"date":"2025-06-01T10:43:07","date_gmt":"2025-06-05T03:38:07","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=1160"},"modified":"2025-06-05T10:43:07","modified_gmt":"2025-06-05T03:38:07","slug":"sign-in-with-apple","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/sign-in-with-apple\/","title":{"rendered":"Swift Apple \u767b\u5165\u5168\u653b\u7565\uff1a2025 \u6700\u65b0\u4e09\u6b65\u9a5f\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b\u2728"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>Swift Apple \u767b\u5165\u5168\u653b\u7565\uff1a2025 \u6700\u65b0\u4e09\u6b65\u9a5f\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b Sign in with Apple<\/h1>\n<h2>\u7c21\u4ecb<\/h2>\n<p>Sign In with Apple \u5728 2019 \u7684 WWDC \u767c\u5e03\uff0c\u8fd1\u5e7e\u5e74\u7684\u7b2c\u4e09\u65b9\u767b\u5165\u5728 Swift \u958b\u767c\u4e2d\u53ef\u8aaa\u662f\u5fc5\u5b78\u6280\u80fd\u3002\u82e5\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9\u767b\u5165\uff0c\u6839\u64da Apple \u7684 Guideline 4.8\uff0c\u5fc5\u9808\u63d0\u4f9b Sign in with Apple \u4f5c\u70ba\u7b49\u6548\u9078\u9805\uff0c\u5426\u5247\u5c07\u6703\u88ab\u62d2\u7d55\u4e0a\u67b6\u3002\u4eca\u5929\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728\u60a8\u7684 Swift \u61c9\u7528\u4e2d\u5be6\u73fe Apple \u767b\u5165\u3002<\/p>\n<h2>Guideline 4.8 &#8211; Design &#8211; Sign in with Apple<\/h2>\n<p>\u7c21\u800c\u8a00\u4e4b\uff0c\u82e5\u61c9\u7528\u4f7f\u7528\u4e86\u5176\u4ed6\u767b\u5165\u65b9\u5f0f\uff0c\u5247\u5fc5\u9808\u63d0\u4f9b Apple \u767b\u5165\u9078\u9805\u3002\u9019\u662f Apple \u7684\u898f\u7bc4\uff0c\u78ba\u4fdd\u7528\u6236\u7684\u96b1\u79c1\u548c\u5b89\u5168\u3002<\/p>\n<h2>\u4e00\u3001 Apple \u767b\u5165\u57fa\u672c\u8a2d\u5b9a<\/h2>\n<h3>1. \u5728 Apple Developer \u6dfb\u52a0 Sign in with Apple \u6b0a\u9650<\/h3>\n<p>\u524d\u5f80 <a href=\"https:\/\/developer.apple.com\/\" title=\"apple developer\" target=\"_blank\" rel=\"noopener\">Apple Developer<\/a> \u4e26\u9ede\u64ca\u60a8\u7684\u5c08\u6848\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6dfb\u52a0\u6559\u5b78\u7bc4\u4f8b2-1024x817.png\" alt=\"\u5728 Apple Developer \u6dfb\u52a0 Sign in with Apple \u6b0a\u9650\" title=\"\"><\/p>\n<p>\u63a5\u8457\uff0c\u5c07 Sign in with Apple \u529f\u80fd\u958b\u555f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6dfb\u52a0\u6559\u5b78\u7bc4\u4f8b1-1024x382.png\" alt=\"\u958b\u555f Sign in with Apple \u529f\u80fd\" title=\"\"><\/p>\n<h3>2. \u5728 Xcode \u6dfb\u52a0 Sign in with Apple \u6b0a\u9650<\/h3>\n<p>\u5728 Xcode \u4e2d\uff0c\u524d\u5f80 Info \u6a19\u7c64\u4e26\u9ede\u64ca Capability\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6dfb\u52a0\u6559\u5b78\u7bc4\u4f8b3.png\" alt=\"\u5728 Xcode \u4e2d\u7684 Capability \u8a2d\u5b9a\" title=\"\"><\/p>\n<p>\u641c\u5c0b\u4e26\u65b0\u589e Sign in with Apple\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6dfb\u52a0\u6559\u5b78\u7bc4\u4f8b5.png\" alt=\"\u65b0\u589e Sign in with Apple\" title=\"\"><\/p>\n<p>\u5b8c\u6210\u5f8c\uff0c\u60a8\u6703\u770b\u5230\u76f8\u61c9\u7684\u6b0a\u9650\u986f\u793a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6dfb\u52a0\u6559\u5b78\u7bc4\u4f8b4.png\" alt=\"\u986f\u793a Apple \u767b\u5165\u6b0a\u9650\" title=\"\"><\/p>\n<h2>\u4e8c\u3001 Apple \u767b\u5165\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h2>\n<h3>1. \u532f\u5165 AuthenticationServices<\/h3>\n<pre><code class=\"language-swift line-numbers\">import AuthenticationServices<\/code><\/pre>\n<h3>2. \u65b0\u589e Apple \u767b\u5165\u539f\u751f\u6309\u9215<\/h3>\n<pre><code class=\"language-swift line-numbers\">let button = ASAuthorizationAppleIDButton(authorizationButtonType: .default, authorizationButtonStyle: .black)\nbutton.frame = CGRect(x: 20, y: 100, width: 100, height: 50) \/\/ \u8abf\u6574\u9ad8\u5ea6\u4ee5\u7b26\u5408\u8a2d\u8a08\nbutton.cornerRadius = 8.0\nbutton.addTarget(self, action: #selector(appleLoginButtonTapped), for: .touchUpInside)\nself.view.addSubview(button)<\/code><\/pre>\n<h3>3. \u8a2d\u7f6e Apple \u767b\u5165\u6a23\u5f0f<\/h3>\n<ul>\n<li>black : \u9ed1\u8272<\/li>\n<li>white : \u7121\u908a\u6846\u767d\u8272<\/li>\n<li>whiteOutline : \u6709\u908a\u6846\u767d\u8272<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6a23\u5f0f2-473x1024.png\" alt=\"Apple \u767b\u5165\u6a23\u5f0f\u793a\u4f8b\" title=\"\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6a23\u5f0f1-473x1024.png\" alt=\"Apple \u767b\u5165\u6a23\u5f0f\u793a\u4f8b\" title=\"\"><\/p>\n<h3>4. \u65b0\u589e Apple \u767b\u5165\u4e8b\u4ef6<\/h3>\n<pre><code class=\"language-swift line-numbers\">@objc func appleLoginButtonTapped() {\n    if #available(iOS 13.0, *) {\n        let provider = ASAuthorizationAppleIDProvider()\n        let request = provider.createRequest()\n        request.requestedScopes = [.email, .fullName]\n        let controller = ASAuthorizationController(authorizationRequests: [request])\n        controller.delegate = self\n        controller.presentationContextProvider = self\n        controller.performRequests()\n    }\n}<\/code><\/pre>\n<h3>5. \u76e3\u807d Apple \u767b\u5165 Delegate<\/h3>\n<pre><code class=\"language-swift line-numbers\">extension HomeViewController: ASAuthorizationControllerDelegate {\n    @available(iOS 13.0, *)\n    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {\n        guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential else {\n            return\n        }\n        \/\/ \u4e0a\u50b3\u6191\u8b49\u81f3 API\n    }\n    @available(iOS 13.0, *)\n    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {\n        \/\/ \u986f\u793a\u932f\u8aa4\u8a0a\u606f\n    }\n}\n\nextension HomeViewController: ASAuthorizationControllerPresentationContextProviding {\n    @available(iOS 13.0, *)\n    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {\n        return self.view.window!\n    }\n}<\/code><\/pre>\n<h2>\u4e09\u3001 \u6700\u5f8c\u57f7\u884c DEMO<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/ezgif-1-2bf80becb58b.gif\" alt=\"Apple \u767b\u5165 DEMO\" title=\"\"><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u900f\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u5c07\u80fd\u5920\u5728 Swift \u61c9\u7528\u4e2d\u6210\u529f\u6574\u5408 Sign in with Apple \u529f\u80fd\u3002\u9019\u4e0d\u50c5\u7b26\u5408 Apple \u7684\u898f\u7bc4\uff0c\u4e5f\u70ba\u7528\u6236\u63d0\u4f9b\u4e86\u65b9\u4fbf\u8207\u5b89\u5168\u7684\u767b\u5165\u9078\u9805\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. Sign in with Apple \u6709\u54ea\u4e9b\u512a\u52e2\uff1f<\/h3>\n<p>Sign in with Apple \u8b93\u7528\u6236\u80fd\u4ee5\u7c21\u55ae\u4e14\u5b89\u5168\u7684\u65b9\u5f0f\u767b\u5165\u61c9\u7528\uff0c\u4e26\u4fdd\u8b77\u5176\u500b\u4eba\u8cc7\u8a0a\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406 Apple \u767b\u5165\u5931\u6557\u7684\u60c5\u6cc1\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728\u4ee3\u7406\u65b9\u6cd5\u4e2d\u6355\u6349\u932f\u8aa4\uff0c\u4e26\u986f\u793a\u9069\u7576\u7684\u932f\u8aa4\u8a0a\u606f\u7d66\u7528\u6236\uff0c\u5efa\u8b70\u7528\u6236\u91cd\u8a66\u6216\u4f7f\u7528\u5176\u4ed6\u767b\u5165\u65b9\u5f0f\u3002<\/p>\n<h3>3. \u662f\u5426\u53ef\u4ee5\u5728\u5176\u4ed6\u5e73\u53f0\u4e0a\u4f7f\u7528 Sign in with Apple\uff1f<\/h3>\n<p>\u662f\u7684\uff0cSign in with Apple \u4e5f\u53ef\u4ee5\u5728 Web \u61c9\u7528\u548c\u5176\u4ed6\u5e73\u53f0\u4e2d\u4f7f\u7528\uff0c\u53ea\u9700\u9075\u5faa\u76f8\u61c9\u7684\u6574\u5408\u6b65\u9a5f\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`html Swift Apple &#8230;<\/p>\n","protected":false},"author":1,"featured_media":1169,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-1160","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/02\/apple\u767b\u5165\u6dfb\u52a0\u6559\u5b78\u7bc4\u4f8b2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-iI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=1160"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1160\/revisions"}],"predecessor-version":[{"id":1177,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1160\/revisions\/1177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/1169"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=1160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=1160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=1160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}