Search code examples
phpcocoansurlconnectionmime

Cocoa - application/force-download and NSURLConnection


In my application, I have to deal with an unfortunate MIME-type, application/force-download. I do know for a fact that a NSURLConnection can be established to download a file if a direct link is given (e.x. http://blah.com/something.zip), but nowhere can I find any success/information on an URL like such http://blah.com/download.php?id=123 which would be a PHP file. Web browsers can certainly execute this function, but how would this be possible with the Cocoa library. Any suggestions/guidelines would be certainly appreciated.

Thanks.


Solution

  • You need to implement a delegate for your NSURLConnection object, and in that delegate implement this method:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    

    From the NSURLReponse argument, you can obtain the MIME type and the suggested file name:

    NSString* type = [response MIMEType];
    NSString* fileName = [response suggestedFilename];
    

    It's then up to you to write your app such that for MIME types of application/download, you take the NSData received via the delegate and save it to disk with the appropriate filename.