Skip to content

Commit 9c4e889

Browse files
committed
Added initial data usage tracking - only displayed in print statements so far.
1 parent 933704f commit 9c4e889

File tree

9 files changed

+94
-6
lines changed

9 files changed

+94
-6
lines changed

IP Address WatchOS Extension/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>105</string>
22+
<string>112</string>
2323
<key>NSExtension</key>
2424
<dict>
2525
<key>NSExtensionAttributes</key>

IP Address WatchOS/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>105</string>
22+
<string>112</string>
2323
<key>UISupportedInterfaceOrientations</key>
2424
<array>
2525
<string>UIInterfaceOrientationPortrait</string>

IP Address.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
AAE235271F91223400FB8E69 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AAE235261F91223400FB8E69 /* Assets.xcassets */; };
2424
AAE2352A1F91223400FB8E69 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AAE235281F91223400FB8E69 /* LaunchScreen.storyboard */; };
2525
AAE235341F91254800FB8E69 /* Interface.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAE235331F91254800FB8E69 /* Interface.swift */; };
26+
AAE59D49221314D2008D5982 /* DataUsage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAE59D48221314D2008D5982 /* DataUsage.swift */; };
2627
F08E45FF03556FE3AD50F8D7 /* Pods_IP_Address.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E59AF8F9952F45C5DC035509 /* Pods_IP_Address.framework */; };
2728
/* End PBXBuildFile section */
2829

