What does NSURLConnection’s error code “-1009” mean?

Since the error returned should be within the NSURLErrorDomain, the code -1009 means: NSURLErrorNotConnectedToInternet Returned when a network resource was requested, but an internet connection is not established and cannot be established automatically, either through a lack of connectivity, or by the user’s choice not to make a network connection automatically.

NSURLConnection sendAsynchronousRequest:queue:completionHandler: making multiple requests in a row?

There’s lots of ways you can do this depending on the behavior you want. You can send a bunch of asynchronous requests at once, track the number of requests that have been completed, and do something once they’re all done: NSInteger outstandingRequests = [requestsArray count]; for (NSURLRequest *request in requestsArray) { [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] … Read more

Changing the userAgent of NSURLConnection

Obj-C: NSString* userAgent = @”My Cool User Agent”; NSURL* url = [NSURL URLWithString:@”http://whatsmyuseragent.com/”]; NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease]; [request setValue:userAgent forHTTPHeaderField:@”User-Agent”]; NSURLResponse* response = nil; NSError* error = nil; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; Swift: let userAgent = “My Cool User Agent” if let url = NSURL(string: “http://whatsmyuseragent.com/”) { let request … Read more

NSURLConnection Using iOS Swift

Check Below Codes : 1. SynchronousRequest Swift 1.2 let urlPath: String = “YOUR_URL_HERE” var url: NSURL = NSURL(string: urlPath)! var request1: NSURLRequest = NSURLRequest(URL: url) var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)! var err: NSError println(response) var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary println(“Synchronous\(jsonResult)”) Swift 2.0 + … Read more

NSURLConnection timeout?

You can specify a timeout in your NSURLRequest object. One way to do this is to construct it via the requestWithURL:cachePolicy:timeoutInterval: method. (You can pass in the default NSURLRequestUseProtocolCachePolicy cachePolicy parameter if you don’t want to worry about that part.) The timeout is a floating-point value in seconds, as are basically all time intervals in … Read more

how to use sendAsynchronousRequest:queue:completionHandler:

PArt 1: NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] > 0 && error == nil) [delegate receivedData:data]; else if ([data length] == 0 && error == nil) [delegate emptyReply]; else if (error != … Read more

How To Check Response.statusCode in sendSynchronousRequest on Swift

you pass in a reference to response so it is filled THEN you check the result and cast it to a HTTPResponse as only http responses declare the status code property. let urlPath: String = “http://www.google.de” var url: NSURL = NSURL(string: urlPath) var request: NSURLRequest = NSURLRequest(URL: url) var response: NSURLResponse? var data = NSURLConnection.sendSynchronousRequest(request, … Read more

NSURLConnection and Basic HTTP Authentication in iOS

I’m using an asynchronous connection with MGTwitterEngine and it sets the authorization in the NSMutableURLRequest (theRequest) like so: NSString *authStr = [NSString stringWithFormat:@”%@:%@”, [self username], [self password]]; NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding]; NSString *authValue = [NSString stringWithFormat:@”Basic %@”, [authData base64EncodingWithLineLength:80]]; [theRequest setValue:authValue forHTTPHeaderField:@”Authorization”]; I don’t believe this method requires going through the challenge loop but … Read more