Wednesday, May 1, 2013

NSURLResponse suggestedFilename example ios


Returns a suggested filename for the response data.
- (NSString *)suggestedFilename
Return Value of [NSURLResponse suggestedFilename]
A suggested filename for the response data.
Discussion of [NSURLResponse suggestedFilename]
The method tries to create a filename using the following, in order:
  1. A filename specified using the content disposition header.
  2. The last path component of the URL.
  3. The host of the URL.
If the host of URL can't be converted to a valid filename, the filename “unknown” is used.
In most cases, this method appends the proper file extension based on the MIME type. This method will always return a valid filename regardless of whether or not the resource is saved to disk.
Example of [NSURLResponse suggestedFilename]
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
    // store the response information
    self.response = aResponse;

    // Check for bad connection
    if ([aResponse expectedContentLength] < 0)
        NSString *reason = [NSString stringWithFormat:@"Invalid URL [%@]", self.urlString];
        DELEGATE_CALLBACK(dataDownloadFailed:, reason);
        [connection cancel];
        [self cleanup];

    if ([aResponse suggestedFilename])
        DELEGATE_CALLBACK(didReceiveFilename:, [aResponse suggestedFilename]);
Example of [NSURLResponse suggestedFilename]
- (NSString *)total {
NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSString *fileName = [response suggestedFilename];
NSLog(@"Value : %@", fileName);

return fileName;