在UIWebView中修改加载请求之前的URL [英] Modify URL Before Loading Request in UIWebView

查看:222
本文介绍了在UIWebView中修改加载请求之前的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 UIWebView 从我的网络应用程序创建了一个原生应用程序。



我的webapp是一个基本的CMS一些静态页面,一些类别页面和一些详细页面。



现在我需要检查请求是来自网络应用还是来自原生应用。

为此,我有两种可能:



第一个解决方案:修改URL并检查服务器端的URL:

  @IBOutlet var WebView:UIWebView! 

override func viewDidLoad(){
super.viewDidLoad()

loadWebPage()
}

func loadWebPage ){
let url = NSURL(string:http:// localhost:8888?apptype = native)
let request = NSURLRequest(URL:url!)
WebView.loadRequest )
}

当我检查服务器端,如果参数存在于第一页它会工作,但是当我切换到其他页面(类别等)条件失败。



第二个解决方案:我尝试设置cookie:

  @IBOutlet var WebView:UIWebView! 

override func viewDidLoad(){
super.viewDidLoad()

loadWebPage()
}

func loadWebPage ){
let url = NSURL(string:http:// localhost:8888)
let request = NSURLRequest(URL:url!)

var cookies = NSHTTPCookieStorage。 sharedHTTPCookieStorage()。cookies as! [NSHTTPCookie]

var reqCookies:[AnyObject] = []

for a Cookie in cookies {

reqCookies + = [aCookie]

}

var headers = NSHTTPCookie.requestHeaderFieldsWithCookies(reqCookies)

WebView.loadRequest(request)
}

但是如何在响应中添加这个示例中的Cookie,以便我可以在服务器端检索它们?



有没有其他解决方案或如何修改我现有的解决方案?

解决方案

c $ c> UIWebViewDelegate 方法 webView:shouldStartLoadWithRequest:navigationType:



当用户在webview中点击链接时,将调用此代理方法。将 request.url NSURLRequest 传递给委托方法并创建一个新的 NSURLRequest ,并附加到 request.url 属性后面的?apptype = native b $ b

I created a native app from my web app using UIWebView.

My webapp is a basic CMS with some static pages, some category pages and and some detail pages.

Now I need to check if the request comes from the web app or the native app.

To do this I have two possibilities:

First solution: Modify URL and check URL on server side:

@IBOutlet var WebView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    loadWebPage()
}

func loadWebPage() {
    let url = NSURL(string: "http://localhost:8888?apptype=native")
    let request = NSURLRequest(URL: url!) 
    WebView.loadRequest(request)
}

When I check on server side if the the parameter exists on the first page it works, but when I switch to other pages (categories etc.) the condition fails.

Second solution: I tried to set cookies:

@IBOutlet var WebView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    loadWebPage()
}

func loadWebPage() {
    let url = NSURL(string: "http://localhost:8888")
    let request = NSURLRequest(URL: url!)

    var cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as! [NSHTTPCookie]

    var reqCookies:[AnyObject] = []

    for aCookie in cookies {

        reqCookies += [aCookie]

    }

    var headers = NSHTTPCookie.requestHeaderFieldsWithCookies(reqCookies)

    WebView.loadRequest(request)
}

But how do I add cookies in this example in the response so I can retrieve them on server side?

Are there any other solutions or how can I modify my existing ones?

解决方案

Implement the UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:.

When the user taps a link in the webview, this delegate method is called. Take the request.url of NSURLRequest passed into the delegate method and create a new NSURLRequest with ?apptype=native appended to the request.url property.

这篇关于在UIWebView中修改加载请求之前的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