NSMutableURLRequest not obeying my timeoutInterval

There’s a thread on Apple dev forums discussing this issue. Apparently on iPhone OS, the setter mandates timeoutInterval a minimum of 240 seconds (4 minutes). This only occurs when the postBody is not empty (typically when using a POST request). This seems crazy, but apparently it’s there to make sure requests leave the system even … Read more

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

How to send Asynchronous URL Request?

you can use NSURLConnection class NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; and handle its response and errors using its delegate methods. – (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response – (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data – (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error – (void)connectionDidFinishLoading:(NSURLConnection *)connection You can find implementation of NSURLConnection Apple docs: Using NSURLConnection How … Read more

Post data in Objective C using Json

I think you would be better off using the NSJSONSerialization class like this: NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: email, @”Email”, fname, @”FirstName”, nil]; NSError *error; NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error]; [request setHTTPBody:postData]; Using a dictionary and then converting it to JSON it’s easier than creating it like a string. Good luck! [SWIFT … 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

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

NSURLConnection sendAsynchronousRequest can’t get variable out of closure

This is calling asynchronous function that is using a completion handler block/closure. So, you need to employ the completion handler pattern in your own code. This consists of changing the method return type to Void and adding a new completionHandler closure that will be called when the asynchronous call is done: func post(url: String, info: … Read more

Xml parsing in iOS tutorial [closed]

You can parse XML with below code. But you must need to create classes which you can find below. NSURL *URL = [[NSURL alloc] initWithString:@”http:/sites.google.com/site/iphonesdktutorials/xml/Books.xml”]; NSString *xmlString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL]; NSLog(@”string: %@”, xmlString); NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:xmlString]; NSLog(@”dictionary: %@”, xmlDoc); You need to create this classes for XML parsing with name … Read more