From a82ce9d12e9b243c83205b524bb87ad6ec160c39 Mon Sep 17 00:00:00 2001 From: Eric Vargas Date: Thu, 7 May 2020 22:07:28 -0700 Subject: [PATCH] Set bundleId of app as the baseURL for the webView. (#371) * Set bundleId of app as the baseURL for the webView. Lazy load the originURL and always set it to the bundleId of the app. Prefix it with http:// if not the webView will not load correctly. * Overwrite Youtube's iFrame origin attribute We always want the origin attribute from the iFrame API (https://developers.google.com/youtube/player_parameters#origin) to be consistent with the webView.baseURL. This means we ignore what a user might set as origin in the playerVars. --- Classes/YTPlayerView.m | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Classes/YTPlayerView.m b/Classes/YTPlayerView.m index 2be469d..379c147 100644 --- a/Classes/YTPlayerView.m +++ b/Classes/YTPlayerView.m @@ -64,7 +64,7 @@ @interface YTPlayerView() -@property (nonatomic, strong) NSURL *originURL; +@property (nonatomic) NSURL *originURL; @property (nonatomic, weak) UIView *initialLoadingView; @end @@ -538,6 +538,14 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio #pragma mark - Private methods +- (NSURL *)originURL { + if (!_originURL) { + NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier]; + _originURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", bundleId]]; + } + return _originURL; +} + /** * Private method to handle "navigation" to a callback URL of the format * ytplayer://action?data=someData @@ -707,20 +715,16 @@ - (BOOL)loadWithPlayerParams:(NSDictionary *)additionalPlayerParams { } [playerParams setValue:playerCallbacks forKey:@"events"]; - - if ([playerParams objectForKey:@"playerVars"]) { - NSMutableDictionary *playerVars = [[NSMutableDictionary alloc] init]; - [playerVars addEntriesFromDictionary:[playerParams objectForKey:@"playerVars"]]; - - if (![playerVars objectForKey:@"origin"]) { - self.originURL = [NSURL URLWithString:@"about:blank"]; - } else { - self.originURL = [NSURL URLWithString: [playerVars objectForKey:@"origin"]]; - } - } else { - // This must not be empty so we can render a '{}' in the output JSON - [playerParams setValue:[[NSDictionary alloc] init] forKey:@"playerVars"]; + + NSMutableDictionary *playerVars = [[playerParams objectForKey:@"playerVars"] mutableCopy]; + if (!playerVars) { + // playerVars must not be empty so we can render a '{}' in the output JSON + playerVars = [NSMutableDictionary dictionary]; } + // We always want to ovewrite the origin to self.originURL, not just for + // the webView.baseURL + [playerVars setObject:self.originURL.absoluteString forKey:@"origin"]; + [playerParams setValue:playerVars forKey:@"playerVars"]; // Remove the existing webview to reset any state [self.webView removeFromSuperview]; @@ -764,9 +768,10 @@ - (BOOL)loadWithPlayerParams:(NSDictionary *)additionalPlayerParams { [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *embedHTML = [NSString stringWithFormat:embedHTMLTemplate, playerVarsJsonString]; + [self.webView loadHTMLString:embedHTML baseURL: self.originURL]; self.webView.navigationDelegate = self; - + if ([self.delegate respondsToSelector:@selector(playerViewPreferredInitialLoadingView:)]) { UIView *initialLoadingView = [self.delegate playerViewPreferredInitialLoadingView:self]; if (initialLoadingView) {