Skip to content

Commit 5f1884e

Browse files
committed
PotLoc: Version 1.1, 2016-03-21
Adopt Swift 2.2 language changes.
1 parent df4e0b9 commit 5f1884e

15 files changed

+153
-130
lines changed

PotLoc/Common/PotlocConstants.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
2+
Copyright (C) 2016 Apple Inc. All Rights Reserved.
33
See LICENSE.txt for this sample’s licensing information
44

55
Abstract:

PotLoc/LICENSE.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Sample code project: PotLoc: CoreLocation with iPhone and Apple Watch
2-
Version: 1.0
2+
Version: 1.1
33

44
IMPORTANT: This Apple software is supplied to you by Apple
55
Inc. ("Apple") in consideration of your agreement to the following
@@ -39,4 +39,4 @@ AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
3939
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
4040
POSSIBILITY OF SUCH DAMAGE.
4141

42-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
42+
Copyright (C) 2016 Apple Inc. All Rights Reserved.

PotLoc/Potloc WatchKit App/Base.lproj/Interface.storyboard

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="8121.17" systemVersion="15A178t" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="AgC-eL-Hgc">
2+
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="8152.3" systemVersion="15A214" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="AgC-eL-Hgc">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.14"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="8066.14"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8124.4"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="8077.2"/>
66
</dependencies>
77
<scenes>
88
<!--Potloc-->
@@ -43,15 +43,13 @@
4343
<action selector="requestLocation:" destination="vKN-QW-QSX" id="Sub-nD-iv4"/>
4444
</connections>
4545
</button>
46-
<timer alignment="left" id="qpm-Zg-mhf"/>
4746
<separator alignment="left" id="vK1-9E-kfS"/>
4847
<label alignment="left" text="Label" id="jpF-sQ-sjD"/>
4948
<label alignment="left" text="Label" id="Xry-1x-JDh"/>
5049
<separator alignment="left" id="x6Z-ra-Hm7"/>
5150
<label alignment="left" text="Label" numberOfLines="0" id="WTm-mB-7Kd"/>
5251
</items>
5352
<connections>
54-
<outlet property="displayTimer" destination="qpm-Zg-mhf" id="Xzb-eh-U9S"/>
5553
<outlet property="errorLabel" destination="WTm-mB-7Kd" id="RXk-5s-CYR"/>
5654
<outlet property="latitudeLabel" destination="jpF-sQ-sjD" id="IZU-CZ-aIh"/>
5755
<outlet property="longitudeLabel" destination="Xry-1x-JDh" id="5TX-s5-9RD"/>

PotLoc/Potloc WatchKit Extension/ExtensionDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
2+
Copyright (C) 2016 Apple Inc. All Rights Reserved.
33
See LICENSE.txt for this sample’s licensing information
44

55
Abstract:

PotLoc/Potloc WatchKit Extension/PotlocInterfaceController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
2+
Copyright (C) 2016 Apple Inc. All Rights Reserved.
33
See LICENSE.txt for this sample’s licensing information
44

55
Abstract:

PotLoc/Potloc WatchKit Extension/RequestLocationInterfaceController.swift

+39-72
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
2+
Copyright (C) 2016 Apple Inc. All Rights Reserved.
33
See LICENSE.txt for this sample’s licensing information
44

55
*/
@@ -20,12 +20,6 @@ import Foundation
2020
*/
2121
class RequestLocationInterfaceController: WKInterfaceController, CLLocationManagerDelegate {
2222
// MARK: Properties
23-
24-
/**
25-
When this timer times out, the labels in the interface reset to a default
26-
state that does not resemble a requestLocation result.
27-
*/
28-
var interfaceResetTimer = NSTimer()
2923

3024
/// Location manager to request authorization and location updates.
3125
let manager = CLLocationManager()
@@ -36,9 +30,6 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
3630
/// Button to request location. Also allows cancelling the location request.
3731
@IBOutlet var requestLocationButton: WKInterfaceButton!
3832

39-
/// Timer to count down 5 seconds as a visual cue that the interface will reset.
40-
@IBOutlet var displayTimer: WKInterfaceTimer!
41-
4233
/// Label to display the most recent location's latitude.
4334
@IBOutlet var latitudeLabel: WKInterfaceLabel!
4435

@@ -70,29 +61,19 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
7061
return NSLocalizedString("Unexpected authorization status.", comment: "Text to indicate authorization status is an unexpected value")
7162
}
7263

