Skip to content

Instantly share code, notes, and snippets.

@tigi44
Created May 20, 2020 08:48
Show Gist options
  • Save tigi44/cfd6c0fc466dfef29122c62522317968 to your computer and use it in GitHub Desktop.
Save tigi44/cfd6c0fc466dfef29122c62522317968 to your computer and use it in GitHub Desktop.
[iOS, Objective-c] File Download (HTTP Response attachment)
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
BOOL sIsFileDownload = NO;
NSURL *sURL = navigationResponse.response.URL;
NSString *sMIMEType = navigationResponse.response.MIMEType;
NSString *sFileName = navigationResponse.response.suggestedFilename;
NSDictionary *sHeaders = ((NSHTTPURLResponse *)navigationResponse.response).allHeaderFields;
NSString *sContentDisposition = sHeaders[@"Content-Disposition"];
BOOL sIsAttachment = [sContentDisposition containsString:@"attachment"];
BOOL sIsSpreadSheetType = [sMIMEType containsString:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
if (sIsSpreadSheetType || sIsAttachment)
{
NSURLRequest *sRequest = [NSURLRequest requestWithURL:sURL];
[self downloadTaskFromRequest:sRequest fileName:sFileName];
sIsFileDownload = YES;
}
else
{
sIsFileDownload = NO;
}
decisionHandler(sIsFileDownload ? WKNavigationResponsePolicyCancel : WKNavigationResponsePolicyAllow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment