mirror of
https://github.com/status-im/react-native-webview.git
synced 2026-08-31 11:41:08 +00:00
feat(iOS): Add Hardware Silence (#1218)
* Fixes Issue #1140 Fixes https://github.com/react-native-community/react-native-webview/issues/1140 Based on a solution found at: https://stackoverflow.com/questions/56460362/how-to-force-wkwebview-to-ignore-hardware-silent-switch-on-ios I changed the code found in the linked source from Swift to Objective-C, as required by this project. WARNING: I haven't used Swift before and very limited experience with Objective-C. - For me this seems to work, but it is not the cleanest solution in my opinion. - It might also be possible to play generated sound (i.e. using oscillator) instead of hardcoding the silent base64 mp3 data. - Maybe ignoring silence switch should only be done if a parameter is supplied * fixes import path * adds documentation for ignoreSilentHardwareSwitch * adds ignoreSilentHardwareSwitch parameter * reverting back to old import path * Update Guide.md Co-authored-by: Dominik Beste <dominik.beste@gmail.com>
This commit is contained in:
co-authored by
Dominik Beste
parent
f6d54c2b14
commit
d4ab332891
@@ -517,3 +517,10 @@ const App = () => {
|
||||
```
|
||||
|
||||
Note that these cookies will only be sent on the first request unless you use the technique above for [setting custom headers on each page load](#Setting-Custom-Headers).
|
||||
|
||||
### Hardware Silence Switch
|
||||
There are some inconsistencies in how the hardware silence switch is handled between embedded `audio` and `video` elements and between iOS and Android platforms.
|
||||
|
||||
Audio on `iOS` will be muted when the hardware silence switch is in the on position, unless the `ignoreSilentHardwareSwitch` parameter is set to true.
|
||||
|
||||
Video on `iOS` will always ignore the hardware silence switch.
|
||||
|
||||
@@ -64,6 +64,7 @@ This document lays out the current public properties and methods for the React N
|
||||
- [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
|
||||
- [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
|
||||
- [`textZoom`](Reference.md#textZoom)
|
||||
- [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch)
|
||||
|
||||
## Methods Index
|
||||
|
||||
@@ -1127,6 +1128,16 @@ Example:
|
||||
|
||||
`<WebView textZoom={100} />`
|
||||
|
||||
### `ignoreSilentHardwareSwitch`
|
||||
|
||||
(ios only)
|
||||
|
||||
When set to true the hardware silent switch is ignored. Default: `false`
|
||||
|
||||
| Type | Required | Platform |
|
||||
| ------- | -------- | -------- |
|
||||
| boolean | No | iOS |
|
||||
|
||||
## Methods
|
||||
|
||||
### `extraNativeComponentConfig()`
|
||||
|
||||
@@ -140,6 +140,15 @@ static NSDictionary* customCertificatesForHost;
|
||||
}
|
||||
|
||||
#if !TARGET_OS_OSX
|
||||
[[NSNotificationCenter defaultCenter]addObserver:self
|
||||
selector:@selector(appDidBecomeActive)
|
||||
name:UIApplicationDidBecomeActiveNotification
|
||||
object:nil];
|
||||
|
||||
[[NSNotificationCenter defaultCenter]addObserver:self
|
||||
selector:@selector(appWillResignActive)
|
||||
name:UIApplicationWillResignActiveNotification
|
||||
object:nil];
|
||||
if (@available(iOS 12.0, *)) {
|
||||
// Workaround for a keyboard dismissal bug present in iOS 12
|
||||
// https://openradar.appspot.com/radar?id=5018321736957952
|
||||
@@ -163,6 +172,7 @@ static NSDictionary* customCertificatesForHost;
|
||||
selector:@selector(hideFullScreenVideoStatusBars)
|
||||
name:UIWindowDidBecomeHiddenNotification
|
||||
object:nil];
|
||||
|
||||
}
|
||||
#endif // !TARGET_OS_OSX
|
||||
return self;
|
||||
@@ -1031,6 +1041,37 @@ static NSDictionary* customCertificatesForHost;
|
||||
}];
|
||||
}
|
||||
|
||||
-(void)forceIgnoreSilentHardwareSwitch:(BOOL)initialSetup
|
||||
{
|
||||
NSString *mp3Str = @"data:audio/mp3;base64,//tAxAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAAFAAAESAAzMzMzMzMzMzMzMzMzMzMzMzMzZmZmZmZmZmZmZmZmZmZmZmZmZmaZmZmZmZmZmZmZmZmZmZmZmZmZmczMzMzMzMzMzMzMzMzMzMzMzMzM//////////////////////////8AAAA5TEFNRTMuMTAwAZYAAAAAAAAAABQ4JAMGQgAAOAAABEhNIZS0AAAAAAD/+0DEAAPH3Yz0AAR8CPqyIEABp6Ax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qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqg==";
|
||||
NSString *scr;
|
||||
if (initialSetup) {
|
||||
scr = [NSString stringWithFormat:@"var s=new Audio('%@');s.id='wkwebviewAudio';s.controls=false;s.loop=true;s.play();document.body.appendChild(s);true", mp3Str];
|
||||
} else {
|
||||
scr = [NSString stringWithFormat:@"var s=document.getElementById('wkwebviewAudio');s.src=null;s.parentNode.removeChild(s);s=null;s=new Audio('%@');s.id='wkwebviewAudio';s.controls=false;s.loop=true;s.play();document.body.appendChild(s);true", mp3Str];
|
||||
}
|
||||
[self evaluateJS: scr thenCall: nil];
|
||||
}
|
||||
|
||||
-(void)disableIgnoreSilentSwitch
|
||||
{
|
||||
[self evaluateJS: @"document.getElementById('wkwebviewAudio').src=null;true" thenCall: nil];
|
||||
}
|
||||
|
||||
-(void)appDidBecomeActive
|
||||
{
|
||||
if (_ignoreSilentHardwareSwitch) {
|
||||
[self forceIgnoreSilentHardwareSwitch:false];
|
||||
}
|
||||
}
|
||||
|
||||
-(void)appWillResignActive
|
||||
{
|
||||
if (_ignoreSilentHardwareSwitch) {
|
||||
[self disableIgnoreSilentSwitch];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the navigation is complete.
|
||||
* @see https://fburl.com/rtys6jlb
|
||||
@@ -1038,6 +1079,10 @@ static NSDictionary* customCertificatesForHost;
|
||||
- (void)webView:(WKWebView *)webView
|
||||
didFinishNavigation:(WKNavigation *)navigation
|
||||
{
|
||||
if (_ignoreSilentHardwareSwitch) {
|
||||
[self forceIgnoreSilentHardwareSwitch:true];
|
||||
}
|
||||
|
||||
if (_onLoadingFinish) {
|
||||
_onLoadingFinish([self baseEvent]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user