73-
var latitudeResetText: String {
74-
return NSLocalizedString("<latitude reset>", comment: "String indicating that no latitude is shown to the user due to a timer reset")
75-
}
76-
77-
var longitudeResetText: String {
78-
return NSLocalizedString("<longitude reset>", comment: "String indicating that no longitude is shown to the user due to a timer reset")
79-
}
80-
81-
var errorResetText: String {
82-
return NSLocalizedString("<no error>", comment: "String indicating that no error is shown to the user")
83-
}
84-
8564
// MARK: Interface Controller
8665

8766
override func awakeWithContext(context: AnyObject?) {
8867
super.awakeWithContext(context)
8968

90-
self.setTitle(interfaceTitle)
69+
setTitle(interfaceTitle)
9170

9271
// Remember to set the location manager's delegate.
9372
manager.delegate = self
94-
95-
resetInterface()
73+
74+
latitudeLabel.setAlpha(0)
75+
longitudeLabel.setAlpha(0)
76+
errorLabel.setAlpha(0)
9677
}
9778

9879
/// MARK - Button Actions
@@ -127,12 +108,14 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
127108
manager.requestLocation()
128109

129110
case .Denied:
111+
errorLabel.setAlpha(1)
130112
errorLabel.setText(deniedText)
131-
restartTimers()
113+
simulateFadeOut(errorLabel)
132114

133115
default:
116+
errorLabel.setAlpha(1)
134117
errorLabel.setText(unexpectedText)
135-
restartTimers()
118+
simulateFadeOut(errorLabel)
136119
}
137120
}
138121

@@ -142,21 +125,27 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
142125
When the location manager receives new locations, display the latitude and
143126
longitude of the latest location and restart the timers.
144127
*/
145-
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
128+
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
146129
guard !locations.isEmpty else { return }
147130

148131
dispatch_async(dispatch_get_main_queue()) {
149-
let lastLocationCoordinate = locations.last!.coordinate!
150-
132+
let lastLocationCoordinate = locations.last!.coordinate
133+
151134
self.latitudeLabel.setText(String(lastLocationCoordinate.latitude))
152135

153136
self.longitudeLabel.setText(String(lastLocationCoordinate.longitude))
154137

138+
self.latitudeLabel.setAlpha(1)
139+
140+
self.longitudeLabel.setAlpha(1)
141+
155142
self.isRequestingLocation = false
156143

157144
self.requestLocationButton.setTitle(self.requestLocationTitle)
158145

159-
self.restartTimers()
146+
self.simulateFadeOut(self.latitudeLabel)
147+
148+
self.simulateFadeOut(self.longitudeLabel)
160149
}
161150
}
162151

@@ -166,13 +155,15 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
166155
*/
167156
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
168157
dispatch_async(dispatch_get_main_queue()) {
158+
self.errorLabel.setAlpha(1)
159+
169160
self.errorLabel.setText(String(error.localizedDescription))
170161

171162
self.isRequestingLocation = false
172163

173164
self.requestLocationButton.setTitle(self.requestLocationTitle)
174165

175-
self.restartTimers()
166+
self.simulateFadeOut(self.errorLabel)
176167
}
177168
}
178169

@@ -189,63 +180,39 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
189180
manager.requestLocation()
190181

191182
case .Denied:
183+
self.errorLabel.setAlpha(1)
192184
self.errorLabel.setText(self.deniedText)
193185
self.isRequestingLocation = false
194186
self.requestLocationButton.setTitle(self.requestLocationTitle)
195-
self.restartTimers()
187+
self.simulateFadeOut(self.errorLabel)
196188

197189
default:
190+
self.errorLabel.setAlpha(1)
198191
self.errorLabel.setText(self.unexpectedText)
199192
self.isRequestingLocation = false
200193
self.requestLocationButton.setTitle(self.requestLocationTitle)
201-
self.restartTimers()
194+
self.simulateFadeOut(self.errorLabel)
202195
}
203196
}
204197
}
205198

206199
/// MARK - Resetting
207200

