Skip to content

Commit

Permalink
Merge pull request #19 from e2l3n/tpopov/watch-os2-support
Browse files Browse the repository at this point in the history
Add WatchOS 2 support.
  • Loading branch information
EddyVerbruggen committed Jan 7, 2016
2 parents 7784148 + abc101b commit 72f61aa
Show file tree
Hide file tree
Showing 31 changed files with 1,360 additions and 794 deletions.
119 changes: 65 additions & 54 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,58 +1,69 @@
<?xml version='1.0' encoding='UTF-8'?>
<plugin
id="cordova-plugin-applewatch"
version="0.6.1"
version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0">

<name>Apple Watch</name>

<author>Telerik</author>

<description>AppleWatch plugin for your Cordova project. You build the UI with JS, so no XCode native code hacking required.</description>

<keywords>AppleWatch</keywords>

<license>MIT</license>

<engines>
<engine name="cordova" version=">=3.5.0"/>
<!-- When multiple SDKs are installed the check will fail, seems to be a bug in cordova-lib -->
<!--<engine name="apple-ios" version=">=8.2.0"/>-->
</engines>

<js-module name="AppleWatch" src="www/applewatch.js">
<clobbers target="applewatch"/>
</js-module>

<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="AppleWatch">
<param name="ios-package" value="AppleWatch"/>
<param name="onload" value="true"/>
</feature>
<preference name="deployment-target" value="8.2"/>
</config-file>

<header-file src="src/ios/app/AppleWatch.h"/>
<source-file src="src/ios/app/AppleWatch.m"/>
<header-file src="src/ios/app/AppDelegate+applewatch.h"/>
<source-file src="src/ios/app/AppDelegate+applewatch.m"/>

<framework src="Foundation.framework"/>

<header-file src="src/ios/lib/headers/MMWormhole.h" />
<source-file src="src/ios/lib/libmmwormhole.a" framework="true" />


<!-- (1/2) THESE HOOKS ARE FOR TELERIK APPBUILDER AND ARE ENABLED IN THE appbuilder BRANCH OF THIS REPOSITORY -->
<hook type="before_plugin_install" src="src/ios/hooks/ab/prerequisites.js"/>
<hook type="before_plugin_install" src="src/ios/hooks/ab/add-targets.js"/>


<!-- (2/2) THESE HOOKS ARE FOR THE CORDOVA CLI
<hook type="after_plugin_install" src="src/ios/hooks/cordova/copy_watchkitfiles.js" />
<hook type="after_plugin_install" src="src/ios/hooks/cordova/fix_watchkitapp_buildtarget.js" />
<hook type="after_prepare" src="src/ios/hooks/cordova/copy_custom_storyboard.js" /-->
</platform>

</plugin>

<name>Apple Watch</name>

<author>Telerik</author>

<description>AppleWatch plugin for your Cordova project. You build the UI with JS, so no XCode native code hacking required.</description>

<keywords>AppleWatch</keywords>

<license>MIT</license>

<engines>
<engine name="cordova" version=">=3.5.0"/>
<!-- When multiple SDKs are installed the check will fail, seems to be a bug in cordova-lib -->
<!--<engine name="apple-ios" version=">=8.2.0"/>-->
</engines>

<js-module name="AppleWatch" src="www/applewatch.js">
<clobbers target="applewatch"/>
</js-module>

<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="AppleWatch">
<param name="ios-package" value="AppleWatch"/>
<param name="onload" value="true"/>
</feature>
<preference name="deployment-target" value="9.0"/>
</config-file>

<header-file src="src/ios/app/AppleWatch.h"/>
<source-file src="src/ios/app/AppleWatch.m"/>
<header-file src="src/ios/app/AppDelegate+applewatch.h"/>
<source-file src="src/ios/app/AppDelegate+applewatch.m"/>

<framework src="Foundation.framework"/>

