Skip to content

Commit

Permalink
Counter project
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristina Thai authored and Kristina Thai committed Jan 19, 2015
1 parent 5126732 commit 0cac62e
Show file tree
Hide file tree
Showing 22 changed files with 1,451 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# WatchKitCounter2WayCom
WatchKitCounter2WayComm
===================
Demo of 2 way communication between an Apple Watch app and it's parent iOS app. Information can be passed to the iOS app, which then "replies" back with a response to the watch extension and updates the UI for the watch app. This is an add-on to my original [Send Data to Parent iOS App tutorial](http://www.kristinathai.com/send-data-to-parent-ios-app/).

See full 2 way communication tutorial on http://www.kristinathai.com/watchkit-tutorial-communicate-from-parent-app-back-to-watch-via-reply




Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"images" : [
{
"size" : "22x22",
"idiom" : "watch",
"scale" : "2x",
"role" : "notificationCenter",
"subtype" : "38mm"
},
{
"size" : "27.5x27.5",
"idiom" : "watch",
"scale" : "2x",
"role" : "notificationCenter",
"subtype" : "42mm"
},
{
"size" : "29x29",
"idiom" : "watch",
"role" : "companionSettings",
"scale" : "2x"
},
{
"size" : "29.3x29.3",
"idiom" : "watch",
"role" : "companionSettings",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "watch",
"scale" : "2x",
"role" : "appLauncher",
"subtype" : "38mm"
},
{
"size" : "44x44",
"idiom" : "watch",
"scale" : "2x",
"role" : "appLauncher",
"subtype" : "42mm"
},
{
"size" : "86x86",
"idiom" : "watch",
"scale" : "2x",
"role" : "quickLook",
"subtype" : "38mm"
},
{
"size" : "98x98",
"idiom" : "watch",
"scale" : "2x",
"role" : "quickLook",
"subtype" : "42mm"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"images" : [
{
"orientation" : "portrait",
"idiom" : "watch",
"extent" : "full-screen",
"minimum-system-version" : "8.0",
"subtype" : "38mm",
"scale" : "2x"
},
{
"orientation" : "portrait",
"idiom" : "watch",
"extent" : "full-screen",
"minimum-system-version" : "8.0",
"subtype" : "42mm",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
35 changes: 35 additions & 0 deletions WatchKitCounterDemo WatchKit App/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>WatchKitCounterDemo</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>kristina.WatchKitCounterDemo.watchkitapp</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>WKCompanionAppBundleIdentifier</key>
<string>kristina.WatchKitCounterDemo</string>
<key>WKWatchKitApp</key>
<true/>
</dict>
</plist>
51 changes: 51 additions & 0 deletions WatchKitCounterDemo WatchKit App/Interface.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="6724" systemVersion="14B25" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="AgC-eL-Hgc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6711"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="3735"/>
</dependencies>
<scenes>
<!--Interface Controller-->
<scene sceneID="aou-V4-d1y">
<objects>
<controller id="AgC-eL-Hgc" customClass="InterfaceController">
<items>
<label width="27" height="27" alignment="center" text="0" textAlignment="center" numberOfLines="0" id="NOi-xv-tO3">
<fontDescription key="font" name="HelveticaNeue" family="Helvetica Neue" pointSize="27"/>
</label>
<label width="1" alignment="left" alpha="0.0" text="Saved N" textAlignment="center" numberOfLines="0" id="Y6v-vz-ZW7">
<color key="textColor" red="0.0" green="0.74775904605263155" blue="0.12588408318369154" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="font" style="UICTFontTextStyleSubhead"/>
</label>
<group width="1" alignment="left" id="izM-BR-bkF">
<items>
<button width="67" alignment="left" title="Hit" id="VqA-qs-DTG">
<variation key="device=watch42mm" width="79"/>
<connections>
<action selector="incrementCounter" destination="AgC-eL-Hgc" id="QBN-TE-DPq"/>
</connections>
</button>
<button width="69" alignment="left" title="Clear" id="G6H-HL-q2a">
<variation key="device=watch42mm" width="79"/>
<connections>
<action selector="clearCounter" destination="AgC-eL-Hgc" id="ydA-ov-hQV"/>
</connections>
</button>
</items>
</group>
<button width="1" alignment="left" title="Save" id="1Xz-Ip-6AU">
<connections>
<action selector="saveCounter" destination="AgC-eL-Hgc" id="Yms-ze-wmo"/>
</connections>
</button>
</items>
<connections>
<outlet property="counterLabel" destination="NOi-xv-tO3" id="odQ-LJ-WKa"/>
<outlet property="savedNotificationLabel" destination="Y6v-vz-ZW7" id="2ux-rV-8JV"/>
</connections>
</controller>
</objects>
<point key="canvasLocation" x="219.69230769230771" y="336.94871794871796"/>
</scene>
</scenes>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
38 changes: 38 additions & 0 deletions WatchKitCounterDemo WatchKit Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>WatchKitCounterDemo WatchKit Extension</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>kristina.WatchKitCounterDemo.watchkitextension</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>WKAppBundleIdentifier</key>
<string>kristina.WatchKitCounterDemo.watchkitapp</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.watchkit</string>
</dict>
<key>RemoteInterfacePrincipalClass</key>
<string>InterfaceController</string>
</dict>
</plist>
17 changes: 17 additions & 0 deletions WatchKitCounterDemo WatchKit Extension/InterfaceController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// InterfaceController.h
// WatchKitCounterDemo WatchKit Extension
//
// Created by Thai, Kristina on 12/10/14.
// Copyright (c) 2014 Kristina Thai. All rights reserved.
//

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>

@interface InterfaceController : WKInterfaceController

@property (weak, nonatomic) IBOutlet WKInterfaceLabel *counterLabel;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *savedNotificationLabel;

@end
85 changes: 85 additions & 0 deletions WatchKitCounterDemo WatchKit Extension/InterfaceController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// InterfaceController.m
// WatchKitCounterDemo WatchKit Extension
//
// Created by Thai, Kristina on 12/10/14.
// Copyright (c) 2014 Kristina Thai. All rights reserved.
//

#import "InterfaceController.h"


@interface InterfaceController()

@property (nonatomic, assign) int counter;
@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];

// Configure interface objects here.
NSLog(@"%@ awakeWithContext", self);
self.counter = 0;
}

- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
NSLog(@"%@ will activate", self);
}

- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
NSLog(@"%@ did deactivate", self);
}

