Make it work with WKWebView #198

This commit is contained in:
Sebastián Katzer 2017-01-17 14:02:16 +01:00
parent a2bcdb8942
commit b76f70a9e6
5 changed files with 283 additions and 68 deletions

View File

@ -40,7 +40,6 @@
</feature>
</config-file>
<!-- The app is able to run in background through audio mode -->
<config-file target="*-Info.plist" parent="UIBackgroundModes">
<array>
<string>audio</string>
@ -51,6 +50,8 @@
<header-file src="src/ios/APPBackgroundMode.h" />
<source-file src="src/ios/APPBackgroundMode.m" />
<header-file src="src/ios/APPMethodMagic.h" />
<source-file src="src/ios/APPMethodMagic.m" />
</platform>
<!-- android -->
@ -67,11 +68,6 @@
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<!--
* Puts the service in a foreground state, where the system considers
* it to be something the user is actively aware of and thus not a
* candidate for killing when low on memory.
-->
<service android:name="de.appplant.cordova.plugin.background.ForegroundService" />
</config-file>
@ -86,32 +82,8 @@
<source-file
src="src/android/ForegroundService.java"
target-dir="src/de/appplant/cordova/plugin/background" />
</platform>
<!-- wp8 -->
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="BackgroundMode">
<param name="wp-package" value="BackgroundMode" />
</feature>
</config-file>
<!-- The app is able to run in background through location-tracking mode -->
<config-file target="Properties/WMAppManifest.xml" parent="/Deployment/App/Tasks/DefaultTask">
<BackgroundExecution>
<ExecutionType Name="LocationTracking" />
</BackgroundExecution>
</config-file>
<config-file target="Properties/WMAppManifest.xml" parent="/Deployment/App/Capabilities">
<Capability Name="ID_CAP_LOCATION" />
</config-file>
<source-file src="src/wp8/BackgroundMode.cs" />
</platform>
<!-- windows -->
<platform name="windows">
<config-file target="config.xml" parent="/*">

View File

@ -19,17 +19,32 @@
under the License.
*/
#import "APPMethodMagic.h"
#import "APPBackgroundMode.h"
#import <Cordova/CDVAvailability.h>
@implementation APPBackgroundMode
#pragma mark -
#pragma mark Constants
NSString* const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode";
NSString* const kAPPBackgroundEventActivate = @"activate";
NSString* const kAPPBackgroundEventDeactivate = @"deactivate";
NSString* const kAPPBackgroundEventFailure = @"failure";
#pragma mark -
#pragma mark Initialization
#pragma mark Life Cycle
/**
* Called by runtime once the Class has been loaded.
* Exchange method implementations to hook into their execution.
*/
+ (void) load
{
[self swizzleWKWebViewEngine];
}
/**
* Initialize the plugin.
@ -208,4 +223,33 @@ NSString* const kAPPBackgroundEventFailure = @"failure";
[self.commandDelegate evalJs:js];
}
#pragma mark -
#pragma mark Swizzling
/**
* Swizzle some implementations of CDVWKWebViewEngine.
*/
+ (void) swizzleWKWebViewEngine
{
if (!IsAtLeastiOSVersion(@"8.0"))
return;
Class wkWebViewEngineCls = NSClassFromString(@"CDVWKWebViewEngine");
SEL selector = NSSelectorFromString(@"createConfigurationFromSettings:");
if (!wkWebViewEngineCls)
return;
SwizzleSelectorWithBlock_Begin(wkWebViewEngineCls, selector)
^(CDVPlugin *self, NSDictionary *settings) {
id obj = ((id (*)(id, SEL, NSDictionary*))_imp)(self, _cmd, settings);
SEL sel = NSSelectorFromString(@"_setAlwaysRunsAtForegroundPriority:");
((void (*)(id, SEL, BOOL))[obj methodForSelector:selector])(obj, sel, YES);
return sel;
}
SwizzleSelectorWithBlock_End;
}
@end

93
src/ios/APPMethodMagic.h Normal file
View File

