Reputation: 37581
I'm loading a local html page and within shouldStartLoadWithRequest: with the following url:
... /Library/Application%20Support/CDS/%3Fpid=27ce1ef8-c75e-403b-aea1-db1ae31e05cc/...
within that page if a user clicks on a link to go to an external web site, then they click on the back button my code deals with it with:
if ([self.webView canGoBack])
[self.webView goBack];
However when shouldStartLoadWithRequest: gets called again as a result of calling [self.webView goBack] the URL that gets passed to shouldStartLoadWithRequest: has been changed to:
`... /Library/Application%20Support/CDS/?pid=27ce1ef8-c75e-403b-aea1-db1ae31e05cc/..`.
i.e. the OS has changed the "%3F" within the URL to "?".
I return YES from shouldStartLoadWithRequest: but due to "%3f" turning to "?" has the consequence that didFailLoadWithError: gets called with WebKitErrorDomain 102 and the page fails to load.
The file actually has ? in its name but it is the iOS system calls that convert that into %3F during the process of building up the NSURL object which is passed to UIWebView:loadRequest: as follows
NSURL *fullURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask
appropriateForURL:nil
create: YES
error:&err];
fullURL = [fullURL URLByAppendingPathComponent:folderLocation isDirectory:YES];
fullRUL = [fullURL URLByAppendingPathComponent: pageToLoad isDirectory:NO];
NSURLRequest *requestObj = [NSURLRequest requestWithURL: fullURL];
[self.webView loadRequest:requestObj];
folderLocation is an NSString that contains the ?, the call to URLByAppendingPathComponent automatically converts this into %3F, without that conversion the page load fails.
Has anybody seen this before?
Upvotes: 4
Views: 1674
Reputation: 1
I've solved the same problem in this way:
let baseRequest = "yourURLString"
let URLRequest = NSMutableURLRequest(URL: NSURL(string: baseRequest)!)
The most important thing is to pass the URLRequest as a URL created by a String.
Upvotes: 0
Reputation: 427
Query params must be added with relativeToURL method. Try following code ;
NSURL *baseUrl = [self.baseURL URLByAppendingPathComponent:[NSString stringWithFormat:@"/api_url"]];
NSString *urlString = [[NSString stringWithFormat:@"?param1=%@¶m2=%@", param1, param2] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString relativeToURL:baseUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
Upvotes: 1
Reputation: 1145
I'm not using file url's but I had the same issue. Looks like this is the best way to deal with it.
NSString *newURLString = [[origurl absoluteString] stringByAppendingPathComponent:appendedString];
NSURL *url = [NSURL URLWithString:newURLString];
Upvotes: 1