objective-cpdfhttp-redirectdownloadshort-url

Objective-C Trying to download PDF that is from a short url


I have been trying to get this to work without having it load on the web view first and getting the absoluteString from that so I can download the URL. I have tried many shortURL solutions and they never fully load the URL. They always give me the URL that is not the final url and does not that the PDF url. Any help would be amazing. I am trying to download the PDF when the app first opens or when it checks for updates, but at the time it just gets the short url and I have to wait till the web view is called to get the full url to be able to download the PDF a head of time.


Solution

  • You download the PDF, just like you would download any other file.

    Take a look at NSURLDownload

    - (void)startDownloadingURL:sender
    {
        // Create the request.
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/index.html"]
                                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                 timeoutInterval:60.0];
    
        // Create the download with the request and start loading the data.
    NSURLDownload  *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
        if (!theDownload) {
            // Inform the user that the download failed.
        }
    }
    
    - (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
    {
        NSString *destinationFilename;
        NSString *homeDirectory = NSHomeDirectory();
    
        destinationFilename = [[homeDirectory stringByAppendingPathComponent:@"Desktop"]
            stringByAppendingPathComponent:filename];
        [download setDestination:destinationFilename allowOverwrite:NO];
    }
    
    
    - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
    {
        // Dispose of any references to the download object
        // that your app might keep.
        ...
    
        // Inform the user.
        NSLog(@"Download failed! Error - %@ %@",
              [error localizedDescription],
              [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    }
    
    - (void)downloadDidFinish:(NSURLDownload *)download
    {
        // Dispose of any references to the download object
        // that your app might keep.
        ...
    
        // Do something with the data.
        NSLog(@"%@",@"downloadDidFinish");
    }