Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support save webarchive #3297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ class RNCWebViewManagerImpl {
val COMMAND_INJECT_JAVASCRIPT = 6
val COMMAND_LOAD_URL = 7
val COMMAND_FOCUS = 8
val COMMAND_SAVE_WEB_ARCHIVE = 9


// android commands
val COMMAND_CLEAR_FORM_DATA = 1000
Expand All @@ -302,6 +304,7 @@ class RNCWebViewManagerImpl {
.put("clearFormData", COMMAND_CLEAR_FORM_DATA)
.put("clearCache", COMMAND_CLEAR_CACHE)
.put("clearHistory", COMMAND_CLEAR_HISTORY)
.put("saveWebArchive", COMMAND_SAVE_WEB_ARCHIVE)
.build()
}

Expand Down Expand Up @@ -346,6 +349,10 @@ class RNCWebViewManagerImpl {
webView.clearCache(includeDiskFiles)
}
"clearHistory" -> webView.clearHistory()
"saveWebArchive" -> {
val path = args.getString(0)
webView.saveWebArchive(path)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions apple/RNCWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ - (void)stopLoading {
[_view stopLoading];
}

- (void)saveWebArchive:(nonnull NSString *)filename {
[_view saveWebArchive:data];
}

- (void)clearFormData {
// android only
}
Expand Down
1 change: 1 addition & 0 deletions apple/RNCWebViewImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
- (void)stopLoading;
- (void)requestFocus;
- (void)clearCache:(BOOL)includeDiskFiles;
- (void)saveWebArchive:(NSString *_Nullable)filename;
#ifdef RCT_NEW_ARCH_ENABLED
- (void)destroyWebView;
#endif
Expand Down
23 changes: 23 additions & 0 deletions apple/RNCWebViewImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,24 @@ - (void)evaluateJS:(NSString *)js
}];
}

- (void)dosave:(NSString *)filepath {
if (@available(iOS 14.0, *)) {
[self.webView createWebArchiveDataWithCompletionHandler:^(NSData * _Nullable webArchiveData, NSError * _Nullable error) {
if (error) {
NSLog(@"Error creating web archive data: %@", error.localizedDescription);
return;
}

if (webArchiveData) {
[webArchiveData writeToFile:filepath atomically:YES];
NSLog(@"Web archive data saved to %@", filepath);
}
}];
} else {
NSLog(@"Web archive data saved to %@, but need iOS 14.0 or above", filepath);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message seems wrong? The archive data was not saved in this case, no?

}
}

-(void)forceIgnoreSilentHardwareSwitch:(BOOL)initialSetup
{
NSString *mp3Str = @"data:audio/mp3;base64,//tAxAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAAFAAAESAAzMzMzMzMzMzMzMzMzMzMzMzMzZmZmZmZmZmZmZmZmZmZmZmZmZmaZmZmZmZmZmZmZmZmZmZmZmZmZmczMzMzMzMzMzMzMzMzMzMzMzMzM//////////////////////////8AAAA5TEFNRTMuMTAwAZYAAAAAAAAAABQ4JAMGQgAAOAAABEhNIZS0AAAAAAD/+0DEAAPH3Yz0AAR8CPqyIEABp6AxjG/4x/XiInE4lfQDFwIIRE+uBgZoW4RL0OLMDFn6E5v+/u5ehf76bu7/6bu5+gAiIQGAABQIUJ0QolFghEn/9PhZQpcUTpXMjo0OGzRCZXyKxoIQzB2KhCtGobpT9TRVj/3Pmfp+f8X7Pu1B04sTnc3s0XhOlXoGVCMNo9X//9/r6a10TZEY5DsxqvO7mO5qFvpFCmKIjhpSItGsUYcRO//7QsQRgEiljQIAgLFJAbIhNBCa+JmorCbOi5q9nVd2dKnusTMQg4MFUlD6DQ4OFijwGAijRMfLbHG4nLVTjydyPlJTj8pfPflf9/5GD950A5e+jsrmNZSjSirjs1R7hnkia8vr//l/7Nb+crvr9Ok5ZJOylUKRxf/P9Zn0j2P4pJYXyKkeuy5wUYtdmOu6uobEtFqhIJViLEKIjGxchGev/L3Y0O3bwrIOszTBAZ7Ih28EUaSOZf/7QsQfg8fpjQIADN0JHbGgQBAZ8T//y//t/7d/2+f5m7MdCeo/9tdkMtGLbt1tqnabRroO1Qfvh20yEbei8nfDXP7btW7f9/uO9tbe5IvHQbLlxpf3DkAk0ojYcv///5/u3/7PTfGjPEPUvt5D6f+/3Lea4lz4tc4TnM/mFPrmalWbboeNiNyeyr+vufttZuvrVrt/WYv3T74JFo8qEDiJqJrmDTs///v99xDku2xG02jjunrICP/7QsQtA8kpkQAAgNMA/7FgQAGnobgfghgqA+uXwWQ3XFmGimSbe2X3ksY//KzK1a2k6cnNWOPJnPWUsYbKqkh8RJzrVf///P///////4vyhLKHLrCb5nIrYIUss4cthigL1lQ1wwNAc6C1pf1TIKRSkt+a//z+yLVcwlXKSqeSuCVQFLng2h4AFAFgTkH+Z/8jTX/zr//zsJV/5f//5UX/0ZNCNCCaf5lTCTRkaEdhNP//n/KUjf/7QsQ5AEhdiwAAjN7I6jGddBCO+WGTQ1mXrYatSAgaykxBTUUzLjEwMKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqg==";
Expand Down Expand Up @@ -1513,6 +1531,11 @@ - (void)goBack
[_webView goBack];
}

