kodeco.com Forums

Using NSURLProtocol with Swift

In this NSURLProtocol tutorial you will learn how to work with the URL loading system and URL schemes to add custom behavior to your apps.

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2292-using-nsurlprotocol-with-swift

As per the CustomHTTPProtocol implementation here and the documentation here, it seems that all the calls to the NSURLProtocolClient ( atleast the below) should be run on the same clientThread as where the NSURLProtocol startLoading is called. If we don’t, we could get into issues as described in this thread

One of the crash stack trace is below:

0 libsystem_platform.dylib 0x00000001812217d0 _os_lock_corruption_abort + 0
1 libsystem_platform.dylib 0x00000001812217d0 _os_lock_corruption_abort + 0
2 CFNetwork 0x0000000181ba3308 CFURLProtocol_NS::forgetProtocolClient() + 36
3 MyApp 0x0000000100324004 0x1000f4000 + 2293764
4 MyApp 0x000000010024b0a0 0x1000f4000 + 1405088
5 CFNetwork 0x0000000181cba1d8 __51-[NSURLSession delegate_task:didCompleteWithError:]_block_invoke170 + 72
6 Foundation 0x0000000181f64334 NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 16
7 Foundation 0x0000000181eb7100 -[NSBlockOperation main] + 96
8 Foundation 0x0000000181ea7348 -[__NSOperationInternal _start:] + 604
9 Foundation 0x0000000181f66728 __NSOQSchedule_f + 224
10 libdispatch.dylib 0x00000001810115f0 _dispatch_client_callout + 16
11 libdispatch.dylib 0x000000018101d634 _dispatch_queue_drain + 864
12 libdispatch.dylib 0x00000001810150f4 _dispatch_queue_invoke + 464
13 libdispatch.dylib 0x000000018101f504 _dispatch_root_queue_drain + 728
14 libdispatch.dylib 0x000000018101f224 _dispatch_worker_thread3 + 112
15 libsystem_pthread.dylib 0x0000000181225470 _pthread_wqthread + 1092
16 libsystem_pthread.dylib 0x0000000181225020 start_wqthread + 4

Would appreciate your thoughts on this.


Is there a Swift 3.0 version of the code used for this tutorial? Thanks!

GitHub - yarshure/NSURLProtocolExample: NSURLProtocol- Apple’s URL Loading System update to Swift 3

Thanks for this amazing Introduction to a cool concept. I thought many times of skipping reading it in the beginning but now I am glad I dint. Keep up the good work.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]