在shouldStartLoadWithRequest方法中修改URL请求并在Webview中显示 [英] Modify URL request in shouldStartLoadWithRequest method and displaying in Webview
问题描述
我有一个webview和3个网址。
I have one webview and 3 url.
因此,当应用程序启动时,我在webview中显示URL1。
So when application starts i am showing URL1 in webview.
现在,当我选择webview的任何部分时,它将重定向到URL2。
Now when i select any portion of webview it will redirect to URL2.
但我只想从URL2获取一些数据而不想向用户显示它。
But only i want to fetch some data from URL2 and dont want to show it to user.
我可以通过使用shouldStartLoadWithRequest:方法返回NO。
Which i can able to do by using shouldStartLoadWithRequest: method with return NO.
但是现在我需要在我的Webview中显示从URL2收到的数据的URL 3.
but Now i need to show URL 3 with data received from URL2 in my Webview.
但它没有显示任何内容,我该怎么办?
But it is not showing anything ,how can i do it ?
为此我使用以下代码
-(void)viewDidLoad
{
//Normal showing of URL1 in webview
}
- (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
{
if(selectedDataExist){
//get data from URL2
//Make New URL3 string
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:myNewUrlString]]];
return NO;
}
else
{
//by default URL1 comes
return YES;
}
推荐答案
我这样做了:
我试图通过GET方法发送信息,所以我将所有请求的url的最后10个字符(在我的情况下)子字符串,如果它没有GET方法,它创建一个新的请求将GET方法附加到url并返回NO,下次调用此函数时它将具有GET方法,因此它将继续。
i was trying to send a info via GET method so i substring the last 10 characters (in my case) of any url requested, if it doesn't have the GET method, it make a new request appending the GET method to the url and return NO, next time this function get called it will have the GET method so it will continue.
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *urls = [request.URL absoluteString];
NSString *code = [urls substringFromIndex: [urls length] - 10];
if (![code isEqualToString:@"?iphone=si"]) {
NSURL *nueva = [NSURL URLWithString:[NSString stringWithFormat:@"%@?iphone=si",urls]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:nueva];
[self.mywebview loadRequest:requestObj];
return NO;
}
return YES;
}
这篇关于在shouldStartLoadWithRequest方法中修改URL请求并在Webview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!