Bad URL when requesting with NSURL

NSString *strURL = [loc_address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // requesting weather for this location … NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat: @”http://www.google.com/ig/api?weather=%@”, strURL]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; [req setHTTPMethod:@”POST”]; [req addValue:@”text/xml; charset=utf-8″ forHTTPHeaderField:@”Content-Type”];

Using AFNetworking and HTTP Basic Authentication

Answer updated for AFNetworking 2.x For AFNetworking 2.x: In 2.x, they did away with AFHTTPClient, so you’ll need to extend AFHTTPRequestOperationManager with your own class. Then, you can call that class from other code. For example, here’s a sample class that extends the AFHTTPRequestOperationManager: SBAPIManager.h: #import “AFHTTPRequestOperationManager.h” @interface SBAPIManager : AFHTTPRequestOperationManager – (void)setUsername:(NSString *)username andPassword:(NSString … Read more

UIWebView webpage caching for offline viewing

It sounds like the standard caching is not good enough because you have no control over what will be cached and for how long. The easiest way for solving this is by creating your own caching meganism by overriding the NSURLCache. You can find some documentation about that at http://nshipster.com/nsurlcache/ and a sample at http://github.com/evermeer/EVURLCache … Read more

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

Why my return is nil but if i press the url in chrome/safari, i can get data?

This HTTP server sends a Content-Type = application/x-javascript; charset=GBK header field in the response, therefore you get the correct encoding from the textEncodingName property of the NSURLResponse. This can be converted to a NSStringEncoding. This is just a translation of the solution presented in https://stackoverflow.com/a/19885463/1187415 to Swift, plus some simple error checking: let session = … 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

NSData and Uploading Images via POST in iOS

This code works in my app. If you’re not using ARC you’ll need to modify the code to release anything alloc’ed. // create request NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setHTTPShouldHandleCookies:NO]; [request setTimeoutInterval:30]; [request setHTTPMethod:@”POST”]; // set Content-Type in HTTP header NSString *contentType = [NSString stringWithFormat:@”multipart/form-data; boundary=%@”, boundary]; [request setValue:contentType forHTTPHeaderField: @”Content-Type”]; … 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