@ -0,0 +1,93 @@
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/**
* Code extracted from
* - http://defagos.github.io/yet_another_article_about_method_swizzling/
* - https://gist.github.com/defagos/1312fec96b48540efa5c
*/
#import <objc/runtime.h>
#import <objc/message.h>
#define SwizzleSelector(clazz, selector, newImpl, oldImpl) \
(*oldImpl) = (__typeof((*oldImpl)))class_swizzleSelector((clazz), (selector), (IMP)(newImpl))
#define SwizzleClassSelector(clazz, selector, newImpl, oldImpl) \
(*oldImpl) = (__typeof((*oldImpl)))class_swizzleClassSelector((clazz), (selector), (IMP)(newImpl))
#define SwizzleSelectorWithBlock_Begin(clazz, selector) { \
SEL _cmd = selector; \
__block IMP _imp = class_swizzleSelectorWithBlock((clazz), (selector),
#define SwizzleSelectorWithBlock_End );}
#define SwizzleClassSelectorWithBlock_Begin(clazz, selector) { \
SEL _cmd = selector; \
__block IMP _imp = class_swizzleClassSelectorWithBlock((clazz), (selector),
#define SwizzleClassSelectorWithBlock_End );}
/**
* Swizzle class method specified by class and selector
* through the provided method implementation.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ IMP ] newImpl The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleClassSelector(Class clazz, SEL selector, IMP newImpl);
/**
* Swizzle class method specified by class and selector
* through the provided code block.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ id ] newImplBlock The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleClassSelectorWithBlock(Class clazz, SEL selector, id newImplBlock);
/**
* Swizzle method specified by class and selector
* through the provided code block.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ id ] newImplBlock The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleSelectorWithBlock(Class clazz, SEL selector, id newImplBlock);
/**
* Swizzle method specified by class and selector
* through the provided method implementation.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ IMP ] newImpl The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl);

106
src/ios/APPMethodMagic.m Normal file
View File

@ -0,0 +1,106 @@
/*
Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/**
* Code extracted from
* - http://defagos.github.io/yet_another_article_about_method_swizzling/
* - https://gist.github.com/defagos/1312fec96b48540efa5c
*/
#import "APPMethodMagic.h"
#import <objc/runtime.h>
#import <objc/message.h>
/**
* Swizzle class method specified by class and selector
* through the provided method implementation.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ IMP ] newImpl The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleClassSelector(Class clazz, SEL selector, IMP newImpl)
{
return class_swizzleSelector(object_getClass(clazz), selector, newImpl);
}
/**
* Swizzle class method specified by class and selector
* through the provided code block.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ id ] newImplBlock The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleClassSelectorWithBlock(Class clazz, SEL selector, id newImplBlock)
{
IMP newImpl = imp_implementationWithBlock(newImplBlock);
return class_swizzleClassSelector(clazz, selector, newImpl);
}
/**
* Swizzle method specified by class and selector
* through the provided code block.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ id ] newImplBlock The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleSelectorWithBlock(Class clazz, SEL selector, id newImplBlock)
{
IMP newImpl = imp_implementationWithBlock(newImplBlock);
return class_swizzleSelector(clazz, selector, newImpl);
}
/**
* Swizzle method specified by class and selector
* through the provided method implementation.
*
* @param [ Class ] clazz The class containing the method.
* @param [ SEL ] selector The selector of the method.
* @param [ IMP ] newImpl The new implementation of the method.
*
* @return [ IMP ] The previous implementation of the method.
*/
IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl)
{
Method method = class_getInstanceMethod(clazz, selector);
const char *types = method_getTypeEncoding(method);
class_addMethod(clazz, selector, imp_implementationWithBlock(^(__unsafe_unretained id self, va_list argp) {
struct objc_super super = {
.receiver = self,
.super_class = class_getSuperclass(clazz)
};
id (*objc_msgSendSuper_typed)(struct objc_super*, SEL, va_list) = (void*)&objc_msgSendSuper;
return objc_msgSendSuper_typed(&super, selector, argp);
}), types);
return class_replaceMethod(clazz, selector, newImpl, types);
}