UIWebView(本地数据部分)

时间:2022-07-16 04:14:18

创建UIWebView和UISegmentedControl

webView用于显示内容,segmentedControl用于切换读取内容的类型

为了方便起见 用拖拉控件形式布局完界面

/*
使用UIWebView加载本地数据或资源有如下三种方式:
1,使用loadHTMLString方法加载HTML内容
2,使用loadRequest方法加载本地资源(也可用于加载服务器资源)
3,先将内容保存成NSData数据,再使用loadData方法加载
*/
loadTypeSegment.selectedSegmentIndex = 0
typeChange(loadTypeSegment)

读取不同类型的数据

@IBAction func typeChange(sender: UISegmentedControl) {

    let index = sender.selectedSegmentIndex
switch index{
case 0:
let html = "<h1>前往:<a href='http://www.baidu.com'>百度</a></h1>";
webView.loadHTMLString(html,baseURL:nil)
case 1:
let path = NSBundle.mainBundle().pathForResource("22", ofType:"pdf")
let urlStr = NSURL.fileURLWithPath(path!);
print(urlStr)
webView.loadRequest(NSURLRequest(URL:urlStr));
case 2:
let path = NSBundle.mainBundle().pathForResource("22", ofType:"pdf")
let urlStr = NSURL.fileURLWithPath(path!);
let data = NSData(contentsOfURL:urlStr);
webView.loadData(data!, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: urlStr) default:
print("出错了") }
}

来源:http://www.cnblogs.com/spaceID/p/4990635.html