From 697af6e8cf3f6c12c2534432f00cb601ef9c112e Mon Sep 17 00:00:00 2001 From: zhaixian Date: Wed, 15 May 2024 17:43:54 +0800 Subject: [PATCH] fix: fix the build error on tvOS (#54) * fix: fix the build error on tvOS * update: update ci.yml --- .github/workflows/ci.yml | 2 +- .../AliyunpanAuthenticator.swift | 38 ++++++++++++------- Sources/AliyunpanSDK/AliyunpanError.swift | 2 + Tests/AliyunpanSDKTests/MessageTests.swift | 19 +--------- fastlane/Fastfile | 4 ++ 5 files changed, 32 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 816d3fa..deafcd9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,5 +31,5 @@ jobs: - name: Test env: DESTINATION: platform=${{ matrix.destination }} - run: bundle exec fastlane test + run: bundle exec fastlane test_ci diff --git a/Sources/AliyunpanSDK/AliyunpanCredentials/AliyunpanAuthenticator.swift b/Sources/AliyunpanSDK/AliyunpanCredentials/AliyunpanAuthenticator.swift index 0b73105..7691fd9 100644 --- a/Sources/AliyunpanSDK/AliyunpanCredentials/AliyunpanAuthenticator.swift +++ b/Sources/AliyunpanSDK/AliyunpanCredentials/AliyunpanAuthenticator.swift @@ -75,22 +75,29 @@ class AliyunpanAuthenticator: NSObject { func startAuthenticationSession(_ url: URL) async throws -> String { return try await withCheckedThrowingContinuation { continuation in - let session = ASWebAuthenticationSession(url: url, callbackURLScheme: "smartdrive") { url, error in - if let error { - continuation.resume(with: .failure(error)) - return + if #available(iOS 13, macOS 10.15, tvOS 16.0, visionOS 1, *) { + let session = ASWebAuthenticationSession(url: url, callbackURLScheme: "smartdrive") { url, error in + if let error { + continuation.resume(with: .failure(error)) + return + } + + let components = URLComponents(string: url?.absoluteString ?? "") + if let code = components?.queryItems?.first(where: { $0.name == "code" })?.value { + continuation.resume(with: .success(code)) + } else { + continuation.resume(with: .failure(AliyunpanError.AuthorizeError.invalidCode)) + } } - - let components = URLComponents(string: url?.absoluteString ?? "") - if let code = components?.queryItems?.first(where: { $0.name == "code" })?.value { - continuation.resume(with: .success(code)) - } else { - continuation.resume(with: .failure(AliyunpanError.AuthorizeError.invalidCode)) +#if canImport(TVUIKit) +#else + session.presentationContextProvider = self +#endif + DispatchQueue.main.async { + session.start() } - } - session.presentationContextProvider = self - DispatchQueue.main.async { - session.start() + } else { + continuation.resume(with: .failure(AliyunpanError.AuthorizeError.invalidPlatform)) } } } @@ -104,9 +111,12 @@ class AliyunpanAuthenticator: NSObject { } } +#if canImport(TVUIKit) +#else extension AliyunpanAuthenticator: ASWebAuthenticationPresentationContextProviding { @MainActor func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { Platform.mainPresentationAnchor } } +#endif diff --git a/Sources/AliyunpanSDK/AliyunpanError.swift b/Sources/AliyunpanSDK/AliyunpanError.swift index 24e5187..0a92a45 100644 --- a/Sources/AliyunpanSDK/AliyunpanError.swift +++ b/Sources/AliyunpanSDK/AliyunpanError.swift @@ -22,6 +22,8 @@ public struct AliyunpanError { case accessTokenInvalid /// 授权 code 错误 case invalidCode + /// 当前平台不支持 + case invalidPlatform } /// 网络层错误 diff --git a/Tests/AliyunpanSDKTests/MessageTests.swift b/Tests/AliyunpanSDKTests/MessageTests.swift index e984f50..8b056a4 100644 --- a/Tests/AliyunpanSDKTests/MessageTests.swift +++ b/Tests/AliyunpanSDKTests/MessageTests.swift @@ -9,25 +9,8 @@ import XCTest @testable import AliyunpanSDK extension AliyunpanError.AuthorizeError: Equatable { - var stringValue: String { - switch self { - case .invalidAuthorizeURL: - return "invalidAuthorizeURL" - case .notInstalledApp: - return "notInstalledApp" - case .authorizeFailed(let error, let errorMsg): - return "authorizeFailed_\(error ?? "")_\(errorMsg ?? "")" - case .qrCodeAuthorizeTimeout: - return "qrCodeAuthorizeTimeout" - case .accessTokenInvalid: - return "accessTokenInvalid" - case .invalidCode: - return "invalidCode" - } - } - public static func == (lhs: AliyunpanError.AuthorizeError, rhs: AliyunpanError.AuthorizeError) -> Bool { - lhs.stringValue == rhs.stringValue + lhs.localizedDescription == rhs.localizedDescription } } diff --git a/fastlane/Fastfile b/fastlane/Fastfile index 66dae8a..dfb9bdc 100644 --- a/fastlane/Fastfile +++ b/fastlane/Fastfile @@ -7,6 +7,10 @@ platform :ios do test(destination: "platform=tvOS Simulator,name=Apple TV") end + lane :test_ci do + test(destination: ENV["DESTINATION"]) + end + lane :test do |options| scan( scheme: "AliyunpanSDK",