Skip to content

Commit 512b754

Browse files
committed
iOS changes
1 parent 14c745c commit 512b754

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

ios/Promise.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77

88
import Foundation
9-
109
// Original Implementation: https://github.com/expo/expo/blob/168ee43f71f005baa11edf98e518593443e1807a/packages/expo-modules-core/ios/Swift/Promise.swift
1110

1211
struct Promise {

ios/WebAuthSession.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
// Created by Michael Lee on 23/3/2022.
66
//
77

8+
//https://github.com/expo/expo/blob/main/packages/expo-web-browser/ios/WebAuthSession.swift
9+
810
import AuthenticationServices
911
import Foundation
1012

11-
@available(iOS 12.0, *)
1213
private class PresentationContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
1314
func presentationAnchor(for _: ASWebAuthenticationSession) -> ASPresentationAnchor {
1415
return UIApplication.shared.keyWindow ?? ASPresentationAnchor()
1516
}
1617
}
1718

18-
@available(iOS 12.0, *)
1919
internal final class WebAuthSession {
2020
var authSession: ASWebAuthenticationSession?
2121
var promise: Promise?
@@ -26,10 +26,10 @@ internal final class WebAuthSession {
2626
// It must be initialized before hand as `ASWebAuthenticationSession` holds it as a weak property
2727
private var presentationContextProvider = PresentationContextProvider()
2828

29-
init(authUrl: URL, redirectUrl: URL, options: AuthSessionOptions) {
29+
init(authUrl: URL, redirectUrl: URL?, options: AuthSessionOptions) {
3030
authSession = ASWebAuthenticationSession(
3131
url: authUrl,
32-
callbackURLScheme: redirectUrl.scheme,
32+
callbackURLScheme: redirectUrl?.scheme,
3333
completionHandler: { callbackUrl, error in
3434
self.finish(with: [
3535
"type": callbackUrl != nil ? "success" : "cancel",

ios/WebBrowser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ class NativeWebBrowser: NSObject {
3434
}
3535

3636
@objc(openAuthSessionAsync:withRedirectUrl:withOptionsDict:withResolver:withRejector:)
37-
func openAuthSessionAsync(_ authUrlStr: NSString, redirectUrlStr: NSString, optionsDict: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
37+
func openAuthSessionAsync(_ authUrlStr: NSString, redirectUrlStr: NSString?, optionsDict: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
3838
guard
3939
let authUrl = URL(string: authUrlStr as String),
40-
let redirectUrl = URL(string: redirectUrlStr as String),
4140
let options = try? JSONDecoder().decode(AuthSessionOptions.self, from: JSONSerialization.data(withJSONObject: optionsDict))
4241
else {
4342
reject(ReactNativeWebBrowserErrorCode, "Invalid Argument: authUrl or redirectUrl or options is invalid.", ReactNativeWebBrowserError.invalidArgument("authUrl or redirectUrl or options"))
@@ -50,6 +49,7 @@ class NativeWebBrowser: NSObject {
5049
reject(ReactNativeWebBrowserErrorCode, "AuthSession is already opened.", ReactNativeWebBrowserError.alreadyOpen)
5150
return
5251
}
52+
let redirectUrl = URL(string: redirectUrlStr as? String ?? "")
5353
self.currentAuthSession = WebAuthSession(authUrl: authUrl, redirectUrl: redirectUrl, options: options)
5454
self.currentAuthSession?.open(promise)
5555
}

ios/WebBrowserOptions.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Created by Michael Lee on 23/3/2022.
66
//
77

8+
//https://github.com/expo/expo/blob/main/packages/expo-web-browser/ios/WebBrowserOptions.swift
9+
810
import Foundation
911
import SafariServices
1012

@@ -19,6 +21,12 @@ class WebBrowserOptions: Codable {
1921

2022
var controlsColor: CodableColor?
2123

24+
var presentationStyle:PresentationStyle = .overFullScreen
25+
26+
struct AuthSessionOptions:Codable{
27+
var preferEphemeralSession: Bool = false
28+
}
29+
2230
required init(from decoder: Decoder) throws {
2331
let container = try decoder.container(keyedBy: CodingKeys.self)
2432
if let readerMode = try container.decodeIfPresent(Bool.self, forKey: .readerMode) {
@@ -83,6 +91,45 @@ enum DismissButtonStyle: String, Codable {
8391
}
8492
}
8593

94+
internal enum PresentationStyle: String ,Codable{
95+
case fullScreen
96+
case pageSheet
97+
case formSheet
98+
case currentContext
99+
case overFullScreen
100+
case overCurrentContext
101+
case popover
102+
case none
103+
case automatic
104+
105+
func toPresentationStyle() -> UIModalPresentationStyle {
106+
switch self {
107+
case .fullScreen:
108+
return .fullScreen
109+
case .pageSheet:
110+
return .pageSheet
111+
case .formSheet:
112+
return .formSheet
113+
case .currentContext:
114+
return .currentContext
115+
case .overFullScreen:
116+
return .overFullScreen
117+
case .overCurrentContext:
118+
return .overCurrentContext
119+
case .popover:
120+
return .popover
121+
case .none:
122+
return .none
123+
case .automatic:
124+
if #available(iOS 13.0, *) {
125+
return .automatic
126+
}
127+
// default prior iOS 13
128+
return .fullScreen
129+
}
130+
}
131+
}
132+
86133
// https://github.com/expo/expo/blob/168ee43f71f005baa11edf98e518593443e1807a/packages/expo-modules-core/ios/Swift/Arguments/Convertibles.swift
87134
// https://stackoverflow.com/questions/50928153/make-uicolor-codable
88135

ios/WebBrowserSession.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Created by Michael Lee on 23/3/2022.
66
//
77

8+
// https://github.com/expo/expo/blob/main/packages/expo-web-browser/ios/WebBrowserSession.swift
9+
810
import Foundation
911
import SafariServices
1012

@@ -27,7 +29,6 @@ internal class WebBrowserSession: NSObject, SFSafariViewControllerDelegate {
2729

2830
super.init()
2931
viewController.delegate = self
30-
3132
// By setting the modal presentation style to OverFullScreen, we disable the "Swipe to dismiss"
3233
// gesture that is causing a bug where sometimes `safariViewControllerDidFinish` is not called.
3334
// There are bugs filed already about it on OpenRadar.
@@ -55,6 +56,11 @@ internal class WebBrowserSession: NSObject, SFSafariViewControllerDelegate {
5556
func safariViewControllerDidFinish(_: SFSafariViewController) {
5657
finish(type: "cancel")
5758
}
59+
60+
// MARK: - UIAdaptivePresentationControllerDelegate
61+
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
62+
finish(type: "cancel")
63+
}
5864

5965
// MARK: - Private
6066

0 commit comments

Comments
 (0)