加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中UIWebView的使用

发布时间:2020-12-14 06:40:02 所属栏目:百科 来源:网络整理
导读: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))))sel

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",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,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("无效操作")
        }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读