Skip to content

Commit

Permalink
Handle Javascript-based navigation for iFrame player in WKWebView (#425)
Browse files Browse the repository at this point in the history
The web iFrame embedded player may use Javascript to open URLs for some UI elements. WKWebView ignores this navigation by default, so this change adds the logic to link to the YT app.
  • Loading branch information
Eric Vargas committed Feb 10, 2021
1 parent da1ae1b commit 35159c1
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Classes/YTPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
NSString static *const kYTPlayerStaticProxyRegexPattern = @"^https://content.googleapis.com/static/proxy.html(.*)$";
NSString static *const kYTPlayerSyndicationRegexPattern = @"^https://tpc.googlesyndication.com/sodar/(.*).html$";

@interface YTPlayerView() <WKNavigationDelegate>
@interface YTPlayerView() <WKNavigationDelegate, WKUIDelegate>

@property (nonatomic) NSURL *originURL;
@property (nonatomic, weak) UIView *initialLoadingView;
Expand Down Expand Up @@ -540,6 +540,20 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio
}
}

#pragma mark - WKUIDelegate

- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures {
// Handle navigation actions initiated by Javascript.
[[UIApplication sharedApplication] openURL:navigationAction.request.URL
options:@{}
completionHandler:nil];
// Returning nil results in canceling the navigation, which has already been handled above.
return nil;
}

#pragma mark - Private methods

- (NSURL *)originURL {
Expand Down Expand Up @@ -770,6 +784,7 @@ - (BOOL)loadWithPlayerParams:(NSDictionary *)additionalPlayerParams {

[self.webView loadHTMLString:embedHTML baseURL: self.originURL];
self.webView.navigationDelegate = self;
self.webView.UIDelegate = self;

if ([self.delegate respondsToSelector:@selector(playerViewPreferredInitialLoadingView:)]) {
UIView *initialLoadingView = [self.delegate playerViewPreferredInitialLoadingView:self];
Expand Down

0 comments on commit 35159c1

Please sign in to comment.