URLresponse is not retrieved after storing in cache using storeCachedResponse

Welcome to the wonderful world of asynchronous caches. NSURLCache is highly asynchronous. Just because you’ve shoved data into it doesn’t mean it is available for retrieval. You have to let the main run loop return before it will be available, and possibly even wait a little while. The failure to return a response immediately after … Read more

NSURLSessionDataTask dataTaskWithURL completion handler not getting called

If you’re going to use the completion block rendition of the data task, rather than specifying a delegate of nil, like this: NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; You should instead instantiate your session using the method that does not take a delegate at all: NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: … Read more

How to programmatically add a proxy to an NSURLSession

It turns out, the dictionary keys you want are the Stream variants, they are the ones that resolve down to “HTTPProxy” and such: NSString* proxyHost = @”myProxyHost.com”; NSNumber* proxyPort = [NSNumber numberWithInt: 12345]; // Create an NSURLSessionConfiguration that uses the proxy NSDictionary *proxyDict = @{ @”HTTPEnable” : [NSNumber numberWithInt:1], (NSString *)kCFStreamPropertyHTTPProxyHost : proxyHost, (NSString *)kCFStreamPropertyHTTPProxyPort … Read more

Showing the file download progress with NSURLSessionDataTask

You need to implement following delegates: <NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate> Also need to create two properties: @property (nonatomic, retain) NSMutableData *dataToDownload; @property (nonatomic) float downloadSize; – (void)viewDidLoad { [super viewDidLoad]; NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]]; NSURL *url = [NSURL URLWithString: @”your url”]; NSURLSessionDataTask *dataTask = … Read more

URLSession.datatask with request block not called in background

If you want downloads to progress after your app is no longer in foreground, you have to use background session. The basic constraints of background sessions are outlined in Downloading Files in Background, and are essentially: Use delegate-based URLSession with background URLSessionConfiguration. Use upload and download tasks only, with no completion handlers. In iOS, Implement … Read more

NSURLSession: How to increase time out for URL requests?

ObjC NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.timeoutIntervalForRequest = 30.0; sessionConfig.timeoutIntervalForResource = 60.0; Swift let sessionConfig = URLSessionConfiguration.default sessionConfig.timeoutIntervalForRequest = 30.0 sessionConfig.timeoutIntervalForResource = 60.0 let session = URLSession(configuration: sessionConfig) What docs say timeoutIntervalForRequest and timeoutIntervalForResource specify the timeout interval for the request as well as the resource. timeoutIntervalForRequest – The timeout interval to use when waiting … Read more

How to send POST and GET request?

Sending POST and GET requests in iOS is quite easy; and there’s no need for an additional framework. POST Request: We begin by creating our POST‘s body (ergo. what we’d like to send) as an NSString, and converting it to NSData. objective-c NSString *post = [NSString stringWithFormat:@”test=Message&this=isNotReal”]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; Next up, … Read more

Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit

All error codes are on “CFNetwork Errors Codes References” on the documentation (link) A small extraction for CFURL and CFURLConnection Errors: kCFURLErrorUnknown = -998, kCFURLErrorCancelled = -999, kCFURLErrorBadURL = -1000, kCFURLErrorTimedOut = -1001, kCFURLErrorUnsupportedURL = -1002, kCFURLErrorCannotFindHost = -1003, kCFURLErrorCannotConnectToHost = -1004, kCFURLErrorNetworkConnectionLost = -1005, kCFURLErrorDNSLookupFailed = -1006, kCFURLErrorHTTPTooManyRedirects = -1007, kCFURLErrorResourceUnavailable = -1008, kCFURLErrorNotConnectedToInternet … Read more