#pragma mark - Button actions

- (IBAction)incrementCounter {
[self hideSaveNotificationLabel];

self.counter++;
[self setCounterLabelText];
}
- (IBAction)saveCounter {
//Send count to parent application
NSString *counterString = [NSString stringWithFormat:@"%d", self.counter];
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[counterString] forKeys:@[@"counterValue"]];

//Handle reciever in app delegate of parent app
[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {

int reply = [[replyInfo objectForKey:@"response"] intValue];

//Show and change text for hidden save notification label
[self.savedNotificationLabel setText:[NSString stringWithFormat:@"Saved %d", reply]];
[self showSaveNotificationLabel];
}];

}
- (IBAction)clearCounter {
[self hideSaveNotificationLabel];

self.counter = 0;
[self setCounterLabelText];
}

#pragma mark - Helper methods

- (void)setCounterLabelText {
[self.counterLabel setText:[NSString stringWithFormat:@"%d", self.counter]];
}

- (void)hideSaveNotificationLabel {
[self.savedNotificationLabel setAlpha:0];
}

-(void)showSaveNotificationLabel {
[self.savedNotificationLabel setAlpha:1];
}

@end



Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"aps": {
"alert": "Test message",
"title": "Optional title",
"category": "myCategory"
},

"WatchKit Simulator Actions": [
{
"title": "First Button",
"identifier": "firstButtonAction"
}
],

"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."
}
Loading

0 comments on commit 0cac62e

Please sign in to comment.