【问题标题】:UIWebView load local html with cocos2d-js animaitonUIWebView 用 cocos2d-js 动画加载本地 html
【发布时间】:2015-04-09 01:48:04
【问题描述】:

我有一些关于 UIWebView 使用 cocos2d-js 动画加载本地 html 的问题。 事情是这样的: 文件夹的结构,如:

资源

 | -> www

          |->1
               | -> 1.html,1.js
               | -> res
                        | -> Cowboy.ExportJson,Cowboy.plist,Cowboy.png

。当我在 xcode 项目中拖动文件夹时,我选择“创建文件夹引用”,

我想加载“1.html”,它会调用1.js然后显示动画,但是牛仔资源无法加载成功,我 这是代码:

我捕获了异常,例如没有获取资源...

我尝试了 3 种方法来加载到我的 webView:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"www/1/1" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",[url URLByDeletingLastPathComponent]);
[self.webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];


NSURL *requestUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"html" inDirectory:@"www/1"]];
[self.webView loadRequest:[NSURLRequest requestWithURL:requestUrl]];


 NSURL *url = [[NSBundle mainBundle] URLForResource:@"www/1/1" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSString *baseURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www/1"];   self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:baseURL]];

购买它们没有用。谁能帮我 。谢谢

【问题讨论】:

    标签: ios objective-c uiwebview cocos2d-js


    【解决方案1】:

    首先,检查目标构建阶段 -> 复制捆绑资源

    其次,我觉得应该是 NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"html"];

    第三,使用 stringWithContentsOfFile 而不是 stringWithContentsOfURL

    应该是这样的-

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:nil];
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多