Skip to content

Commit

Permalink
Set bundleId of app as the baseURL for the webView. (#371)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Eric Vargas committed May 8, 2020
1 parent e9dbe9e commit a82ce9d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions Classes/YTPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

@interface YTPlayerView() <WKNavigationDelegate>

@property (nonatomic, strong) NSURL *originURL;
@property (nonatomic) NSURL *originURL;
@property (nonatomic, weak) UIView *initialLoadingView;

@end
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a82ce9d

Please sign in to comment.