PlatformHostApiImpl.swift
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Foundation
import AuthenticationServices
import UIKit
import WebKit
/**
* PlatformApi iOS implementation.
*
* 组装完整 User-Agent,格式与 Android 端保持一致:
* `{systemWebViewUA} doublefeel/{versionCode}({versionName})(Apple##Apple##{model}; iOS{osVersion}; {height}x{width})(huawei)`
*/
final class PlatformHostApiImpl: PlatformHostApi {
private var appleSignInCoordinator: AppleSignInCoordinator?
func requestAppleSignIn(completion: @escaping (Result<AppleSignInModel?, any Error>) -> Void) {
DispatchQueue.main.async { [weak self] in
guard let self else {
completion(.failure(PlatformHostApiError.deallocated))
return
}
let provider = ASAuthorizationAppleIDProvider()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
guard let presentationAnchor = AppleSignInCoordinator.currentPresentationAnchor() else {
completion(.failure(PlatformHostApiError.missingPresentationAnchor))
return
}
let coordinator = AppleSignInCoordinator(presentationAnchor: presentationAnchor) { [weak self] result in
self?.appleSignInCoordinator = nil
completion(result)
}
appleSignInCoordinator = coordinator
let controller = ASAuthorizationController(authorizationRequests: [request])
controller.delegate = coordinator
controller.presentationContextProvider = coordinator
controller.performRequests()
}
}
func getFullUserAgent() throws -> String {
return UserAgent.share.finalUA
}
}