UIWebView - Add an extra parameter to the url requ

2019-07-25 11:45发布

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

2条回答
够拽才男人
2楼-- · 2019-07-25 12:29

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

查看更多
祖国的老花朵
3楼-- · 2019-07-25 12:34

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.

查看更多
登录 后发表回答