Skip to content

Commit

Permalink
Added -playerViewPreferredInitialLoadingView:
Browse files Browse the repository at this point in the history
I've added this feature because sometimes it takes a lot of time before YouTube iframe player become ready, especially in bad connection situations. During that time users are not notified the view is actually loading or not. This can be handy.

Perhaps I should add a code to remove self.initialLoadingView when
-webView:didFailLoadWithError: as well to prevent the loading view from being displayed forever.
  • Loading branch information
Ono Masashi committed Dec 14, 2015
1 parent 324ffc3 commit dd01965
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Classes/YTPlayerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ typedef NS_ENUM(NSInteger, YTPlayerError) {
*/
- (nonnull UIColor *)playerViewPreferredWebViewBackgroundColor:(nonnull YTPlayerView *)playerView;

/**
* Callback invoked when initially loading the YouTube iframe to the webview to display a custom
* loading view while the player view is not ready. This loading view will be dismissed just before
* -playerViewDidBecomeReady: callback is invoked. The loading view will be automatically resized
* to cover the entire player view.
*
* The default implementation does not display any custom loading views so the player will display
* a blank view with a background color of (-playerViewPreferredWebViewBackgroundColor:).
*
* Note that the custom loading view WILL NOT be displayed after iframe is loaded. It will be
* handled by YouTube iframe API. This callback is just intended to tell users the view is actually
* doing something while iframe is being loaded, which will take some time if users are in poor networks.
*
* @param playerView The YTPlayerView instance where the error has occurred.
* @return A view object that will be displayed while YouTube iframe API is being loaded.
* Pass nil to display no custom loading view. Default implementation returns nil.
*/
- (nullable UIView *)playerViewPreferredInitialLoadingView:(nonnull YTPlayerView *)playerView;

@end

/**
Expand Down
17 changes: 16 additions & 1 deletion Classes/YTPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@

@interface YTPlayerView()

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

@end

Expand Down Expand Up @@ -533,6 +534,9 @@ - (void)notifyDelegateOfYouTubeCallbackUrl: (NSURL *) url {
}

if ([action isEqual:kYTPlayerCallbackOnReady]) {
if (self.initialLoadingView) {
[self.initialLoadingView removeFromSuperview];
}
if ([self.delegate respondsToSelector:@selector(playerViewDidBecomeReady:)]) {
[self.delegate playerViewDidBecomeReady:self];
}
Expand Down Expand Up @@ -727,6 +731,17 @@ - (BOOL)loadWithPlayerParams:(NSDictionary *)additionalPlayerParams {
[self.webView setDelegate:self];
self.webView.allowsInlineMediaPlayback = YES;
self.webView.mediaPlaybackRequiresUserAction = NO;

if ([self.delegate respondsToSelector:@selector(playerViewPreferredInitialLoadingView:)]) {
UIView *initialLoadingView = [self.delegate playerViewPreferredInitialLoadingView:self];
if (initialLoadingView) {
initialLoadingView.frame = self.bounds;
initialLoadingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:initialLoadingView];
self.initialLoadingView = initialLoadingView;
}
}

return YES;
}

Expand Down

0 comments on commit dd01965

Please sign in to comment.