swift中UIWebView的使用

https://github.com/potato512/SYSwiftLearning




// 实例化
self.webview = UIWebView(frame: CGRectMake(0.0, CGRectGetHeight(segment.frame), CGRectGetWidth(self.view.bounds), (CGRectGetHeight(self.view.bounds) - CGRectGetHeight(segment.bounds))))
self.view.addSubview(self.webview!)
        
self.webview!.backgroundColor = UIColor.clearColor()
        
// 使用"|"无效,应改成[xx,xx,..]
// self.webview!.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin
self.webview!.autoresizingMask = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleBottomMargin]
self.webview!.scrollView.scrollEnabled = true
        
// 设置代理对象(注意添加协议及实现代理方法)
self.webview!.delegate = self
// 释放,内存管理
deinit
{
        self.webview?.delegate = nil
        self.webview?.loadHTMLString("", baseURL: nil)
        self.webview?.stopLoading()
        self.webview?.removeFromSuperview()
        self.webview = nil
        
        NSURLCache.sharedURLCache().removeAllCachedResponses()
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
// MARK: - UIWebViewDelegate
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print("\(request),\(navigationType)")
        
        // 用于控制子链接是否可以打开跳转
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        
        return true
}
    
func webViewDidStartLoad(webView: UIWebView) {
        
        // 加载开始
        print("1 webViewDidStartLoad")
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
    
func webViewDidFinishLoad(webView: UIWebView) {
        // 加载结束(加载成功)
        print("2 webViewDidFinishLoad")
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
    
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        // 加载结束(加载失败)
        print("3 webView error = \(error)")
        
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}

let item01 = UIBarButtonItem(title: "stop", style: UIBarButtonItemStyle.Done, target: self, action: Selector("buttonClick:"))
item01.tag = 1000
let item02 = UIBarButtonItem(title: "start", style: UIBarButtonItemStyle.Done, target: self, action: Selector("buttonClick:"))
item02.tag = 2000
self.navigationItem.rightBarButtonItems = [item01, item02]
// MARK: - 响应事件
func buttonClick(sender:UIBarButtonItem)
{
        let index = sender.tag
        if 1000 == index
        {
            // 停止
            if (self.webview != nil)
            {
                if (self.webview!.loading)
                {
                    self.webview!.stopLoading()
                    
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                }
            }
        }
        else if 2000 == index
        {
            // 开始
            if (self.webview != nil)
            {
                if (!self.webview!.loading)
                {
                    let request = NSURLRequest(URL: self.url)
                    self.webview!.loadRequest(request)
                    
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
                }
            }
        }
}


// 多按钮视图控件(控制webview的后退,前进,或重新加载属性)
let segment = UISegmentedControl(items: ["GoBack","GoForward","Reload"])
self.view.addSubview(segment)
segment.momentary = true
segment.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0)
segment.addTarget(self, action: Selector("segmentValueChange:"), forControlEvents: UIControlEvents.ValueChanged)
// MARK: - segmentValueChange
func segmentValueChange(sender:UISegmentedControl)
{
        let index = sender.selectedSegmentIndex;
        switch (index)
        {
            case 0:
                let isGoBack = self.webview!.canGoBack
                if isGoBack
                {
                    self.webview?.goBack()
                }

            case 1:
                
                let isGoForward = self.webview!.canGoForward
                if isGoForward
                {
                    self.webview?.goForward()
                }

            case 2:
                let isReload = self.webview!.loading
                if !isReload
                {
                    self.webview?.reload()
                    
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
                }
            default : print("无效操作")
        }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值