<header-file src="src/ios/lib/headers/MMWormhole.h" />
<header-file src="src/ios/lib/headers/MMWormholeCoordinatedFileTransiting.h" />
<header-file src="src/ios/lib/headers/MMWormholeFileTransiting.h" />
<header-file src="src/ios/lib/headers/MMWormholeSessionContextTransiting.h" />
<header-file src="src/ios/lib/headers/MMWormholeSessionFileTransiting.h" />
<header-file src="src/ios/lib/headers/MMWormholeSessionMessageTransiting.h" />
<header-file src="src/ios/lib/headers/MMWormholeSession.h" />
<header-file src="src/ios/lib/headers/MMWormholeTransiting.h" />
<header-file src="src/ios/lib/headers/MMWormholeUmbrella.h" />

<source-file src="src/ios/lib/libmmwormhole-ios.a" framework="true" />
<source-file src="src/ios/lib/libmmwormhole-watchos.a" />

<!-- (1/2) THESE HOOKS ARE FOR TELERIK APPBUILDER AND ARE ENABLED IN THE appbuilder BRANCH OF THIS REPOSITORY -->
<hook type="before_plugin_install" src="src/ios/hooks/ab/prerequisites.js"/>
<hook type="before_plugin_install" src="src/ios/hooks/ab/add-targets.js"/>


<!-- (2/2) THESE HOOKS ARE FOR THE CORDOVA CLI -->
<!-- execute these once -->
<!-- <hook type="after_plugin_install" src="src/ios/hooks/cordova/copy_watchkitfiles.js" /> -->
<!-- <hook type="after_plugin_install" src="src/ios/hooks/cordova/fix_watchkitapp_buildtarget.js" /> -->
<!-- execute this one every time because the user may have done updates -->
<hook type="after_prepare" src="src/ios/hooks/cordova/copy_custom_storyboard.js" />
</platform>

</plugin>
3 changes: 2 additions & 1 deletion src/ios/app/AppDelegate+applewatch.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "AppDelegate.h"

@interface AppDelegate (applewatch)
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply;

- (void) callJavascriptFunctionWhenAvailable:(NSString*)function;

@end
55 changes: 8 additions & 47 deletions src/ios/app/AppDelegate+applewatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,17 @@

@implementation AppDelegate (applewatch)

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
// requesting a bit of background processing time, useful when the app was killed instead of running in the background
__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
expirationHandler:^{
watchKitHandler = UIBackgroundTaskInvalid;
}];
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 30 ), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
[[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
} );

NSString* jsFunction = [userInfo objectForKey:@"action"];

NSLog(@"In handleWatchKitExtensionRequest, jsFunction: %@", jsFunction);

NSString* params;
// TODO 'onVoted' should not be hardcoded!
if ([jsFunction isEqualToString:@"onVoted"]) {
if ([[userInfo objectForKey:@"params"] isKindOfClass:[NSData class]]) {
// animated gif
params = [NSString stringWithFormat:@"{'type':'base64img', 'data':'data:image/gif;base64,%@'}", [[userInfo objectForKey:@"params"] base64EncodedStringWithOptions:0]];
} else {
// text label, emoji, dictated text
params = [NSString stringWithFormat:@"{'type':'text', 'data':'%@'}", [userInfo objectForKey:@"params"]];
}
} else {
params = [NSString stringWithFormat:@"'%@'", [userInfo objectForKey:@"params"]];
}

NSString* result = [NSString stringWithFormat:@"%@(%@)", jsFunction, params == nil ? @"" : params];
[self callJavascriptFunctionWhenAvailable:result];

// no need to wait as data is passed back async
reply(@{});
}

// check every x seconds for the phone app to be ready, or stop from glance.didDeactivate
// TODO stop after x tries
- (void) callJavascriptFunctionWhenAvailable:(NSString*)function {
AppleWatch *appleWatch = [self.viewController getCommandInstance:@"AppleWatch"];
if (appleWatch.initDone) {
[appleWatch.webView stringByEvaluatingJavaScriptFromString:function];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 80 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[self callJavascriptFunctionWhenAvailable:function];
});
}
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
AppleWatch *appleWatch = [self.viewController getCommandInstance:@"AppleWatch"];
if (appleWatch.initDone) {
[appleWatch.webView stringByEvaluatingJavaScriptFromString:function];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 80 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[self callJavascriptFunctionWhenAvailable:function];
});
}
}

@end
Loading

0 comments on commit 72f61aa

Please sign in to comment.