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

Commit 280ef02

Browse files
authored
Update UpdaterView.swift
1 parent 5683774 commit 280ef02

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

Sources/prostore/views/UpdaterView.swift

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,12 @@ struct UpdaterWebView: UIViewRepresentable {
2424

2525
let webView = WKWebView(frame: .zero, configuration: config)
2626
webView.navigationDelegate = context.coordinator
27-
webView.uiDelegate = context.coordinator
2827
webView.allowsBackForwardNavigationGestures = true
29-
30-
// Add pull-to-refresh
31-
let refreshControl = UIRefreshControl()
32-
refreshControl.addTarget(context.coordinator, action: #selector(Coordinator.refreshWebView(_:)), for: .valueChanged)
33-
webView.scrollView.refreshControl = refreshControl
3428

3529
// Clear cache completely
3630
let dataTypes = Set([WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeCookies])
3731
WKWebsiteDataStore.default().removeData(ofTypes: dataTypes, modifiedSince: Date.distantPast) {
3832
print("✅ WKWebView cache fully cleared!")
39-
// Load the request after clearing cache
4033
let request = URLRequest(url: self.url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
4134
webView.load(request)
4235
}
@@ -45,15 +38,14 @@ struct UpdaterWebView: UIViewRepresentable {
4538
}
4639

4740
func updateUIView(_ uiView: WKWebView, context: Context) {
48-
// Reload only if URL changed
4941
if uiView.url != url {
5042
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
5143
uiView.load(request)
5244
}
5345
}
5446

5547
// MARK: - Coordinator
56-
class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
48+
class Coordinator: NSObject, WKNavigationDelegate {
5749
let parent: UpdaterWebView
5850

5951
init(parent: UpdaterWebView) {
@@ -62,33 +54,30 @@ struct UpdaterWebView: UIViewRepresentable {
6254
requestNotificationPermission()
6355
}
6456

65-
// Pull-to-refresh handler
66-
@objc func refreshWebView(_ sender: UIRefreshControl) {
67-
if let webView = sender.superview as? WKWebView {
68-
let request = URLRequest(url: parent.url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
69-
webView.load(request)
70-
}
71-
sender.endRefreshing()
72-
}
73-
74-
// Request notification permissions
7557
private func requestNotificationPermission() {
7658
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
7759
if granted {
7860
print("🔔 Notifications allowed!")
61+
} else if let error = error {
62+
print("❌ Notification permission error: \(error)")
7963
}
8064
}
8165
}
8266

83-
// Send local notification
8467
private func sendUpdateNotification() {
8568
let content = UNMutableNotificationContent()
8669
content.title = "ProStore Update"
8770
content.body = "ProStore will now update!"
8871
content.sound = .default
8972

9073
let request = UNNotificationRequest(identifier: "ProStoreUpdate", content: content, trigger: nil)
91-
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
74+
UNUserNotificationCenter.current().add(request) { error in
75+
if let error = error {
76+
print("❌ Failed to schedule notification: \(error)")
77+
} else {
78+
print("✅ Notification scheduled!")
79+
}
80+
}
9281
}
9382

9483
// MARK: Navigation Delegate
@@ -102,17 +91,29 @@ struct UpdaterWebView: UIViewRepresentable {
10291

10392
let scheme = reqURL.scheme?.lowercased() ?? ""
10493

105-
// 1️⃣ itms-services: open in system & send push
94+
// Handle itms-services
10695
if scheme == "itms-services" {
10796
if UIApplication.shared.canOpenURL(reqURL) {
108-
UIApplication.shared.open(reqURL, options: [:], completionHandler: nil)
109-
sendUpdateNotification()
97+
// Open Apple install dialog
98+
UIApplication.shared.open(reqURL, options: [:]) { success in
99+
if success {
100+
// Minimise app to home screen
101+
DispatchQueue.main.async {
102+
// Trick to go home
103+
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
104+
// Send push notification after a tiny delay to ensure we're on Home Screen
105+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
106+
self.sendUpdateNotification()
107+
}
108+
}
109+
}
110+
}
110111
}
111112
decisionHandler(.cancel)
112113
return
113114
}
114115

115-
// 2️⃣ target="_blank": open externally
116+
// Open external links in Safari
116117
if navigationAction.navigationType == .linkActivated, navigationAction.targetFrame == nil {
117118
UIApplication.shared.open(reqURL, options: [:], completionHandler: nil)
118119
decisionHandler(.cancel)

0 commit comments

Comments
 (0)