Here's my implementation:
- (void)webViewLoadRequestWithSession:(BOOL)session
{
NSURL *url = [NSURL URLWithString:@"https://referalURL.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
if (session == NO)
{
[self.webView loadRequest:request];
}
else
{
NSURLSessionConfiguration *ephemeralConfiguration =
[NSURLSessionConfiguration ephemeralSessionConfiguration];
NSOperationQueue *oQueue = [NSOperationQueue mainQueue];
NSURLSession *ephemeralSession =
[NSURLSession sessionWithConfiguration:ephemeralConfiguration
delegate:self
delegateQueue:oQueue];
NSURLSessionDataTask *task =
[ephemeralSession dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
if (error == nil)
{
NSString *urlString = request.URL.absoluteString;
[self.webView loadRequest:request];
}
else
{
[self showErrorWithTitle:@"Error"
message:error.localizedDescription];
}
}];
[task resume];
}
}
- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
newRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSURLRequest *))completionHandler
{
NSInteger statusCode = response.;
NSLog(@"statusCode: %i",statusCode);
NSDictionary *allHeaderFields = response.allHeaderFields;
NSString *locationHeader = [allHeaderFields objectForKey:@"Location"];
NSLog(@"locationHeader: %@",locationHeader);
NSString *newURLString = request.URL.absoluteString;
NSLog(@"newURLString: %@",newURLString);
completionHandler(request);
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = request.URL;
NSString *scheme = [requestURL.scheme lowercaseString];
BOOL startLoading = [scheme isEqualToString: @"https"];
return startLoading;
}
When calling
[self webViewLoadRequestWithSession:YES];
On iOS 10 the delegate method (willPerformHTTPRedirection) delivers:
Calling the completionHandler, I get the above stated ATS error -1022 in all cases.
On iOS 11 the delegate method (willPerformHTTPRedirection) delivers:
Calling the completionHandler, error == nil and the website will load, yet in some cases the css and some other ressources are missing.
Also, with this implementation of the delegate callback I don't see the https://referalURL.com at all, if I call
[self webViewLoadRequestWithSession:NO];
The requestURL will be https://referalURL.com