How to make an progress bar for an NSURLConnection when downloading a file?

In your NSURLConnection delegate, implement something like this to find out the total content length. The server has to support this, but it will most likely work fine with static content: – (void)connection: (NSURLConnection*) connection didReceiveResponse: (NSHTTPURLResponse*) response { statusCode_ = [response statusCode]; if (statusCode_ == 200) { download_.size = [response expectedContentLength]; } } And … 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

Managing multiple asynchronous NSURLConnection connections

I track responses in an CFMutableDictionaryRef keyed by the NSURLConnection associated with it. i.e.: connectionToInfoMapping = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); It may seem odd to use this instead of NSMutableDictionary but I do it because this CFDictionary only retains its keys (the NSURLConnection) whereas NSDictionary copies its keys (and NSURLConnection doesn’t support copying). Once … 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

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

Sending an HTTP POST request on iOS

The following code describes a simple example using POST method.(How one can pass data by POST method) Here, I describe how one can use of POST method. 1. Set post string with actual username and password. NSString *post = [NSString stringWithFormat:@”Username=%@&Password=%@”,@”username”,@”password”]; 2. Encode the post string using NSASCIIStringEncoding and also the post string you need … Read more

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

I have solved it with adding some key in info.plist. The steps I followed are: Opened my Project target’s info.plist file Added a Key called NSAppTransportSecurity as a Dictionary. Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES as like following image. Clean the Project and Now Everything is Running fine … Read more

CFNetwork SSLHandshake failed iOS 9

iOS 9 and OSX 10.11 require TLSv1.2 SSL for all hosts you plan to request data from unless you specify exception domains in your app’s Info.plist file. The syntax for the Info.plist configuration looks like this: <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yourserver.com</key> <dict> <!–Include to allow subdomains–> <key>NSIncludesSubdomains</key> <true/> <!–Include to allow insecure HTTP requests–> <key>NSExceptionAllowsInsecureHTTPLoads</key> … Read more