Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 35159c1

Browse files
author
Eric Vargas
authored
Handle Javascript-based navigation for iFrame player in WKWebView (#425)
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.
1 parent da1ae1b commit 35159c1

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Classes/YTPlayerView.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
NSString static *const kYTPlayerStaticProxyRegexPattern = @"^https://content.googleapis.com/static/proxy.html(.*)$";
6363
NSString static *const kYTPlayerSyndicationRegexPattern = @"^https://tpc.googlesyndication.com/sodar/(.*).html$";
6464

65-
@interface YTPlayerView() <WKNavigationDelegate>
65+
@interface YTPlayerView() <WKNavigationDelegate, WKUIDelegate>
6666

6767
@property (nonatomic) NSURL *originURL;
6868
@property (nonatomic, weak) UIView *initialLoadingView;
@@ -540,6 +540,20 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio
540540
}
541541
}
542542

543+
#pragma mark - WKUIDelegate
544+
545+
- (WKWebView *)webView:(WKWebView *)webView
546+
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
547+
forNavigationAction:(WKNavigationAction *)navigationAction
548+
windowFeatures:(WKWindowFeatures *)windowFeatures {
549+
// Handle navigation actions initiated by Javascript.
550+
[[UIApplication sharedApplication] openURL:navigationAction.request.URL
551+
options:@{}
552+
completionHandler:nil];
553+
// Returning nil results in canceling the navigation, which has already been handled above.
554+
return nil;
555+
}
556+
543557
#pragma mark - Private methods
544558

545559
- (NSURL *)originURL {
@@ -770,6 +784,7 @@ - (BOOL)loadWithPlayerParams:(NSDictionary *)additionalPlayerParams {
770784

771785
[self.webView loadHTMLString:embedHTML baseURL: self.originURL];
772786
self.webView.navigationDelegate = self;
787+
self.webView.UIDelegate = self;
773788

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

0 commit comments

Comments
 (0)