@@ -90,6 +91,7 @@
9091
AAE235291F91223400FB8E69 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
9192
AAE2352B1F91223400FB8E69 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9293
AAE235331F91254800FB8E69 /* Interface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Interface.swift; sourceTree = "<group>"; };
94+
AAE59D48221314D2008D5982 /* DataUsage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataUsage.swift; sourceTree = "<group>"; };
9395
AC8FF94B6EFEB74B4FE34994 /* Pods-IP Address.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-IP Address.release.xcconfig"; path = "Pods/Target Support Files/Pods-IP Address/Pods-IP Address.release.xcconfig"; sourceTree = "<group>"; };
9496
D0A5CED612EB906B8ABBB984 /* Pods-IP Address.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-IP Address.debug.xcconfig"; path = "Pods/Target Support Files/Pods-IP Address/Pods-IP Address.debug.xcconfig"; sourceTree = "<group>"; };
9597
E59AF8F9952F45C5DC035509 /* Pods_IP_Address.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_IP_Address.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -187,6 +189,7 @@
187189
AAE235261F91223400FB8E69 /* Assets.xcassets */,
188190
AAE235281F91223400FB8E69 /* LaunchScreen.storyboard */,
189191
AAE2352B1F91223400FB8E69 /* Info.plist */,
192+
AAE59D48221314D2008D5982 /* DataUsage.swift */,
190193
);
191194
path = "IP Address";
192195
sourceTree = "<group>";
@@ -404,6 +407,7 @@
404407
buildActionMask = 2147483647;
405408
files = (
406409
AAE235221F91223400FB8E69 /* ViewController.swift in Sources */,
410+
AAE59D49221314D2008D5982 /* DataUsage.swift in Sources */,
407411
AA12DD8F1FAB85F8004EB978 /* DNSlookup.swift in Sources */,
408412
AAE235201F91223400FB8E69 /* AppDelegate.swift in Sources */,
409413
AAE235341F91254800FB8E69 /* Interface.swift in Sources */,

IP Address/Base.lproj/LaunchScreen.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
2222
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2323
<subviews>
24-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 105 (5c974c5)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
24+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 112 (933704f)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
2525
<rect key="frame" x="132.5" y="527" width="55.5" height="21"/>
2626
<fontDescription key="fontDescription" type="system" pointSize="17"/>
2727
<nil key="textColor"/>

IP Address/Base.lproj/LaunchScreen.storyboardbak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
2222
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2323
<subviews>
24-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 104 (5c974c5)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
24+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Build: 111 (933704f)" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yhA-lX-4Of" userLabel="APP_VERSION">
2525
<rect key="frame" x="132.5" y="527" width="55.5" height="21"/>
2626
<fontDescription key="fontDescription" type="system" pointSize="17"/>
2727
<nil key="textColor"/>

IP Address/DataUsage.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// DataUsage.swift
3+
// IP Address
4+
//
5+
// Created by Steve on 12/02/2019.
6+
// Copyright © 2019 Steve. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
13+
struct DataUsageInfo {
14+
var wifiReceived: UInt32 = 0
15+
var wifiSent: UInt32 = 0
16+
var wirelessWanDataReceived: UInt32 = 0
17+
var wirelessWanDataSent: UInt32 = 0
18+
19+
mutating func updateInfoByAdding(info: DataUsageInfo) {
20+
wifiSent += info.wifiSent
21+
wifiReceived += info.wifiReceived
22+
wirelessWanDataSent += info.wirelessWanDataSent
23+
wirelessWanDataReceived += info.wirelessWanDataReceived
24+
}
25+
}
26+
27+
class DataUsage {
28+
29+
private static let wwanInterfacePrefix = "pdp_ip"
30+
private static let wifiInterfacePrefix = "en"
31+
32+
class func getDataUsage() -> DataUsageInfo {
33+
var interfaceAddresses: UnsafeMutablePointer<ifaddrs>? = nil
34+
35+
var dataUsageInfo = DataUsageInfo()
36+
37+
guard getifaddrs(&interfaceAddresses) == 0 else { return dataUsageInfo }
38+
39+
var pointer = interfaceAddresses
40+
while pointer != nil {
41+
guard let info = getDataUsageInfo(from: pointer!) else {
42+
pointer = pointer!.pointee.ifa_next
43+
continue
44+
}
45+
dataUsageInfo.updateInfoByAdding(info: info)
46+
pointer = pointer!.pointee.ifa_next
47+
}
48+
49+
freeifaddrs(interfaceAddresses)
50+
51+
return dataUsageInfo
52+
}
53+
54+
private class func getDataUsageInfo(from infoPointer: UnsafeMutablePointer<ifaddrs>) -> DataUsageInfo? {
55+
let pointer = infoPointer
56+
57+
let name: String! = String(cString: infoPointer.pointee.ifa_name)
58+
let addr = pointer.pointee.ifa_addr.pointee
59+
guard addr.sa_family == UInt8(AF_LINK) else { return nil }
60+
61+
return dataUsageInfo(from: pointer, name: name)
62+
}
63+
64+
private class func dataUsageInfo(from pointer: UnsafeMutablePointer<ifaddrs>, name: String) -> DataUsageInfo {
65+
var networkData: UnsafeMutablePointer<if_data>? = nil
66+
var dataUsageInfo = DataUsageInfo()
67+
68+
if name.hasPrefix(wifiInterfacePrefix) {
69+
networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
70+
dataUsageInfo.wifiSent += networkData?.pointee.ifi_obytes ?? 0
71+
dataUsageInfo.wifiReceived += networkData?.pointee.ifi_ibytes ?? 0
72+
} else if name.hasPrefix(wwanInterfacePrefix) {
73+
networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
74+
dataUsageInfo.wirelessWanDataSent += networkData?.pointee.ifi_obytes ?? 0
75+
dataUsageInfo.wirelessWanDataReceived += networkData?.pointee.ifi_ibytes ?? 0
76+
}
77+
78+
return dataUsageInfo
79+
}
80+
}

IP Address/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0</string>
1919
<key>CFBundleVersion</key>
20-
<string>105</string>
20+
<string>112</string>
2121
<key>ITSAppUsesNonExemptEncryption</key>
2222
<false/>
2323
<key>LSRequiresIPhoneOS</key>

IP Address/ViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,9 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
139139
}
140140
}
141141
}
142+
143+
let dataUsed = DataUsage.getDataUsage()
144+
print ("WiFi: ", dataUsed.wifiReceived + dataUsed.wifiSent)
145+
print ("Cellular:", dataUsed.wirelessWanDataReceived + dataUsed.wirelessWanDataSent)
142146
}
143147
}

build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
105
1+
112

0 commit comments

Comments
 (0)