Search code examples
iosobjective-cyoutube-apiyoutube-livestreaming-api

YTPlayer will redirect to OAuth2 URL and shows 'please stand by'


I'm trying to use YTPlayer for played live stream in iOS App.

I use the simple code and only changed 'videoId' to 'w-T2LJ_qLiw' which is live video at YouTube.

But when video loaded, app will open Safari and redirect to URL as following:

https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fwww.youtube.com#rpctoken=840721360&forcesecure=1

and

https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fabc-static%2F_%2Fjs%2Fk%3Dgapi.gapi.en.dPxK-DAj_pE.O%2Fm%3D__features__%2Fam%3DAAQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAItRSTN0fuoBkyFaoHWfzWWLct0BxZgQSQ#parent=https%3A%2F%2Fwww.youtube.com&rpctoken=2021820196

Then video will loaded and played but covered will black block shows 'Please stand by.'

If video is not 'live', video plays well.

I tried it before yesterday and it works well. What happened to YTPlayer? I needs to control video's action like, play, pause, reload by code.
Is there any thing I can work around this?


Solution

  • Although 周義棠's solution may work, it breaks YouTube's Terms of Service. I have modified the handleHttpNavigationToUrl: method to check for these two additional URLs before taking the user out of the UIWebView and into Safari.

    This is my working solution:

    // YTPlayerView.m
    
    // ...
    
    NSString static *const kYTPlayerOAuthRegexPattern = @"^http(s)://accounts.google.com/o/oauth2/(.*)$";
    NSString static *const kYTPlayerStaticProxyRegexPattern = @"^https://content.googleapis.com/static/proxy.html(.*)$";
    
    // ...
    
    - (BOOL)handleHttpNavigationToUrl:(NSURL *) url {
      // Usually this means the user has clicked on the YouTube logo or an error message in the
      // player. Most URLs should open in the browser. The only http(s) URL that should open in this
      // UIWebView is the URL for the embed, which is of the format:
      //     http(s)://www.youtube.com/embed/[VIDEO ID]?[PARAMETERS]
      NSError *error = NULL;
      NSRegularExpression *ytRegex =
          [NSRegularExpression regularExpressionWithPattern:kYTPlayerEmbedUrlRegexPattern
                                                    options:NSRegularExpressionCaseInsensitive
                                                      error:&error];
      NSTextCheckingResult *ytMatch =
          [ytRegex firstMatchInString:url.absoluteString
                            options:0
                              range:NSMakeRange(0, [url.absoluteString length])];
    
      NSRegularExpression *adRegex =
          [NSRegularExpression regularExpressionWithPattern:kYTPlayerAdUrlRegexPattern
                                                    options:NSRegularExpressionCaseInsensitive
                                                      error:&error];
      NSTextCheckingResult *adMatch =
          [adRegex firstMatchInString:url.absoluteString
                            options:0
                              range:NSMakeRange(0, [url.absoluteString length])];
    
      NSRegularExpression *oauthRegex =
          [NSRegularExpression regularExpressionWithPattern:kYTPlayerOAuthRegexPattern
                                                  options:NSRegularExpressionCaseInsensitive
                                                    error:&error];
      NSTextCheckingResult *oauthMatch =
        [oauthRegex firstMatchInString:url.absoluteString
                               options:0
                                 range:NSMakeRange(0, [url.absoluteString length])];
    
      NSRegularExpression *staticProxyRegex =
        [NSRegularExpression regularExpressionWithPattern:kYTPlayerStaticProxyRegexPattern
                                                  options:NSRegularExpressionCaseInsensitive
                                                    error:&error];
      NSTextCheckingResult *staticProxyMatch =
        [[staticProxyRegex firstMatchInString:url.absoluteString
                                      options:0
                                        range:NSMakeRange(0, [url.absoluteString length])];
    
      if (ytMatch || adMatch || oauthMatch || staticProxyMatch) {
        return YES;
      } else {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
      }
    }
    

    I have also proposed this change on the project's GitHub as PR #111.

    EDIT 6/10/15: As of today, this modification has been merged into the codebase.