13

I am trying to open the following url in UIWebView but it fails to load whereas changing it to:

 http://www.google.com

works fine.

The url that I want to load is:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@%@%@",@"http://m.forrent.com/search.php?address=",[[bookListing objectForKey:@"Data"] objectForKey:@"zip"],@"&beds=&baths=&price_to=0#{\"lat\":\"0\",\"lon\":\"0\",\"distance\":\"25\",\"seed\":\"1622727896\",\"is_sort_default\":\"1\",\"sort_by\":\"\",\"page\":\"1\",\"startIndex\":\"0\",\"address\":\"",[[bookListing objectForKey:@"Data"] objectForKey:@"zip"],@"\",\"beds\":\"\",\"baths\":\"\",\"price_to\":\"0\"}"]]]];

UPDATE:

I have purposely escaped the double quotes otherwise it gives me an error. I checked the url by opening in my browser (on laptop) and it works perfectly fine:

The url in browser:

http://m.forrent.com/search.php?address=92115&beds=&baths=&price_to=0#{%22lat%22:%220%22,%22lon%22:%220%22,%22distance%22:%2225%22,%22seed%22:%221622727896%22,%22is_sort_default%22:%221%22,%22sort_by%22:%22%22,%22page%22:%221%22,%22startIndex%22:%220%22,%22address%22:%2292115%22,%22beds%22:%22%22,%22baths%22:%22%22,%22price_to%22:%220%22}
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
iDev
  • 2,163
  • 10
  • 39
  • 64
  • If you want to find why your code is not working, I suggest you break that one liner into: A string, a url, a url request, and finally your `loadRequest:` method. Print your complete string and show it to us. – ohr Oct 08 '12 at 19:14
  • You are assuming that the product of the string formatter is a valid URL. Try logging it out and pasting into a browser or just getting some eyeballs on it. – Warren Burton Oct 08 '12 at 19:15
  • Please check the UPDATE section – iDev Oct 08 '12 at 19:23
  • Do it in simple steps. First create the string. Then the URL. Then the request. This will show, which step fails - I guess it's the URL which will turn out as nil. – Eiko Oct 08 '12 at 19:47

2 Answers2

21

Your line of code looks convoluted, but basically it is a very simple one.

You should breakup this code from a one liner to multiple lines that are more readable. That will also allow you to log and check the URL you actually created, like so:

NSLog(@"My url: %@", urlString);

Update: I see you added the full url. Webview indeed fails to load that url (UIWebkit error 101).

The part of the url that causes the problem is the '#' character and dictionary that follows in the params. You should url encode that part of the url.

Try this:

NSString *address = @"http://m.forrent.com/search.php?";
NSString *params1 = @"address=92115&beds=&baths=&price_to=0";

// URL encode the problematic part of the url.
NSString *params2 = @"#{%22lat%22:%220%22,%22lon%22:%220%22,%22distance%22:%2225%22,%22seed%22:%221622727896%22,%22is_sort_default%22:%221%22,%22sort_by%22:%22%22,%22page%22:%221%22,%22startIndex%22:%220%22,%22address%22:%2292115%22,%22beds%22:%22%22,%22baths%22:%22%22,%22price_to%22:%220%22}";
params2 = [self escape:params2];

// Build the url and loadRequest
NSString *urlString = [NSString stringWithFormat:@"%@%@%@",address,params1,params2];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];

The escaping method I used:

- (NSString *)escape:(NSString *)text
{
    return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                        (__bridge CFStringRef)text, NULL,
                                                                        (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                        kCFStringEncodingUTF8);
}
disco crazy
  • 31,313
  • 12
  • 80
  • 83
PostPCDev
  • 684
  • 5
  • 8
  • To catch "fail to load" errors of a web view, set your controller as the delegate of the web view and implement: -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error – PostPCDev Oct 08 '12 at 20:47
  • in what part of code do u add this? or do u create a new function ? thanks – Alberto Acuña Jul 07 '18 at 07:43
1

I would try encoding all of the key/value items in your url. Specifically the curly braces ({}) and the hash (#) symbols may be causing a problem.

bgolson
  • 3,460
  • 5
  • 24
  • 41