- (void)saveWebArchive:(NSString *)filename
{
[self dosave:filename];
}

- (void)reload
{
/**
Expand Down
1 change: 1 addition & 0 deletions apple/RNCWebViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ - (RNCView *)view
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(postMessage, message:(NSString *)message, message)
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(injectJavaScript, script:(NSString *)script, script)
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(clearCache, includeDiskFiles:(BOOL)includeDiskFiles, includeDiskFiles)
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(saveWebArchive,filename:(NSString *)filename, filename)

RCT_EXPORT_METHOD(shouldStartLoadWithLockIdentifier:(BOOL)shouldStart
lockIdentifier:(double)lockIdentifier)
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ declare class WebView<P = {}> extends Component<WebViewProps & P> {
* Posts a message to WebView.
*/
postMessage: (message: string) => void;

/**
* Save webview content
*/
saveWebArchive?: (filename: string) => void;

/**
* (Android only)
Expand Down
3 changes: 2 additions & 1 deletion src/RNCWebViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export interface NativeCommands {
injectJavaScript: (viewRef: React.ElementRef<HostComponent<NativeProps>>, javascript: string) => void;
requestFocus: (viewRef: React.ElementRef<HostComponent<NativeProps>>) => void;
postMessage: (viewRef: React.ElementRef<HostComponent<NativeProps>>, data: string) => void;
saveWebArchive: (viewRef: React.ElementRef<HostComponent<NativeProps>>, filename: string) => void;
// Android Only
loadUrl: (viewRef: React.ElementRef<HostComponent<NativeProps>>, url: string) => void;
clearFormData: (viewRef: React.ElementRef<HostComponent<NativeProps>>) => void;
Expand All @@ -273,7 +274,7 @@ export interface NativeCommands {
}

export const Commands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['goBack', 'goForward', 'reload', 'stopLoading', 'injectJavaScript', 'requestFocus', 'postMessage', 'loadUrl', 'clearFormData', 'clearCache', 'clearHistory'],
supportedCommands: ['goBack', 'goForward', 'reload', 'stopLoading', 'injectJavaScript', 'requestFocus', 'postMessage', 'loadUrl', 'clearFormData', 'clearCache', 'clearHistory', 'saveWebArchive'],
});

export default codegenNativeComponent<NativeProps>(
Expand Down
1 change: 1 addition & 0 deletions src/WebView.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const WebViewComponent = forwardRef<{}, AndroidWebViewProps>(({
requestFocus: () => webViewRef.current && Commands.requestFocus(webViewRef.current),
clearFormData: () => webViewRef.current && Commands.clearFormData(webViewRef.current),
clearCache: (includeDiskFiles: boolean) => webViewRef.current && Commands.clearCache(webViewRef.current, includeDiskFiles),
saveWebArchive: (filename: string) => webViewRef.current && Commands.saveWebArchive(webViewRef.current, filename),
clearHistory: () => webViewRef.current && Commands.clearHistory(webViewRef.current),
}), [setViewState, webViewRef]);

Expand Down
1 change: 1 addition & 0 deletions src/WebView.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const WebViewComponent = forwardRef<{}, IOSWebViewProps>(({
injectJavaScript: (data: string) => webViewRef.current && Commands.injectJavaScript(webViewRef.current, data),
requestFocus: () => webViewRef.current && Commands.requestFocus(webViewRef.current),
clearCache: (includeDiskFiles: boolean) => webViewRef.current && Commands.clearCache(webViewRef.current, includeDiskFiles),
saveWebArchive: (filename: string) => webViewRef.current && Commands.saveWebArchive(webViewRef.current, filename),
}), [setViewState, webViewRef]);


Expand Down