Swift 3 WebView

前端 未结 5 1818
走了就别回头了
走了就别回头了 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:25

    An alternative to WebView:

    import SafariServices
    
    let webViewController = SFSafariViewController(url: url)
    webViewController.preferredControlTintColor = .primaryTintColor
    present(webViewController, animated: true, completion: nil)
    
    0 讨论(0)
  • 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

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="15G1004" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_39682344" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <webView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="xr8-sF-fyd">
                                <color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            </webView>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="xr8-sF-fyd" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="A3n-Xx-65O"/>
                            <constraint firstAttribute="trailing" secondItem="xr8-sF-fyd" secondAttribute="trailing" id="OZa-E2-XIe"/>
                            <constraint firstItem="xr8-sF-fyd" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="Tn3-gw-V4p"/>
                            <constraint firstItem="xr8-sF-fyd" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="tYl-t1-QdU"/>
                        </constraints>
                    </view>
                    <connections>
                        <outlet property="webView" destination="xr8-sF-fyd" id="s1G-80-juD"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="136.80000000000001" y="137.18140929535232"/>
        </scene>
    </scenes>
    </document>
    

    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

     <key>NSAppTransportSecurity</key>
     <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
     </dict>
    

    Other

    WKWebView Sample

    0 讨论(0)
  • 2021-02-04 00:27

    class WebView: UIViewController {

    @IBOutlet var myWebView: UIWebView!
    
    var urlValue = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let url = URL (string: urlValue){
            let requestObj = URLRequest(url: url)
            _ = myWebView.loadRequest(requestObj)
        }
    }
    

    }

    0 讨论(0)
  • 2021-02-04 00:29

    For this exact situation, retype your line to:

    yourWebView.loadRequest(URLRequest(url: URL(string: "http://hardwirestudios.com")!))
    

    ... and rename yourWebView variable to one, that you actually use.

    In Swift 3.0, use URL and URLRequest as almost all the NS were dropped.

    0 讨论(0)
  • 2021-02-04 00:39

    A dynamic webview class with loading :

    class WebViewController : UIViewController, UIWebViewDelegate {
    
    var url : String!
    var webView : UIWebView!
    var loadingView : UIActivityIndicatorView!
    
    override func viewDidLoad() {
    
    
        let x = Int(UIScreen.main.bounds.width / 2 - 25)
        loadingView = UIActivityIndicatorView(frame : CGRect(x: x, y: 100, width: 50, height: 50))
        loadingView.activityIndicatorViewStyle = .gray
    
        webView = UIWebView(frame: UIScreen.main.bounds)
        webView.delegate = self
        view.addSubview(webView)
        if let url = URL(string: url) {
            let request = URLRequest(url: url)
            webView.loadRequest(request)
        }else{
    
            self.navigationController?.popViewController(animated: true)
        }
    
        view.addSubview(loadingView)
        loadingView.startAnimating()
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
    
        loadingView.removeFromSuperview()
    }
    
    static func initController() -> WebViewController {
    
        let vc = WebViewController()
        return vc
    }   
    }
    

    ///// use it

     let webvc = WebViewController.initController()
     webvc.url = "http://apple.com"
     self.navigationController?.pushViewController(webvc, animated: true)
    
    0 讨论(0)
提交回复
热议问题