iOS - UIWebView not working due to parsing error

南笙酒味 提交于 2019-11-30 11:24:42

问题


I want to play a Youtube video in my app. So, I wrote the following code

NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

NSString* html = [NSString stringWithFormat:embedHTML, videoURL, self.view.frame.size.width, self.view.frame.size.height];  

[videoView loadHTMLString:html baseURL:nil]; 

The videoView is a UIWebView. This was working just fine until later, the view shows nothing. Just a blank white view. And I got this log:

*** WebKit discarding exception: <NSRangeException> *** -[__NSCFString substringToIndex:]: Range or index out of bounds

回答1:


Use:

http://www.youtube.com/v/XXXXXXX

instead of:

http://www.youtube.com/watch?v=XXXXXXX

Found here: https://devforums.apple.com/message/705665#705665




回答2:


Maybe my method for translating youtube URL to correct format will help you:

+ (NSString*)getYTUrlStr:(NSString*)url
{   
    if (url == nil)
        return nil;

    NSString *retVal = [url stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"v/"];

    NSRange pos=[retVal rangeOfString:@"version"];
    if(pos.location == NSNotFound)
    {
        retVal = [retVal stringByAppendingString:@"?version=3&hl=en_EN"];
    }
    return retVal;
}



回答3:


I solved it using the following HTML:

<!doctype html>\
<html>\
<style>body{padding:0;margin:0;}</style>\
<iframe width=\"320\" height=\"367\" src=\"http://www.youtube.com/embed/rVd0XWALswk?rel=0\" frameborder=\"0\" allowfullscreen></iframe>\
</html>

It has nothing to do with using short URL nor the YouTube URL. It's probably using the embed tag and using the type application/x-shockwave-flash. It's now working perfectly fine on iOS 6 GM.




回答4:


Chances are, with that error, there's an array somewhere that you're going through, but at some point you call some method that ends up trying to reach something that's beyond the array.

For example, code like this would produce an out-of-bounds error:

NSArray *array = [[NSArray alloc] initWithObjects:objInPositionZero, objInPositionOne, objInPositionTwo, nil];
NSObject *obj;

for (int i=0; i<3, i++)
{
    obj = [array objectAtIndex: (i+6)
}
//Do something with obj.

would produce an NSRangeException, because the program is trying to return an item from an index that doesn't actually exist in the array.

So check through all your arrays and make sure that's not happening.



来源:https://stackoverflow.com/questions/12399241/ios-uiwebview-not-working-due-to-parsing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!