Created
May 20, 2020 08:48
-
-
Save tigi44/cfd6c0fc466dfef29122c62522317968 to your computer and use it in GitHub Desktop.
[iOS, Objective-c] File Download (HTTP Response attachment)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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