Skip to content

Commit 733ca08

Browse files
improve network usage (#120)
* enhances http connections * wip * Update project.pbxproj * Update Prey.entitlements * Update project.pbxproj * reports even without location * Update project.pbxproj * fix alert * Update Alert.swift
1 parent d48130b commit 733ca08

File tree

8 files changed

+274
-176
lines changed

8 files changed

+274
-176
lines changed

Prey.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@
10571057
CODE_SIGN_IDENTITY = "Apple Development";
10581058
CODE_SIGN_INJECT_BASE_ENTITLEMENTS = YES;
10591059
CODE_SIGN_STYLE = Automatic;
1060-
CURRENT_PROJECT_VERSION = 12;
1060+
CURRENT_PROJECT_VERSION = 8;
10611061
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
10621062
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
10631063
INFOPLIST_FILE = "$(SRCROOT)/Prey/Plist/Info.plist";
@@ -1069,7 +1069,7 @@
10691069
"@executable_path/Frameworks",
10701070
);
10711071
LIBRARY_SEARCH_PATHS = "$(inherited)";
1072-
MARKETING_VERSION = 2.2.3;
1072+
MARKETING_VERSION = 2.2.4;
10731073
OTHER_SWIFT_FLAGS = "$(inherited)";
10741074
PRODUCT_BUNDLE_IDENTIFIER = com.prey;
10751075
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -1093,7 +1093,7 @@
10931093
CODE_SIGN_IDENTITY = "Apple Development";
10941094
CODE_SIGN_INJECT_BASE_ENTITLEMENTS = YES;
10951095
CODE_SIGN_STYLE = Automatic;
1096-
CURRENT_PROJECT_VERSION = 12;
1096+
CURRENT_PROJECT_VERSION = 8;
10971097
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
10981098
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "x86_64 i386";
10991099
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
@@ -1106,7 +1106,7 @@
11061106
"@executable_path/Frameworks",
11071107
);
11081108
LIBRARY_SEARCH_PATHS = "$(inherited)";
1109-
MARKETING_VERSION = 2.2.3;
1109+
MARKETING_VERSION = 2.2.4;
11101110
OTHER_SWIFT_FLAGS = "$(inherited)";
11111111
PRODUCT_BUNDLE_IDENTIFIER = com.prey;
11121112
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -1202,7 +1202,7 @@
12021202
"CODE_SIGN_ENTITLEMENTS[sdk=*]" = "";
12031203
CODE_SIGN_IDENTITY = "Apple Development";
12041204
CODE_SIGN_STYLE = Automatic;
1205-
CURRENT_PROJECT_VERSION = 12;
1205+
CURRENT_PROJECT_VERSION = 8;
12061206
DEBUG_INFORMATION_FORMAT = dwarf;
12071207
GCC_C_LANGUAGE_STANDARD = gnu11;
12081208
INFOPLIST_FILE = PreyNotify/Info.plist;
@@ -1212,7 +1212,7 @@
12121212
"@executable_path/Frameworks",
12131213
"@executable_path/../../Frameworks",
12141214
);
1215-
MARKETING_VERSION = 2.2.3;
1215+
MARKETING_VERSION = 2.2.4;
12161216
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
12171217
MTL_FAST_MATH = YES;
12181218
PRODUCT_BUNDLE_IDENTIFIER = com.prey.NotifyPrey;
@@ -1243,7 +1243,7 @@
12431243
CODE_SIGN_IDENTITY = "Apple Development";
12441244
CODE_SIGN_STYLE = Automatic;
12451245
COPY_PHASE_STRIP = NO;
1246-
CURRENT_PROJECT_VERSION = 12;
1246+
CURRENT_PROJECT_VERSION = 8;
12471247
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
12481248
GCC_C_LANGUAGE_STANDARD = gnu11;
12491249
INFOPLIST_FILE = PreyNotify/Info.plist;
@@ -1253,7 +1253,7 @@
12531253
"@executable_path/Frameworks",
12541254
"@executable_path/../../Frameworks",
12551255
);
1256-
MARKETING_VERSION = 2.2.3;
1256+
MARKETING_VERSION = 2.2.4;
12571257
MTL_FAST_MATH = YES;
12581258
PRODUCT_BUNDLE_IDENTIFIER = com.prey.NotifyPrey;
12591259
PRODUCT_NAME = "$(TARGET_NAME)";
Binary file not shown.

