Преглед изворни кода

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
RedPandaTronics пре 4 година
родитељ
комит
c22d4e712f
No account linked to committer's email address
1 измењених фајлова са 40 додато и 1 уклоњено
  1. 40
    1
      ios/RNCWebView.m

+ 40
- 1
ios/RNCWebView.m Прегледај датотеку

@@ -133,6 +133,15 @@ static NSDictionary* customCertificatesForHost;
133 133
   }
134 134
 
135 135
 #if !TARGET_OS_OSX
136
+    [[NSNotificationCenter defaultCenter]addObserver:self
137
+    selector:@selector(appDidBecomeActive)
138
+        name:UIApplicationDidBecomeActiveNotification
139
+      object:nil];
140
+    
141
+    [[NSNotificationCenter defaultCenter]addObserver:self
142
+    selector:@selector(appWillResignActive)
143
+        name:UIApplicationWillResignActiveNotification
144
+      object:nil];
136 145
   if (@available(iOS 12.0, *)) {
137 146
     // Workaround for a keyboard dismissal bug present in iOS 12
138 147
     // https://openradar.appspot.com/radar?id=5018321736957952
@@ -156,6 +165,7 @@ static NSDictionary* customCertificatesForHost;
156 165
                                                selector:@selector(hideFullScreenVideoStatusBars)
157 166
                                                    name:UIWindowDidBecomeHiddenNotification
158 167
                                                  object:nil];
168
+      
159 169
   }
160 170
 #endif // !TARGET_OS_OSX
161 171
   return self;
@@ -1129,6 +1139,33 @@ static NSDictionary* customCertificatesForHost;
1129 1139
   }];
1130 1140
 }
1131 1141
 
1142
+-(void)forceIgnoreSilentHardwareSwitch:(BOOL)initialSetup
1143
+{
1144
+    NSString *mp3Str = @"data:audio/mp3;base64,//tAxAAAAAAAAAAAAAAAAAAAAAA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+a//z+yLVcwlXKSqeSuCVQFLng2h4AFAFgTkH+Z/8jTX/zr//zsJV/5f//5UX/0ZNCNCCaf5lTCTRkaEdhNP//n/KUjf/7QsQ5AEhdiwAAjN7I6jGddBCO+WGTQ1mXrYatSAgaykxBTUUzLjEwMKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqg==";
1145
+    NSString *scr;
1146
+    if (initialSetup) {
1147
+        scr = [NSString stringWithFormat:@"var s=new Audio('%@');s.id='wkwebviewAudio';s.controls=false;s.loop=true;s.play();document.body.appendChild(s);true", mp3Str];
1148
+    } else {
1149
+        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];
1150
+    }
1151
+    [self evaluateJS: scr thenCall: nil];
1152
+}
1153
+
1154
+-(void)disableIgnoreSilentSwitch
1155
+{
1156
+    [self evaluateJS: @"document.getElementById('wkwebviewAudio').src=null;true" thenCall: nil];
1157
+}
1158
+
1159
+-(void)appDidBecomeActive
1160
+{
1161
+    [self forceIgnoreSilentHardwareSwitch:false];
1162
+}
1163
+
1164
+-(void)appWillResignActive
1165
+{
1166
+    [self disableIgnoreSilentSwitch];
1167
+}
1168
+
1132 1169
 /**
1133 1170
  * Called when the navigation is complete.
1134 1171
  * @see https://fburl.com/rtys6jlb
@@ -1136,11 +1173,13 @@ static NSDictionary* customCertificatesForHost;
1136 1173
 - (void)webView:(WKWebView *)webView
1137 1174
   didFinishNavigation:(WKNavigation *)navigation
1138 1175
 {
1176
+    [self forceIgnoreSilentHardwareSwitch:true];
1177
+    
1139 1178
    if (_injectedJavaScript) {
1140 1179
      [self evaluateJS: _injectedJavaScript thenCall: ^(NSString *jsEvaluationValue) {
1141 1180
        NSMutableDictionary *event = [self baseEvent];
1142 1181
        event[@"jsEvaluationValue"] = jsEvaluationValue;
1143
-
1182
+         
1144 1183
        if (self.onLoadingFinish) {
1145 1184
          self.onLoadingFinish(event);
1146 1185
        }