ios: Fix exporting/downloading to file.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled

This commit is contained in:
2025-11-09 11:54:37 -05:00
parent 61200c4a7d
commit 4447ea63e2
4 changed files with 79 additions and 5 deletions

View File

@@ -1,5 +1,9 @@
#import <UIKit/UIKit.h>
#import <WebKit/WKDownload.h>
#import <WebKit/WKDownloadDelegate.h>
#import <WebKit/WKNavigationAction.h>
#import <WebKit/WKNavigationDelegate.h>
#import <WebKit/WKNavigationResponse.h>
#import <WebKit/WKUIDelegate.h>
#import <WebKit/WKWebView.h>
#import <WebKit/WKWebViewConfiguration.h>
@@ -11,9 +15,10 @@
void tf_run_thread_start(const char* zip_path);
@interface ViewController : UINavigationController <WKUIDelegate, WKNavigationDelegate>
@interface ViewController : UINavigationController <WKUIDelegate, WKNavigationDelegate, WKDownloadDelegate, UIDocumentPickerDelegate>
@property (strong, nonatomic) WKWebView* web_view;
@property bool initial_load_complete;
@property (retain) NSURL* download_url;
@end
static void _start_initial_load(WKWebView* web_view)
@@ -133,6 +138,70 @@ static void _start_initial_load(WKWebView* web_view)
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action) { completionHandler(nil); }]];
[self presentViewController:alertController animated:YES completion:^ {}];
}
- (void)webView:(WKWebView*)webView
decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction
decisionHandler:(void (^)(enum WKNavigationActionPolicy)) decisionHandler
{
decisionHandler(navigationAction.shouldPerformDownload ? WKNavigationActionPolicyDownload : WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView*)webView
decidePolicyForNavigationResponse:(WKNavigationResponse*)navigationResponse
decisionHandler:(void (^)(enum WKNavigationResponsePolicy)) decisionHandler
{
decisionHandler(navigationResponse.canShowMIMEType ? WKNavigationResponsePolicyAllow : WKNavigationResponsePolicyDownload);
}
- (void)webView:(WKWebView*)webView
navigationAction:(WKNavigationAction*)navigationAction
didBecomeDownload:(WKDownload*)download
{
download.delegate = self;
}
- (void)webView:(WKWebView*)webView
navigationResponse:(WKNavigationResponse*)navigationResponse
didBecomeDownload:(WKDownload*)download
{
download.delegate = self;
}
- (void)download:(WKDownload*)download
decideDestinationUsingResponse:(NSURLResponse*)response
suggestedFilename:(NSString*)suggestedFilename
completionHandler:(void (^)(NSURL*))completionHandler
{
self.download_url = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:suggestedFilename];
completionHandler(self.download_url);
}
- (void)downloadDidFinish:(WKDownload*)download
{
UIDocumentPickerViewController* picker = [[UIDocumentPickerViewController alloc] initForExportingURLs:@[self.download_url]];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)download:(WKDownload*)download
didFailWithError:(NSError*)error
resumeData:(NSData*)resumeData
{
tf_printf("didFailWithError:%s\n", [error.localizedDescription UTF8String]);
}
- (void)documentPicker:(UIDocumentPickerViewController*)controller
didPickDocumentAtURLs:(NSArray<NSURL*>*)urls
{
tf_printf("did pick!\n");
[[NSFileManager defaultManager] removeItemAtURL:self.download_url error:nil];
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController*)controller
{
tf_printf("no pick!\n");
[[NSFileManager defaultManager] removeItemAtURL:self.download_url error:nil];
}
@end
@interface AppDelegate : UIResponder <UIApplicationDelegate>