简体   繁体   中英

webView does not load the page

I FOUND THE PROBLEM... I found that the value of berria1 have "\\n " at the end of the url. where he not get this but that's the problem.

I pass very curious thing and which do not find any explanation.

The fact is that in the application, I'm parsing some news and save these in CoreData , then show a list of news in a UITableView and if I click on one of them brings me to a UIWebView in which position the link with the full story. Now comes the weird ...

If I pass to NSURL the variable which I recovery of CoreData which contains the Web address, do not load, if I pass the same direction to a NSString and this NSString to UIWebView , the UIWebView load this normally. The following is my code.

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    self.webView.delegate = self;

    NSString *berria1 = [[NSString alloc]init];
    berria1 = [NSString stringWithFormat:@"%@",self.abisua.noticia];

    NSString *berria2 = @"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";

    NSURL *url = [NSURL URLWithString:berria1];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];

    NSLog(@"El contenido de la noticia 1 es %@", berria1);
    NSLog(@"El contenido de la noticia 2 es %@", berria2);

}

With the NSLog I see that the values of the variables are the same as you see in the picture below.

The value of the two variables in the NSLog

Image - Debug window - URL is nil

That could be happening?

Thank you.

Its not berria1 which holds the URL its berria2.

NSURL *url = [NSURL URLWithString:berria2];

Here you go give it a try the site is pretty slow so it takes a time to load

NSString *berria2 = @"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?%20%20%20%20idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";

NSURL *myUrl = [NSURL URLWithString:berria2];

NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];

[self.webView loadRequest:myRequest];

Try this. I didn't see any code how you load the request to webview. But I added the code with that part. Replace it with your webview.

NSString *myurlString = [NSString stringWithFormat:@"%@",self.abisua.noticia];
NSString *cleanedUrl = [myurlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

NSURL *myUrl = [NSURL URLWithString:cleanedUrl];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];

UIWebView *mywebview = [[UIWebView alloc] init];
[mywebview loadRequest:myRequest];

嗨,我是iOS开发人员的新手,我已经看到了您的代码,因为您向网址添加了第二个字符串,为什么仍然理解,只是尝试添加第二个字符串

NSURL *url = [NSURL URLWithString:berria2];

I finally found and fixed the problem.

The problem was that the variable received whith Core Data and pass to NSString , I do not know how, but the link have added this characters at the end of URL "\\n "

Berria1 variable value in debug area was:

berria1 __NSCFString *  @"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9\n      "  0x0000000126d9e6c0

Then when I passed this value to NSURL and this found these characters, NSURL value pass to nil.

Then I solved this, passing this NSString to a NSMutableString and erasing these characters before pass to NSURL with this code:

NSString *berria1 = self.abisua.noticia;

NSMutableString * miCadena = [NSMutableString stringWithString: berria1];
[miCadena deleteCharactersInRange: [miCadena rangeOfString: @"\n      "]];

Thanks everyone for your help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM