Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在加载webview之前将查询参数嵌入webview的链接页面url?_Ios_Swift_Webview - Fatal编程技术网

Ios 如何在加载webview之前将查询参数嵌入webview的链接页面url?

Ios 如何在加载webview之前将查询参数嵌入webview的链接页面url?,ios,swift,webview,Ios,Swift,Webview,我们需要在每个网页URL中添加查询参数(isfromMobile=true),这是识别请求是否来自后端的移动应用程序所必需的。在第一个页面上加载是非常好的,但是,如果我点击webview中的链接,url会发生变化,这对于后端系统来说是一个全新的url,按照逻辑,移动应用程序也应该在这个url上嵌入查询参数。我尝试用webview的以下委托方法修复相同的问题 func webView(_ webView: UIWebView, shouldStartLoadWith request: URLReq

我们需要在每个网页URL中添加查询参数(isfromMobile=true),这是识别请求是否来自后端的移动应用程序所必需的。在第一个页面上加载是非常好的,但是,如果我点击webview中的链接,url会发生变化,这对于后端系统来说是一个全新的url,按照逻辑,移动应用程序也应该在这个url上嵌入查询参数。我尝试用webview的以下委托方法修复相同的问题

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    request.url = request.url?.getMobileUrl() // this adds the query parameter in url eg : testurl?isFromMobile=true
    return true
}

然而,我观察到请求参数在这个委托中是不可变的,每次创建一个新的请求肯定会增加web URL加载时间,或者我不希望这样。有没有更好的建议。

首先,在苹果文档中搜索时,我发现了很多东西

首先,您使用的是不推荐使用的UIWebview。您应该使用WKWebView。从

重要的 从iOS 8.0和OS X 10.10开始,使用WKWebView向应用程序添加web内容。不要使用UIWebView或WebView

在搜索WKWebView文档时,可以使用一种方法,称为

因此,您可以重写此方法,将url更改为包含(isFromMobile=true),然后使用更新的url调用super方法


希望这能有所帮助。

首先,一定要使用
WKWebView
。然后,您可以点击WKNavigationDelegate
decidePolicyForNavigationAction
方法检查加载的url,如果没有所需的查询参数,则调整url并重新加载:

class ViewController: UIViewController, WKNavigationDelegate  {
    var webView: WKWebView?
    var loadUrl = URL(string: "https://stackoverflow.com/tags?isFromMobile=true")!

    override func viewDidLoad() {
        super.viewDidLoad()
        // .. all your other code here

        // Load first request with initial url
        loadWebPage(url: loadUrl)
    }

    func loadWebPage(url: URL)  {
        var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
    components?.query = nil

        // If already has a query then append a param
        // otherwise create a new one
        if let query = url.query {
            // If isFromMobile param already exists jsut reassign the existing query
            if query.contains("isFromMobile=true") {
                components?.query = query
            } else {
                 components?.query = query + "&isFromMobile=true"
            }
        } else {
            components?.query = "isFromMobile=true"
        }

        let customRequest = URLRequest(url: components!.url!)
        loadUrl = components!.url!
        webView!.load(customRequest)
    }

    // MARK: - WKNavigationDelegate

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        guard let url = navigationAction.request.url else {
            decisionHandler(.cancel)
            return
        }

        // If url changes, cancel current request which has no custom parameters
        // and load a new request with that url with custom parameters
        if url != loadUrl {
            decisionHandler(.cancel)
            loadWebPage(url: url)
         } else {
            decisionHandler(.allow)
         }
     }
 }

谢谢你的回答,但是,我也需要为较低版本提供支持,还需要维护webview堆栈(用户应该能够返回以前的版本)。说真的,您需要支持低于iOS 8的版本吗?我希望我可以只提供高于iOS 8版本的支持。这是一个非常好的方法,但这将开始将isFromMobile=true附加到您网页中的所有url,然后加载您可能不需要的url,如果您只想将此参数附加到主url,此线程可以帮助[链接]检查要将参数附加到哪个URL并不困难:)