【问题标题】:Modify URL request in shouldStartLoadWithRequest method and displaying in Webview在 shouldStartLoadWithRequest 方法中修改 URL 请求并在 Webview 中显示
【发布时间】:2012-04-25 11:40:08
【问题描述】:

我有一个 webview 和 3 个 url。

所以当应用程序启动时,我会在 webview 中显示 URL1。

现在,当我选择 webview 的任何部分时,它将重定向到 URL2。

但只有我想从 URL2 获取一些数据并且不想将其显示给用户。

我可以通过使用 shouldStartLoadWithRequest: 返回 NO 的方法来做到这一点。

但现在我需要在我的 Webview 中显示带有从 URL2 接收的数据的 URL 3。

但它没有显示任何东西,我该怎么办?

为此,我使用以下代码

-(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;
}

【问题讨论】:

    标签: iphone ipad uiwebview


    【解决方案1】:

    我这样做了:

    我试图通过 GET 方法发送信息,所以我将任何请求的 url 的最后 10 个字符(在我的情况下)子字符串,如果它没有 GET 方法,它会发出一个新请求,将 GET 方法附加到url 并返回 NO,下次调用此函数时,它将具有 GET 方法,因此它将继续。

     -(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;
    }
    

    【讨论】:

      【解决方案2】:

      我就是这样做的

                      NSURL *LocalUrl = [[NSURL alloc] initWithString:[newUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]];
      
                      NSURLRequest *objNSURLRequest;
      
                      objNSURLRequest = [NSURLRequest requestWithURL:LocalUrl];
      
                      [yourwebview loadRequest:ObjNSURLRequest];
      
                      [LocalUrl release];
      
                      return NO;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-17
        • 1970-01-01
        • 2011-03-13
        • 1970-01-01
        相关资源
        最近更新 更多