UIWebView + custom URLProtocol crashes on redirects in ios 9

I am working on a hybrid application that has client certificate SSL authentication. As client certificate authentication is broken in WKWebView (http://www.openradar.me/26344026), the authentication is implemented using UIWebView and custom URLProtocol. The URLProtocol intercepts URL requests from the web view, loads them via URLSession and forwards the responses to the web view.



However, in iOS 9 the UIWebView often crashes on redirects.



Here's the delegate method that forwards the redirect to protocol's client:



public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void) {

if (logActions) {

print("REDIRECT:", response)

}

completionHandler(request)

__dispatch_async(DispatchQueue.main) {

//loader is the custom URLProtocol

loader.client?.urlProtocol(loader, wasRedirectedTo: request, redirectResponse: response)

}

}



The crash log is



2017-05-18 14:18:28.707 mPOS0.01[269:12139] NSURLConnection ordering violation: didFinishLoading to be scheduled before didReceiveResponse

/BuildRoot/Library/Caches/com.apple.xbs/Sources/WebCore/WebCore-7601.1.46.145/platform/ios/wak/WKView.mm:409 void WKViewAddSubview(WKViewRef, WKViewRef): invalid parameter

2017-05-18 14:18:28.726 mPOS0.01[269:12209] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSSetM addObject:]: object cannot be nil'

*** First throw call stack:

(0x23e9f91b 0x2363ae17 0x23db8efb 0x2757e2e9 0x2816c391 0x281abc8f 0x2816bd81 0x278e6c31 0x27581d63 0x27581a65 0x275b6191 0x276fe54d 0x275b60a3 0x2439daff 0x2445c8b7 0x1cc7b6b 0x1ccf719 0x2438eb07 0x23daec61 0x2438e9ef 0x2438e8b9 0x2438e751 0x23e61dff 0x23e619ed 0x23e5fd5b 0x23daf229 0x23daf015 0x2757b8cf 0x23bd385b 0x23bd37cf 0x23bd1724)

libc++abi.dylib: terminating with uncaught exception of type NSException



WebThread (18)#0 0x23b2ac5c in __pthread_kill ()

#1 0x23bd4732 in pthread_kill ()

#2 0x23abf0ac in abort ()

#3 0x23616ae4 in abort_message ()

#4 0x2362f69e in default_terminate_handler() ()

#5 0x2363b0b0 in _objc_terminate() ()

#6 0x2362ce16 in std::__terminate(void (*)()) ()

#7 0x2362c5f4 in __cxa_throw ()

#8 0x2363aeea in objc_exception_throw ()

#9 0x23db8efa in -[__NSSetM addObject:] ()

#10 0x2757e2e8 in -[WAKView addSubview:] ()

#11 0x2816c390 in -[WebFrameView(WebInternal) _setDocumentView:] ()

#12 0x281abc8e in -[WebFrameView(WebInternal) _makeDocumentViewForDataSource:] ()

#13 0x2816bd80 in WebFrameLoaderClient::transitionToCommittedForNewPage() ()

#14 0x278e6c30 in WebCore::FrameLoader::transitionToCommitted(WebCore::CachedPage*) ()

#15 0x27581d62 in WebCore::FrameLoader::commitProvisionalLoad() ()

#16 0x27581a64 in WebCore::DocumentLoader::finishedLoading(double) ()

#17 0x275b6190 in WebCore::CachedResource::checkNotify() ()

#18 0x276fe54c in WebCore::CachedRawResource::finishLoading(WebCore::SharedBuffer*) ()

#19 0x275b60a2 in WebCore::SubresourceLoader::didFinishLoading(double) ()

#20 0x2439dafe in ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke ()

#21 0x2445c8b6 in ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 ()

#22 0x01cc7b6a in _dispatch_client_callout ()

#23 0x01ccf718 in _dispatch_block_invoke ()

#24 0x2438eb06 in RunloopBlockContext::_invoke_block(void const*, void*) ()

#25 0x23daec60 in CFArrayApplyFunction ()

#26 0x2438e9ee in RunloopBlockContext::perform() ()

#27 0x2438e8b8 in MultiplexerSource::perform() ()

#28 0x2438e750 in MultiplexerSource::_perform(void*) ()

#29 0x23e61dfe in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()

#30 0x23e619ec in __CFRunLoopDoSources0 ()

#31 0x23e5fd5a in __CFRunLoopRun ()

#32 0x23daf228 in CFRunLoopRunSpecific ()

#33 0x23daf014 in CFRunLoopRunInMode ()

#34 0x2757b8ce in RunWebThread(void*) ()

#35 0x23bd385a in _pthread_body ()

#36 0x23bd37ce in _pthread_start ()

#37 0x23bd1724 in thread_start ()



Everything is fine in iOS 10.

Is there anyone who has encountered the problem before and knows how to solve it?