UIWebView - Add an extra parameter to the url request

被刻印的时光 ゝ 提交于 2020-01-05 16:55:07

问题


Hi I have a url say http://www.foobar.com

[webView loadRequest:[NSURLRequest requestWithURL:appURL 
                cachePolicy:NSURLRequestUseProtocolCachePolicy
                timeoutInterval:20.0
                ]];

Now when this url is formed i can set the urlString to have webtype=iphone

But for every single request that comes from there onwards I need to add webType=iphone to back of the string.

I figured there is some way using

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

But didnt get any solution through it yet... any help


回答1:


To do this, either subclass UIWebView with a simple wrapper that only implements a custom loadRequest or subclass NSURLRequest to change the customURL as JNK said above. It's probably easier to do the later tho.




回答2:


just create a custiom method returning the cusom url:

- (NSURL *)customURLWithPramString:(NSString *)pramString{
    return [NSURL URLWithString:[NSString stringWithFormat:@"http://www.foobar.com?%@&webtype=iphone",pramString]];
}

Then you can just go: [webView loadRequest:[NSURLRequest requestWithURL:[self customURLWithPramString:@"name=123&age=123"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]];

where you pass "name=123&age=123" in this case



来源:https://stackoverflow.com/questions/4380203/uiwebview-add-an-extra-parameter-to-the-url-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!