WKWebView OAuth popup misses window.opener in iOS 17.5+: The Fix You’ve Been Waiting For
Image by Latoria - hkhazo.biz.id

WKWebView OAuth popup misses window.opener in iOS 17.5+: The Fix You’ve Been Waiting For

Posted on

Are you tired of dealing with the frustrating WKWebView OAuth popup issue on iOS 17.5+? You’re not alone! Many developers have been struggling to find a solution to this pesky problem, but fear not, dear reader, for we’ve got the fix you’ve been waiting for.

What’s the Issue?

The WKWebView OAuth popup issue arises when you’re trying to authenticate users using OAuth in a WKWebView on iOS 17.5+. The popup window, which is supposed to open in a new window, fails to set the `window.opener` property, causing the authentication process to break.

Why Does This Happen?

This issue occurs due to a security feature introduced in iOS 17.5+, which restricts the ability of web pages to access the `window.opener` property. This feature, intended to prevent malicious scripts from accessing sensitive information, has an unintended consequence – breaking OAuth authentication in WKWebView.

The Solution

Fear not, dear developer, for we’ve found a solution to this problem. The fix involves using a combination of JavaScript and native iOS code to bypass the security restriction and allow the OAuth popup to set the `window.opener` property correctly.

Step 1: Create a Native Bridge

Create a native iOS class that will act as a bridge between your WKWebView and the OAuth popup. This class will handle the authentication process and set the `window.opener` property correctly.


#import <WebKit/WebKit.h>

@interface OAuthBridge : NSObject <WKScriptMessageHandler>

@end

@implementation OAuthBridge

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"oauth"]) {
        // Handle OAuth authentication
        // Set window.opener property
        [self setWindowOpenerProperty:message.body];
    }
}

- (void)setWindowOpenerProperty:(NSDictionary *)dictionary {
    // Set window.opener property
    NSString *openerScript = [NSString stringWithFormat:@"window.opener = %@", dictionary[@"opener"]];
    [self.webView evaluateJavaScript:openerScript completionHandler:nil];
}

@end

Step 2: Configure WKWebView

Configure your WKWebView to use the native bridge class created in Step 1.


WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
OAuthBridge *bridge = [[OAuthBridge alloc] init];
[configuration.userContentController addScriptMessageHandler:bridge name:@"oauth"];

WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];

Step 3: Handle OAuth Popup

Handle the OAuth popup by injecting a JavaScript script into the web page. This script will communicate with the native bridge class to set the `window.opener` property.


NSString *script = @"javascript:(function() {
    var opener = window.opener;
    if (opener) {
        window.webkit.messageHandlers.oauth.postMessage({opener: opener});
    }
})();
[webView evaluateJavaScript:script completionHandler:nil];

OAuth Flow

Now that you’ve set up the native bridge and configured WKWebView, let’s walk through the OAuth flow:

  1. User initiates OAuth authentication
  2. OAuth popup window opens in WKWebView
  3. JavaScript script injected into popup window communicates with native bridge class
  4. Native bridge class sets `window.opener` property correctly
  5. User authenticates and OAuth popup window closes
  6. WKWebView receives authentication response and redirects user to authenticated page

Conclusion

There you have it, folks! With this solution, you should be able to overcome the WKWebView OAuth popup issue on iOS 17.5+. Remember to carefully follow the instructions and adapt the code to your specific use case.

Troubleshooting Tips

If you’re still experiencing issues, here are some troubleshooting tips:

  • Ensure that you’ve correctly configured the native bridge class and WKWebView.
  • Verify that the JavaScript script is injected correctly into the OAuth popup window.
  • Check the OAuth popup window’s console for any errors or warnings.
  • Test the authentication flow on different iOS versions to ensure compatibility.
iOS Version WKWebView OAuth Popup Issue
iOS 17.5+ Occurs due to security feature
iOS 17.4 and below Does not occur

By following this guide, you should be able to resolve the WKWebView OAuth popup issue on iOS 17.5+ and provide a seamless authentication experience for your users. Happy coding!

References

For more information on WKWebView and OAuth, please refer to the following resources:

  • Apple Developer Documentation: WKWebView
  • OAuth 2.0 Specification

Frequently Asked Question

Get answers to your burning questions about WKWebView OAuth popup and window.opener in iOS 17.5+

What is the issue with WKWebView OAuth popup in iOS 17.5+?

In iOS 17.5+, WKWebView OAuth popup misses window.opener, which leads to authentication failures. This is because iOS 17.5+ introduced a new gesture-based navigation system, which affects the way popups are handled.

Why does window.opener become null in WKWebView OAuth popup?

When the OAuth popup is displayed in WKWebView, the window.opener property is set to null due to the new gesture-based navigation system. This prevents the popup from accessing the parent window, causing authentication failures.

How can I fix the WKWebView OAuth popup issue in iOS 17.5+?

One possible solution is to use the SFSafariViewController instead of WKWebView, as it handles popups differently. Another approach is to implement a custom authentication flow using a different OAuth strategy, such as Authorization Code Flow with PKCE.

Is there a way to detect when the WKWebView OAuth popup is closed in iOS 17.5+?

Yes, you can use the WKWebView’s decidePolicyForNavigationAction delegate method to detect when the popup is closed. By monitoring the navigation actions, you can identify when the popup is dismissed and take appropriate action.

Will Apple fix the WKWebView OAuth popup issue in future iOS updates?

Apple is aware of the issue, and it’s possible that a fix will be included in a future iOS update. However, until then, developers need to find workarounds or alternative solutions to ensure seamless authentication experiences for their users.

Leave a Reply

Your email address will not be published. Required fields are marked *