Prey/Classes/Alert.swift

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,37 @@ class Alert: PreyAction, @unchecked Sendable {
2929

3030
PreyLogger("Alert message: \(message)")
3131

32-
// Send start action immediately to avoid duplicates
32+
// Send start action and, upon completion, send stopped to close lifecycle in order
3333
let params = getParamsTo(kAction.alert.rawValue, command: kCommand.start.rawValue, status: kStatus.started.rawValue)
34-
self.sendData(params, toEndpoint: responseDeviceEndpoint)
34+
if let username = PreyConfig.sharedInstance.userApiKey, PreyConfig.sharedInstance.isRegistered {
35+
PreyHTTPClient.sharedInstance.userRegisterToPrey(
36+
username,
37+
password: "x",
38+
params: params,
39+
messageId: self.messageId,
40+
httpMethod: Method.POST.rawValue,
41+
endPoint: responseDeviceEndpoint,
42+
onCompletion: PreyHTTPResponse.checkResponse(
43+
RequestType.dataSend,
44+
preyAction: self,
45+
onCompletion: { [weak self] (_: Bool) in
46+
guard let self = self else { return }
47+
let stopParams = self.getParamsTo(kAction.alert.rawValue, command: kCommand.start.rawValue, status: kStatus.stopped.rawValue)
48+
PreyHTTPClient.sharedInstance.userRegisterToPrey(
49+
username,
50+
password: "x",
51+
params: stopParams,
52+
messageId: self.messageId,
53+
httpMethod: Method.POST.rawValue,
54+
endPoint: responseDeviceEndpoint,
55+
onCompletion: PreyHTTPResponse.checkResponse(RequestType.dataSend, preyAction: self, onCompletion: { _ in PreyLogger("Alert start->stop cycle sent") })
56+
)
57+
}
58+
)
59+
)
60+
} else {
61+
PreyLogger("Alert: cannot send start/stop - missing API key or not registered")
62+
}
3563

3664
// Only show a notification in background, otherwise show the alert view
3765
// Fix: Check app state on main thread to avoid Main Thread Checker warning
@@ -73,12 +101,6 @@ class Alert: PreyAction, @unchecked Sendable {
73101
UNUserNotificationCenter.current().add(request) { [weak self] error in
74102
if let error = error {
75103
PreyLogger("Error displaying notification: \(error.localizedDescription)")
76-
77-
// Send stopped status if there was an error
78-
if let self = self {
79-
let errorParams = self.getParamsTo(kAction.alert.rawValue, command: kCommand.start.rawValue, status: kStatus.stopped.rawValue)
80-
self.sendData(errorParams, toEndpoint: responseDeviceEndpoint)
81-
}
82104
} else {
83105
PreyLogger("Alert notification scheduled successfully")
84106
}
@@ -87,15 +109,6 @@ class Alert: PreyAction, @unchecked Sendable {
87109
// In foreground, just show the alert view
88110
showAlertVC(message)
89111
}
90-
91-
// Use a background queue with a delay instead of sleeping on the main thread
92-
// Use weak self to prevent retain cycles and memory leaks
93-
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 4.0) { [weak self] in
94-
guard let self = self else { return }
95-
let paramsStopped = self.getParamsTo(kAction.alert.rawValue, command: kCommand.start.rawValue, status: kStatus.stopped.rawValue)
96-
self.sendData(paramsStopped, toEndpoint: responseDeviceEndpoint)
97-
PreyLogger("Alert action completed")
98-
}
99112
}
100113

101114
// Show AlertVC

Prey/Classes/AppDelegate.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
179179
if PreyConfig.sharedInstance.isRegistered {
180180
PreyNotification.sharedInstance.registerForRemoteNotifications() // Might be redundant with previous call, but safe
181181
TriggerManager.sharedInstance.checkTriggers()
182+
// Sync device name if it changed
183+
PreyModule.sharedInstance.syncDeviceNameIfChanged()
182184

183185
// Handle notification if app was launched from a notification
184186
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
@@ -309,6 +311,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
309311
if PreyConfig.sharedInstance.isRegistered {
310312
// Perform immediate sync with server
311313
syncWithServer() // This will also setup the foreground timer
314+
// Sync device name if it changed
315+
PreyModule.sharedInstance.syncDeviceNameIfChanged()
312316
}
313317

314318
// Various UI state checks and re-display logic. Fine as is.
@@ -713,9 +717,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
713717
return
714718
}
715719

716-
// Forward handling to PreyNotification
717-
PreyNotification.sharedInstance.handleNotificationResponse(response)
718-
720+
// Ignore dismiss actions to avoid triggering 'action started' on swipe-to-clear
721+
let actionId = response.actionIdentifier
722+
if actionId == UNNotificationDismissActionIdentifier || actionId == "DISMISS_ACTION" {
723+
PreyLogger("Notification dismissed by user; no action will be triggered")
724+
completionHandler()
725+
return
726+
}
727+
728+
// Only handle default tap or explicit view action
729+
if actionId == UNNotificationDefaultActionIdentifier || actionId == "VIEW_ACTION" {
730+
PreyNotification.sharedInstance.handleNotificationResponse(response)
731+
} else {
732+
PreyLogger("Unhandled notification action: \(actionId); ignoring")
733+
}
734+
719735
completionHandler() // Call completion handler promptly
720736
}
721737

@@ -931,3 +947,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
931947
}
932948
}
933949
}
950+
// Bridge background URLSession completion handler to HTTP client (for report uploads etc.)
951+
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
952+
PreyLogger("handleEventsForBackgroundURLSession: \(identifier)")
953+
PreyHTTPClient.sharedInstance.registerBackgroundCompletionHandler(completionHandler)
954+
}

0 commit comments

Comments
 (0)