Resources

View as Markdown

Download files from web chat to iOS

For iOS 14.5 and onward, use delegate methods to handle download requests in the WKWebView.

Here is an example in Objective C.

// Permit download action
(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler
{
    //The download action only supports iOS 14.5+ version.
    if (@available(iOS 14.5, *)) {
        if (navigationAction.shouldPerformDownload) {
            decisionHandler(WKNavigationActionPolicyDownload);
            return;
        }
    }
}

// Set download delegate
- (void)webView:(WKWebView *)webView navigationAction:(WKNavigationAction *)navigationAction didBecomeDownload:(WKDownload *)download API_AVAILABLE(macos(11.3), ios(14.5))
{
    // Here need to set the download delete to the proper recieve, thus when download finished, we can receive the callbacks from the webvew.
    if (download) {
        download.delegate = self;
    }
}

// Process download delegate callbacks
- (void)download:(WKDownload *)download decideDestinationUsingResponse:(NSURLResponse *)response suggestedFilename:(NSString *)suggestedFilename completionHandler:(void (^)(NSURL * _Nullable destination))completionHandler API_AVAILABLE(ios(14.5));
{
    //Create a local file download directory,and the file path in that directory.
    NSString *tmpDir = NSTemporaryDirectory();
    NSString *uuidStr = [[NSUUID UUID] UUIDString];
    NSURL *tmpDirURL = [NSURL fileURLWithPath:tmpDir];
    NSURL *tmpUUIDURL = [tmpDirURL URLByAppendingPathComponent:uuidStr];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] createDirectoryAtURL:tmpUUIDURL withIntermediateDirectories:NO attributes:nil error:&error]) {
        NSLog(@"creating directory failed: %@", error.localizedDescription);
        completionHandler(nil);
        return;
    }
    self.localFileDownloadURL = [tmpUUIDURL URLByAppendingPathComponent:suggestedFilename];
    completionHandler(self.localFileDownloadURL);
}
- (void)downloadDidFinish:(WKDownload *)download  API_AVAILABLE(ios(14.5)){
    dispatch_async(dispatch_get_main_queue(), ^{
        // When file downloaded, using the UIActivityViewController to open it for the next step usage.
        if (!self.localFileDownloadURL) {
            return;
        }
        UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self.localFileDownloadURL] applicationActivities:nil];
        activityVC.popoverPresentationController.sourceView = self.view;
        activityVC.popoverPresentationController.sourceRect = self.view.frame;
        activityVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
        [self presentViewController:activityVC animated:YES completion:nil];
    });
}
- (void)download:(WKDownload *)download didFailWithError:(NSError *)error resumeData:(nullable NSData *)resumeData
API_AVAILABLE(ios(14.5)){
    NSLog(@"wkwebview download failed");
}