@@ -45,9 +45,22 @@ @implementation OIDExternalUserAgentCatalyst {
4545 ASWebAuthenticationSession *_webAuthenticationVC;
4646}
4747
48+ - (nonnull instancetype )init {
49+ self = [super init ];
50+ return self;
51+ }
52+
53+ - (nullable instancetype )initWithPrefersEphemeralSession : (BOOL )prefersEphemeralSession {
54+ self = [self init ];
55+ if (self) {
56+ _prefersEphemeralSession = prefersEphemeralSession;
57+ }
58+ return self;
59+ }
60+
4861- (nullable instancetype )initWithPresentingViewController :
4962 (UIViewController *)presentingViewController {
50- self = [super init ];
63+ self = [self init ];
5164 if (self) {
5265 _presentingViewController = presentingViewController;
5366 }
@@ -71,6 +84,38 @@ - (BOOL)presentExternalUserAgentRequest:(id<OIDExternalUserAgentRequest>)request
7184 return NO ;
7285 }
7386
87+ if (!_presentingViewController) {
88+ // Find presentingViewController
89+ if (@available (iOS 13.0 , *)) {
90+ NSSet <UIWindowScene *> *scenes =
91+ (NSSet <UIWindowScene *> *)[UIApplication sharedApplication ].connectedScenes ;
92+
93+ NSMutableArray <UIWindow *> *windows = [NSMutableArray array ];
94+
95+ for (UIWindowScene *scene in scenes) {
96+ [windows addObjectsFromArray: scene.windows];
97+ }
98+
99+ for (UIWindow *window in windows) {
100+ if (window.isKeyWindow ) { // False if calling before window appears
101+ UIWindow *keyWindow = window;
102+ _presentingViewController = keyWindow.rootViewController ;
103+ break ;
104+ }
105+ }
106+ } else {
107+ // ≤ iOS 12.X
108+ NSArray <UIWindow *> *windows = UIApplication.sharedApplication .windows ;
109+ NSPredicate *keyWindowPredicate = [NSPredicate predicateWithFormat: @" keyWindow == YES " ];
110+ UIWindow *keyWindow = [windows filteredArrayUsingPredicate: keyWindowPredicate].firstObject ;
111+ _presentingViewController = keyWindow.rootViewController ;
112+ }
113+ if (!_presentingViewController) {
114+ // Unable to find a presentingViewController; perhaps because no window is key and visible
115+ return NO ;
116+ }
117+ }
118+
74119 _externalUserAgentFlowInProgress = YES ;
75120 _session = session;
76121 BOOL openedUserAgent = NO ;
0 commit comments