Cancelling an NSURLRequest - objective-c

Is there a method provided in the native SDK to cancel the URL request made?
I know this method:
[connection cancel]
But I am wondering if we can cancel a request. I only want to use the native SDK and I am not allowed to use the third party libraries.
Also is there a way to track the progress of the download with the native library?

For the first part, you need to call cancel on the NSURLConnection as [connection cancel].
As per NSURLRequest documentation:
NSURLRequest encapsulates two basic data elements of a load request:
the URL to load, and the policy to use when consulting the URL content
cache made available by the implementation.
And as per NSURLConnection documentation
An NSURLConnection object provides support to perform the loading of a
URL request. The interface for NSURLConnection is sparse, providing
only the controls to start and cancel asynchronous loads of a URL
request.
So you are canceling a URL connection and not a URL request.
For the second part, check the NSURLConnectionDownloadDelegate Protocol Reference. It has the following delegate methods for this purpose.
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
– connectionDidResumeDownloading:totalBytesWritten:expectedTotalBytes:
– connectionDidFinishDownloading:destinationURL:

Related

NSURLConnection - Get HTTP status code on request failure

Is there any way I can get HTTP status code after NSURLConnection has failed to request? According to the documentation, I can get the HTTP status code from -connection:didReceiveResponse:. However, in case the request fails, only -connection:didFailedWithError: is called, and by far I haven't found any method to get status code inside this method.
p/s: I'm working on a VERY old, complicated project, and it's very error-prone, so I cannot use AFNetworking or NSURLSession.
As mentioned in the Apple docs for NSURLConnectionDataDelegate Protocol the connection:didReceiveResponse: is called only when sufficient data has been received to create the NSURLResponse object.
While the NSURLResponse object may not be available until later, it is possible for a connection to receive partial data by using the connection:didReceiveData: method. Note that this seems to exclude the HTTP headers and only contain the "data" component of the response. There may be a way to trigger the didReceiveResponse: early if the server knows ahead of time how much data will be transmitted although I am no familiar enough with this to confirm.
Be aware that the response data typically comes in blocks of approximately 1KiB of data each, depending on various network settings such as MTU and how often the server "flush" its output pipe.
Good luck!

Using NSMutableURLRequest and how to manage response/failure

I am creating an iOS app that consumes web services.
I have a class that makes the connections and stores the response in a variable. It also has a status variable where 1 indicates successful connection.
I have set up an NStimer and a function to check when the connection and download is done and if it was successful.
My question is:
Is this a proper way to manage the connection and its outcome?
any suggestions?
Here is the programming guide from Apple Developer website and it describes how to use NSURLConnection delegate. You can manage the received data in connectionDidFinishLoading: method. Notice that using these delegate methods will load data asynchronously. If you want to handle data synchronously, please try sendSynchronousRequest:returningResponse:error:, but this function should never be call in the main thread.

Tracking individual requests to Open Graph through iOS SDK

I'm building an app, essentially a very basic Facebook client using the Facebook SDK for iOS. I have implemented the FBRequestDelegate protocol, and am sending my requests to the Open Graph API in the following way
[facebook requestWithGraphPath:#"me/friends" andDelegate:self]
This works fine, and once the requested content arrives, the SDK calls the delegate method, passing in the data
- (void)request:(FBRequest *)request didLoad:(id)result
My question is, does anyone know the best practice way to use the FBRequest passed into the delegate method to explicitly identify it as coming from my original request?
If I make several requests, how can I differentiate which request the delegate call is related to? I'm using BOOL flags, and only doing one request at a time at the moment, but it's not a good way of doing it.
Any help much appreciated :)
The method requestWithGraphPath returns a FBRequest object which will be also passed to request:didLoad later. So you can simply compare the two objects and determine if they're the same.

GET http request method after redirect when using nsurlconnection SynchronousRequest

Setting NSMutableURLRequest with setHTTPMethod:#"HEAD", as i only want the HEAD information, and using NSURLConnection with sendSynchronousRequest, as i need the thread to block until the info is retrieved, all works well until URL redirect occurs. It appears that synchronousRequest uses a GET request after it encounters a redirect and downloads the content in the background (Not what i want). Need someone to shine some light on this. Either how to do synchronous request of just the HEAD after redirect or how to block NSOperation (where an NSURLConnection is used) until NSURLConnection asynchronously fetches the header information.
Thanks
You need to instantiate an NSURLConnection and use it with a delegate. I thought by default redirects did the right thing, but if not, implement -connection:willSendRequest:redirectResponse: to ensure the new request is a HEAD.
You can either:
Implement this on the main thread
Subclass NSOperation to be "concurrent" and run the connection asynchronously there
Run the runloop until the connection finishes, creating the equivalent of a synchronous connection

NSURLDownload Cancelling

So I am using NSURLDownload in a program, and it requires that I give the user an option to cancel the donwload. I tried using the -cancel method but it only works up until the device starts receiving data at which point i cannot find a way to stop it. any ideas?
I recommend using the ASIHTTPRequest library that provides a lot of nice abstractions and utility methods.
To cancel a request in the library, you can just do [request cancel]! Take a look at "How to use" code sample page.