0

I am playing youtube video on iPad via webView using this code.

NSString *htmlString = [NSString stringWithFormat:@"<html>\
                        <body>\
                          <div id=\"player\"> </div>\
                          <script>\
                            var tag = document.createElement('script');\
                            tag.src = \"http://www.youtube.com/player_api\";\
                            var firstScriptTag = document.getElementsByTagName('script')[0];\
                            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\
                            var done = false;\
                            var player;\
                            function onYouTubePlayerAPIReady() {\
                               player = new YT.Player('player', {\
                               height: '%i',\
                               width: '%i',\
                               videoId: '%@',\
                               events: {\
                                'onReady': onPlayerReady,\
                                'onStateChange': onPlayerStateChange\
                                }\
                              });\
                            }\
                            function onPlayerReady(evt) {\
                                evt.target.playVideo();\
                            }\
                            function onPlayerStateChange(evt) {\
                                if(evt.data==0)\
                                {\
                                      window.location=\"http:\\end\";\
                                 }\
                             }\
                             function resizePlayer(width,height)\
                             {\
                                   player.setSize(width, height);\
                             }\
                           </script>\
                        </body>\
                     </html>",
                        height,width, videoID];

The problem is that when I'm installing my app via xCode it works fine, but when I'm using ipa file it don't.

Vitalii Boiarskyi
  • 1,363
  • 2
  • 10
  • 25

2 Answers2

1

The problem you are having could depend on the specific device and iOS versions (there are subtle differences in UIWebView implementations), more than on using an ipa file.

So, you might try and reproduce the environment where the UIWebView fails to interpret correctly your HTML snippet. Also, don't forget to define webView:didFailLoadWithError: and give a look at a way to intercept javascript errors inside of UIWebViews and display them on the console.

Hope this helps.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
0

I have found what caused my problem.

To allow my application to catch a moment when video reaches the end, I wrote Javascript code to redirect page to another URL. Then I implemented method from WebViewProtocol -(BOOL)webView:(UIWebView *) shouldStartLoadWithREquest:(NSURLRequest *) navigationType:(UIWebViewNavigationType)navigationType. The main idea was to close view when it tries to go to some special link, but I've made one mistake that for some unknown reason didn't raised when I launch my app from XCode.

Code with error:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType
{
     NSString *url = [[request URL] absoluteString];
if ([url isEqualToString:@"http://youtube.com/end"])
{
    [self onCloseVideo];
    [self unsubscribe];
    return = NO;
} 
//here on else I had to return YES but I didn't
}

Code without error:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType
{
     NSString *url = [[request URL] absoluteString];
     BOOL shouldStartRequest = YES;
if ([url isEqualToString:@"http://youtube.com/end"])
{
    [self onCloseVideo];
    shouldStartRequest = NO;
}               

return shouldStartRequest;
}
Vitalii Boiarskyi
  • 1,363
  • 2
  • 10
  • 25