Skip to content

Commit 2fb514f

Browse files
Steve TreaseSteve Trease
authored andcommitted
Added Watch extension to show the Apple Watch's IP addresses...
1 parent 8a01301 commit 2fb514f

File tree

21 files changed

+1026
-6
lines changed

21 files changed

+1026
-6
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "watch",
5+
"screenWidth" : "{130,145}",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "watch",
10+
"screenWidth" : "{146,165}",
11+
"scale" : "2x"
12+
}
13+
],
14+
"info" : {
15+
"version" : 1,
16+
"author" : "xcode"
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"assets" : [
3+
{
4+
"idiom" : "watch",
5+
"filename" : "Circular.imageset",
6+
"role" : "circular"
7+
},
8+
{
9+
"idiom" : "watch",
10+
"filename" : "Extra Large.imageset",
11+
"role" : "extra-large"
12+
},
13+
{
14+
"idiom" : "watch",
15+
"filename" : "Modular.imageset",
16+
"role" : "modular"
17+
},
18+
{
19+
"idiom" : "watch",
20+
"filename" : "Utilitarian.imageset",
21+
"role" : "utilitarian"
22+
}
23+
],
24+
"info" : {
25+
"version" : 1,
26+
"author" : "xcode"
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "watch",
5+
"screenWidth" : "{130,145}",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "watch",
10+
"screenWidth" : "{146,165}",
11+
"scale" : "2x"
12+
}
13+
],
14+
"info" : {
15+
"version" : 1,
16+
"author" : "xcode"
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "watch",
5+
"screenWidth" : "{130,145}",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "watch",
10+
"screenWidth" : "{146,165}",
11+
"scale" : "2x"
12+
}
13+
],
14+
"info" : {
15+
"version" : 1,
16+
"author" : "xcode"
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "watch",
5+
"screenWidth" : "{130,145}",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "watch",
10+
"screenWidth" : "{146,165}",
11+
"scale" : "2x"
12+
}
13+
],
14+
"info" : {
15+
"version" : 1,
16+
"author" : "xcode"
17+
}
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// ExtensionDelegate.swift
3+
// IP Address WatchOS Extension
4+
//
5+
// Created by Steve on 21/12/2017.
6+
// Copyright © 2017 Steve. All rights reserved.
7+
//
8+
9+
import WatchKit
10+
11+
class ExtensionDelegate: NSObject, WKExtensionDelegate {
12+
13+
func applicationDidFinishLaunching() {
14+
// Perform any final initialization of your application.
15+
}
16+
17+
func applicationDidBecomeActive() {
18+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
19+
}
20+
21+
func applicationWillResignActive() {
22+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
23+
// Use this method to pause ongoing tasks, disable timers, etc.
24+
}
25+
26+
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
27+
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
28+
for task in backgroundTasks {
29+
// Use a switch statement to check the task type
30+
switch task {
31+
case let backgroundTask as WKApplicationRefreshBackgroundTask:
32+
// Be sure to complete the background task once you’re done.
33+
backgroundTask.setTaskCompletedWithSnapshot(false)
34+
case let snapshotTask as WKSnapshotRefreshBackgroundTask:
35+
// Snapshot tasks have a unique completion call, make sure to set your expiration date
36+
snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
37+
case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
38+
// Be sure to complete the connectivity task once you’re done.
39+
connectivityTask.setTaskCompletedWithSnapshot(false)
40+
case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
41+
// Be sure to complete the URL session task once you’re done.
42+
urlSessionTask.setTaskCompletedWithSnapshot(false)
43+
default:
44+
// make sure to complete unhandled task types
45+
task.setTaskCompletedWithSnapshot(false)
46+
}
47+
}
48+
}
49+
50+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// IPAddressInterfaceController.swift
3+
// IP Address WatchOS Extension
4+
//
5+
// Created by Steve on 21/12/2017.
6+
// Copyright © 2017 Steve. All rights reserved.
7+
//
8+
9+
import WatchKit
10+
import Foundation
11+
12+
13+
class IPAddressInterfaceController: WKInterfaceController {
14+
15+
@IBOutlet var IPAddressesTable: WKInterfaceTable!
16+
17+
private var interfaces = Interface.allInterfaces()
18+
19+
20+
21+
22+
override func awake(withContext context: Any?) {
23+
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
24+
super.awake(withContext: context)
25+
26+
self.updateView()
27+
}
28+
29+
30+
@IBAction func screenTappedTriggered(sender: AnyObject) {
31+
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
32+
WKInterfaceDevice.current().play(.click)
33+
self.updateView()
34+
}
35+
36+
37+
38+
override func willActivate() {
39+
// This method is called when watch view controller is about to be visible to user
40+
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
41+
super.willActivate()
42+
}
43+
44+
45+
46+
override func didDeactivate() {
47+
// This method is called when watch view controller is no longer visible
48+
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
49+
super.didDeactivate()
50+
}
51+
52+
53+
54+
func updateView () {
55+
print (NSURL (fileURLWithPath: "\(#file)").lastPathComponent!, "\(#function)")
56+
refreshSortAndFilterData()
57+
58+
IPAddressesTable.setNumberOfRows(interfaces.count, withRowType: "addressRowCell")
59+
60+
for index in 0..<interfaces.count {
61+
guard let controller = IPAddressesTable.rowController(at: index) as? IPAddressRowController else { continue }
62+
controller.interface = interfaces[index]
63+
}
64+
}
65+
66+
67+
68+
func refreshSortAndFilterData () {
69+
interfaces = Interface.allInterfaces()
70+
71+
var IPv4Interfaces = interfaces.filter { $0.family == .ipv4 }
72+
var IPv6Interfaces = interfaces.filter { $0.family == .ipv6 }
73+
74+
// print ("\(IPv4Interfaces.count) \(IPv6Interfaces.count) \(interfaces.count)")
75+
76+
IPv6Interfaces = IPv6Interfaces.filter { !($0.address?.hasPrefix ("fe80"))! }
77+
78+
interfaces = IPv4Interfaces + IPv6Interfaces
79+
80+
interfaces.sort {
81+
if $0.description == $1.description { return $0.address! < $1.address! }
82+
return $0.description < $1.description
83+
}
84+
85+
print ("Watch interfaces count = \(interfaces.count)")
86+
}
87+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// IPAddressRowController.swift
3+
// IP Address WatchOS Extension
4+
//
5+
// Created by Steve on 21/12/2017.
6+
// Copyright © 2017 Steve. All rights reserved.
7+
//
8+
9+
import WatchKit
10+
11+
class IPAddressRowController: NSObject {
12+
@IBOutlet var interfaceLabel: WKInterfaceLabel!
13+
@IBOutlet var IPAddressLabel: WKInterfaceLabel!
14+
15+
var interface: Interface? {
16+
didSet {
17+
guard let interface = interface else { return }
18+
19+
interfaceLabel.setText(interface.description)
20+
IPAddressLabel.setText(interface.address)
21+
}
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>IP Address WatchOS Extension</string>
9+
<key>CFBundleExecutable</key>
10+
<string>$(EXECUTABLE_NAME)</string>
11+
<key>CFBundleIdentifier</key>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>$(PRODUCT_NAME)</string>
17+
<key>CFBundlePackageType</key>
18+
<string>XPC!</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleVersion</key>
22+
<string>38</string>
23+
<key>NSExtension</key>
24+
<dict>
25+
<key>NSExtensionAttributes</key>
26+
<dict>
27+
<key>WKAppBundleIdentifier</key>
28+
<string>eu.trease.IP-Address.watchkitapp</string>
29+
</dict>
30+
<key>NSExtensionPointIdentifier</key>
31+
<string>com.apple.watchkit</string>
32+
</dict>
33+
<key>WKExtensionDelegateClassName</key>
34+
<string>$(PRODUCT_MODULE_NAME).ExtensionDelegate</string>
35+
</dict>
36+
</plist>

0 commit comments

Comments
 (0)