ASWebAuthenticationSession
iOS 사용 시, ASWebAuthenticationSession을 사용하는 것이 권장되는 방법 중 하나입니다.
거래를 생성하고 거래 링크를 얻은 후, 다음과 같은 구현이 권장됩니다:
- 비대면 카드 검증이 포함된 일반적인 흐름에서, API를 통해 생성된 링크로 ASWebAuthenticationSession을 엽니다.
- 앱에 가장 적합한 방식으로 이 열기 동작을 커스터마이징할 수 있습니다.
- URL이 변경되었는지(
redirectUrl로) 감지한 다음 페이지를 닫습니다.
이 흐름을 작동시키려면 다음 단계를 따라야 합니다:
1단계: 결제 인증 컨트롤러 생성
첫 번째 단계는 결제 인증 컨트롤러를 생성하는 것입니다. 이를 위해 IDPayAuthenticationController(또는 원하는 이름)라는 클래스를 생성하세요.
다음으로, 클래스 상단에 AuthenticationServices 프레임워크를 import하세요.
클래스를 NSObject로 선언하고 ASWebAuthenticationPresentationContextProviding 프로토콜을 구현하세요.
결과는 다음과 같아야 합니다:
import AuthenticationServices
class IDPayAuthenticationController: NSObject, ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
if let mainWindow = windowScene.windows.first {
return mainWindow
}
}
return ASPresentationAnchor()
}
}
2단계: 인증 구현
인증을 수행할 파일을 열고 필요한 import 구문을 추가하세요(저희 예시에서는 ContentView.swift에서 진행합니다).
import SwiftUI
import AuthenticationServices
인증 상태를 제어하기 위해 @State 속성을 생성합니다.
@State private var isAuthenticated = false
ContentView 구조체의 body 외부에 IDPayAuthenticationController 클래스의 인스턴스를 생성하세요.
let idPayController = IDPayAuthenticationController()
결제를 검증하려면 authenticatePayment라는 함수를 생성하세요.
func authenticatePayment() {
guard let url = URL(string: "URL_AUTHENTICATION") else { return }
var session: ASWebAuthenticationSession?
session = ASWebAuthenticationSession(url: url, callbackURLScheme: "BUNDLE") { callbackURL, error in
guard callbackURL != nil else {
if let error = error {
return print("Error during authentication: \(error.localizedDescription)")
}
return
}
// Processes the callback URL to check whether authentication succeeded
session?.cancel()
isAuthenticated = true
}
session?.presentationContextProvider = idPayController
session?.prefersEphemeralWebBrowserSession = true
session?.start()
}
URL_AUTHENTICATION URL을 거래에서 받은 인증 URL로, 그리고 callbackURLScheme의 BUNDLE을 거래 생성 시 제공된 리다이렉트로 변경하는 것을 잊지 마세요(앱의 Bundle Identifier를 사용하는 것을 권장합니다).
거래별로 고유한 인증을 보장하려면 prefersEphemeralWebBrowserSession을 true로 설정하는 것이 중요합니다.
올바르게 작동하려면 다음과 같은 몇 가지 권한이 필요합니다:
- 카메라
- 위치 정보
자세한 내용은 공식 ASWebAuthenticationSession 문서를 참고하는 것을 권장합니다.