208201
/**
209-
Resets the text labels in the interface to empty labels.
210-
211-
This method is useful for cleaning the interface to ensure that data
212-
displayed to the user is not stale.
202+
Simulates fading out animation by setting the alpha of the given label to
203+
progressively smaller numbers.
213204
*/
214-
func resetInterface() {
215-
dispatch_async(dispatch_get_main_queue()) {
216-
self.stopDisplayTimer()
217-
218-
self.latitudeLabel.setText(self.latitudeResetText)
219-
220-
self.longitudeLabel.setText(self.longitudeResetText)
221-
222-
self.errorLabel.setText(self.errorResetText)
223-
}
224-
}
225-
226-
/**
227-
Restarts the NSTimer and the WKInterface timer by stopping / invalidating
228-
them, then starting them with a 5 second timeout.
229-
*/
230-
func restartTimers() {
231-
stopDisplayTimer()
232-
233-
interfaceResetTimer.invalidate()
234-
235-
interfaceResetTimer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "resetInterface", userInfo: [:], repeats: false)
236-
237-
let fiveSecondDelay = NSDate(timeIntervalSinceNow: 5)
205+
func simulateFadeOut(label: WKInterfaceLabel) {
206+
let mainQueue = dispatch_get_main_queue()
238207

239-
displayTimer.setDate(fiveSecondDelay)
240-
241-
displayTimer.start()
242-
}
243-
244-
/// Stops the display timer.
245-
func stopDisplayTimer() {
246-
let now = NSDate()
247-
displayTimer.setDate(now)
208+
for index in 1...10 {
209+
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(index) / 10.0 * Double(NSEC_PER_SEC)))
210+
211+
dispatch_after(time, mainQueue) {
212+
let alphaAmount = CGFloat(1 - (0.1 * Float(index)))
248213

249-
displayTimer.stop()
214+
label.setAlpha(alphaAmount)
215+
}
216+
}
250217
}
251218
}

PotLoc/Potloc WatchKit Extension/StreamLocationInterfaceController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
2+
Copyright (C) 2016 Apple Inc. All Rights Reserved.
33
See LICENSE.txt for this sample’s licensing information
44

55
*/
@@ -83,7 +83,7 @@ class StreamLocationInterfaceController: WKInterfaceController, WCSessionDelegat
8383
override func awakeWithContext(context: AnyObject?) {
8484
super.awakeWithContext(context)
8585

86-
self.setTitle(interfaceTitle)
86+
setTitle(interfaceTitle)
8787
locationsReeivedOnPhoneCountTitleLabel.setText(locationsReceivedText)
8888

8989
// Initialize the `WCSession`.

PotLoc/Potloc.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,14 @@
494494
isa = XCBuildConfiguration;
495495
buildSettings = {
496496
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
497+
CODE_SIGN_IDENTITY = "iPhone Developer";
497498
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
498499
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
499500
IBSC_MODULE = Potloc_WatchKit_Extension;
500501
INFOPLIST_FILE = "Potloc WatchKit App/Info.plist";
501502
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc.nativewatchkitapp";
502503
PRODUCT_NAME = "$(TARGET_NAME)";
504+
PROVISIONING_PROFILE = "";
503505
SDKROOT = watchos;
504506
SKIP_INSTALL = YES;
505507
TARGETED_DEVICE_FAMILY = 4;
@@ -511,12 +513,14 @@
511513
isa = XCBuildConfiguration;
512514
buildSettings = {
513515
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
516+
CODE_SIGN_IDENTITY = "iPhone Developer";
514517
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
515518
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
516519
IBSC_MODULE = Potloc_WatchKit_Extension;
517520
INFOPLIST_FILE = "Potloc WatchKit App/Info.plist";
518521
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc.nativewatchkitapp";
519522
PRODUCT_NAME = "$(TARGET_NAME)";
523+
PROVISIONING_PROFILE = "";
520524
SDKROOT = watchos;
521525
SKIP_INSTALL = YES;
522526
TARGETED_DEVICE_FAMILY = 4;
@@ -528,6 +532,7 @@
528532
isa = XCBuildConfiguration;
529533
buildSettings = {
530534
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
535+
CODE_SIGN_IDENTITY = "iPhone Developer";
531536
INFOPLIST_FILE = Potloc/Info.plist;
532537
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
533538
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc";
@@ -539,6 +544,7 @@
539544
isa = XCBuildConfiguration;
540545
buildSettings = {
541546
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
547+
CODE_SIGN_IDENTITY = "iPhone Developer";
542548
INFOPLIST_FILE = Potloc/Info.plist;
543549
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
544550
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc";

PotLoc/Potloc/AppDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015 Apple Inc. All Rights Reserved.
2+
Copyright (C) 2016 Apple Inc. All Rights Reserved.
33
See LICENSE.txt for this sample’s licensing information
44

55
Abstract:

PotLoc/Potloc/Assets.xcassets/AppIcon.appiconset/Contents.json

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
"idiom" : "ipad",
7272
"filename" : "Potloc76x2.png",
7373
"scale" : "2x"
74+
},
75+
{
76+
"size" : "83.5x83.5",
77+
"idiom" : "ipad",
78+
"filename" : "Potloc83.5x2.png",
79+
"scale" : "2x"
7480
}
7581
],
7682
"info" : {
Loading

PotLoc/Potloc/Base.lproj/LaunchScreen.storyboard

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8121.17" systemVersion="15A177" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8121.20" systemVersion="15A210a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.13"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.16"/>
55
</dependencies>
66
<scenes>
77
<!--View Controller-->

0 commit comments

Comments
 (0)