Swift 3 WebView

前端 未结 5 1819
走了就别回头了
走了就别回头了 2021-02-04 00:02

So I just updated to the new Xcode8 and Swift3 but now my web view does not work. Here is the code I used:

UIWebView.loadRequest(webVie         


        
5条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 00:26

    For iOS 12.x +

    'UIWebView' was deprecated in iOS 12.0: No longer supported; please adopt WKWebView.

    WKWebView Sample

    For iOS <= 11.x

    Using StoryBoard

    ViewController.swift

    import UIKit
    
    class ViewController: UIViewController, UIWebViewDelegate {
    
        @IBOutlet weak var webView: UIWebView!
        override func viewDidLoad() {
            super.viewDidLoad()
            webView.delegate = self
            if let url = URL(string: "http://apple.com") {
                let request = URLRequest(url: url)
                webView.loadRequest(request)
            }
        }
    }
    

    Main.storyboard

    
    
    
        
        
        
    
    
        
        
            
                
                    
                        
                        
                    
                    
                        
                        
                        
                            
                                
                            
                        
                        
                        
                            
                            
                            
                            
                        
                    
                    
                        
                    
                
                
            
            
        
    
    
    

    Result

    Programmatically

    import UIKit
    
    class ViewController: UIViewController, UIWebViewDelegate {
    
        var webView: UIWebView!
        override func viewDidLoad() {
            super.viewDidLoad()
            webView = UIWebView(frame: UIScreen.main.bounds)
            webView.delegate = self
            view.addSubview(webView)
            if let url = URL(string: "http://apple.com") {
                let request = URLRequest(url: url)
                webView.loadRequest(request)
            }
        }
    }
    

    Info.plist

    add in your Info.plist transport security setting

     NSAppTransportSecurity
     
        NSAllowsArbitraryLoads
        
     
    

    Other

    WKWebView Sample

提交回复
热议问题