Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit a14ac53

Browse files
authored
Refactor UpdaterView and add notification support
Updated UpdaterView and UpdaterWebView to include notification handling and cache clearing.
1 parent b05fd88 commit a14ac53

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

Sources/prostore/views/UpdaterView.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ struct UpdaterWebView: UIViewRepresentable {
1717
}
1818

1919
func makeUIView(context: Context) -> WKWebView {
20-
// Create WKWebView with configuration
2120
let config = WKWebViewConfiguration()
2221
config.preferences.javaScriptEnabled = true
2322
config.allowsInlineMediaPlayback = true
@@ -26,14 +25,18 @@ struct UpdaterWebView: UIViewRepresentable {
2625
webView.navigationDelegate = context.coordinator
2726
webView.allowsBackForwardNavigationGestures = true
2827

29-
// Clear cache completely
28+
// Clear cache
3029
let dataTypes = Set([WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeCookies])
3130
WKWebsiteDataStore.default().removeData(ofTypes: dataTypes, modifiedSince: Date.distantPast) {
32-
print("✅ WKWebView cache fully cleared!")
31+
print("✅ WKWebView cache cleared!")
3332
let request = URLRequest(url: self.url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
3433
webView.load(request)
3534
}
3635

36+
// Setup notification delegate
37+
UNUserNotificationCenter.current().delegate = context.coordinator
38+
context.coordinator.requestNotificationPermission()
39+
3740
return webView
3841
}
3942

@@ -45,25 +48,26 @@ struct UpdaterWebView: UIViewRepresentable {
4548
}
4649

4750
// MARK: - Coordinator
48-
class Coordinator: NSObject, WKNavigationDelegate {
51+
class Coordinator: NSObject, WKNavigationDelegate, UNUserNotificationCenterDelegate {
4952
let parent: UpdaterWebView
5053

5154
init(parent: UpdaterWebView) {
5255
self.parent = parent
5356
super.init()
54-
requestNotificationPermission()
5557
}
5658

57-
private func requestNotificationPermission() {
59+
// Request permission
60+
func requestNotificationPermission() {
5861
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
5962
if granted {
6063
print("🔔 Notifications allowed!")
6164
} else if let error = error {
62-
print("❌ Notification permission error: \(error)")
65+
print("❌ Notification error: \(error)")
6366
}
6467
}
6568
}
6669

70+
// Send local notification
6771
private func sendUpdateNotification() {
6872
let content = UNMutableNotificationContent()
6973
content.title = "ProStore Update"
@@ -80,6 +84,14 @@ struct UpdaterWebView: UIViewRepresentable {
8084
}
8185
}
8286

87+
// Show notification even if app is foreground
88+
func userNotificationCenter(_ center: UNUserNotificationCenter,
89+
willPresent notification: UNNotification,
90+
withCompletionHandler completionHandler:
91+
@escaping (UNNotificationPresentationOptions) -> Void) {
92+
completionHandler([.banner, .sound])
93+
}
94+
8395
// MARK: Navigation Delegate
8496
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
8597
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
@@ -94,14 +106,12 @@ struct UpdaterWebView: UIViewRepresentable {
94106
// Handle itms-services
95107
if scheme == "itms-services" {
96108
if UIApplication.shared.canOpenURL(reqURL) {
97-
// Open Apple install dialog
98109
UIApplication.shared.open(reqURL, options: [:]) { success in
99110
if success {
100-
// Minimise app to home screen
111+
// Minimise app
101112
DispatchQueue.main.async {
102-
// Trick to go home
103113
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
104-
// Send push notification after a tiny delay to ensure we're on Home Screen
114+
// Small delay to ensure app is backgrounded
105115
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
106116
self.sendUpdateNotification()
107117
}
@@ -113,7 +123,7 @@ struct UpdaterWebView: UIViewRepresentable {
113123
return
114124
}
115125

116-
// Open external links in Safari
126+
// External links
117127
if navigationAction.navigationType == .linkActivated, navigationAction.targetFrame == nil {
118128
UIApplication.shared.open(reqURL, options: [:], completionHandler: nil)
119129
decisionHandler(.cancel)

0 commit comments

Comments
 (0)