废话不多说,直接上代码
注意:只能在真机跑,不能进行模拟器调试
代码:
local webView1 = ccexp.WebView:create()
webView1:setContentSize(cc.size(1280,720))
webView1:setScalesPageToFit(true)
-- webView1:loadHTMLString(true)
webView1:setPosition(cc.p(640,360))
webView1:loadURL("http://www.baidu.com")
self:addChild(webView1)
webView1:setOnShouldStartLoading(function(sender,url)
print("onWebView1ShouldStartLoading,url is ", url)
return true
end)
webView1:setOnDidFinishLoading(function(sender,url)
print("onWebView1DidFinishLoading url is " , url)
end)
webView1:setOnDidFailLoading(function(sender,url)
print("onWebView1DidFailLoading url is " , url)
end)
webView1:reload()
因为苹果不支持http协议,所以在真机运行时需要稍微修改xcode设置
添加下面设置
如果是加载本地的html文件,用下面代码
local webView1 = ccexp.WebView:create()
webView1:setContentSize(cc.size(1280,720))
webView1:setScalesPageToFit(true)
-- webView1:loadHTMLString(true)
webView1:setPosition(cc.p(640,360))
local url = "8/8.html"
url = cc.FileUtils:getInstance():fullPathForFilename(url)
webView1:loadURL(url)
self:addChild(webView1)
webView1:setOnShouldStartLoading(function(sender,url)
print("onWebView1ShouldStartLoading,url is ", url)
return true
end)
webView1:setOnDidFinishLoading(function(sender,url)
print("onWebView1DidFinishLoading url is " , url)
end)
webView1:setOnDidFailLoading(function(sender,url)
print("onWebView1DidFailLoading url is " , url)
end)
webView1:reload()