Skip to content

Commit

Permalink
#11 Background issues
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Sep 13, 2015
1 parent 3cec227 commit 9da5dbe
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ In XCode, go to your targets and add this app group to both the phone and watch
##Tips:
Building in XCode 7 requires you to set `bitcode` to `NO` for all 3 targets. Go to 'Build Settings' and disable it 3 times. Will be fixed soon, when [MMWormhole](https://github.com/mutualmobile/MMWormhole) is updated.
For developer builds to work on a real Watch you'll need to [register the Watch UDID and add it to the provisioning profile](http://stackoverflow.com/questions/29854314/debug-on-real-apple-watch-application-verification-failed) or you'll see 'Application verification failed' when installing the app on the Watch.
While you're in the iOS member center, also register an App Group (under Identifiers) and make sure you enable the App Group in the App ID you're using for your app.
Expand Down
15 changes: 7 additions & 8 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ <h1>Apache Cordova</h1>
<p class="event received">Device is Ready</p>
<script>
function feedback(what) {
console.log("feedback: " + what);
document.getElementById("feedback").innerHTML = what;
}

window.onerror = function (a, b, c) {
feedback(a + ", " + b + ", " + c);
alert(a + ", " + b + ", " + c);
}
</script>
<button onclick="sendHeaderMessageImageGlance()">glance: header, message, img</button>
Expand Down Expand Up @@ -104,7 +106,7 @@ <h1>Apache Cordova</h1>

function sendTableGlance() {
var payload = {
'label2': {'value': 'Here\'s a table with a few of different colors. One even has an image!', 'color': '#FFFFFF'},
'label2': {'value': 'Table with a colors. ' + new Date(), 'color': '#FFFFFF'},
'table': { // don't add selectable rows to a glance since glances are read-only
'rows': [
{
Expand All @@ -113,12 +115,8 @@ <h1>Apache Cordova</h1>
'backgroundColor': '#1884C4',
'cornerRadius': 8
},
'label': {'value': ' images!'}, // unlike in HTML, multiple spaces have effect
'imageLeft': {
'data': 'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7',
'width': 16,
'height': 16
},
'label': {'value': ' images!'}, // unlike HTML, multiple spaces have effect
'imageLeft': {'src': 'www/img/logo.png', 'width': 25, 'height': 30},
'imageRight': {'src': 'www/img/logo.png', 'width': 25, 'height': 30}
},
{
Expand Down Expand Up @@ -503,7 +501,8 @@ <h1>Apache Cordova</h1>
function initAppleWatch() {
applewatch.init(function () {
// register for notifications
applewatch.registerNotifications(onNotificationRegistrationSuccess, onNotificationRegistrationError);
// TODO throws 'undefined is not a function'.. setTimeout ?
// applewatch.registerNotifications(onNotificationRegistrationSuccess, onNotificationRegistrationError);
},
function (err) {
// an error occurred
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<plugin
id="cordova-plugin-applewatch"
version="0.5.0"
version="0.6.0-dev"
xmlns="http://apache.org/cordova/ns/plugins/1.0">

<name>Apple Watch</name>
Expand Down
17 changes: 15 additions & 2 deletions src/ios/app/AppDelegate+applewatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
@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
Expand All @@ -35,7 +48,7 @@ - (void) callJavascriptFunctionWhenAvailable:(NSString*)function {
if (appleWatch.initDone) {
[appleWatch.webView stringByEvaluatingJavaScriptFromString:function];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 25 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 80 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[self callJavascriptFunctionWhenAvailable:function];
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/ios/app/AppleWatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ - (void) init:(CDVInvokedUrlCommand*)command {

[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];

self.initDone = YES;
// make sure the app is awake
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
self.initDone = YES; // after a little timeout perhaps?
});
}

- (void) registerNotifications:(CDVInvokedUrlCommand*)command;
Expand Down
11 changes: 8 additions & 3 deletions src/ios/resources/watchkitextension/WatchKitUIHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ + (void) setImage:(WKInterfaceImage*)image fromDic:(NSDictionary*)dic {

if (namedSrc != nil) {
[image setImageNamed:namedSrc];
}
else {
[image setImageData:[dic valueForKey:@"src"]];
} else {
// This implementation seems to use a cached version which gets lost between nav from glance to app,
// not a problem if developers use different images, but we can't expect that to be the case.
// [image setImageData:[dic valueForKey:@"src"]];

// so we're using this instead.. a bit less efficient but more failsafe
UIImage *img = [UIImage imageWithData:[dic valueForKey:@"src"]];
[image setImage:img];
}

[self setCommonPropertiesAndShow:image fromDic:dic];
Expand Down

0 comments on commit 9da5dbe

Please sign in to comment.