iOS - 由于解析错误,UIWebView无法正常工作

Abd*_*tou 24 html iphone uiwebview ipad ios

我想在我的应用中播放Youtube视频.所以,我写了下面的代码

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]; 
Run Code Online (Sandbox Code Playgroud)

videoView是一个UIWebView.这工作得很好,直到以后,视图没有显示任何内容.只是一个空白的白色视图.我得到了这个日志:

*** WebKit discarding exception: <NSRangeException> *** -[__NSCFString substringToIndex:]: Range or index out of bounds
Run Code Online (Sandbox Code Playgroud)

Ric*_*ier 52

使用:

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

代替:

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

在此处找到:https://devforums.apple.com/message/705665#705665


Kep*_*pPM 12

也许我将youtube网址翻译为正确格式的方法可以帮助您:

+ (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;
}
Run Code Online (Sandbox Code Playgroud)


Abd*_*tou 5

我使用以下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>
Run Code Online (Sandbox Code Playgroud)

它与使用短网址和YouTube网址无关.它可能使用embed标签并使用application/x-shockwave-flash类型.它现在在iOS 6 GM上完美运行.