W2A流程梳理

This commit is contained in:
zeng 2025-06-17 09:40:35 +08:00
parent d6e314a97f
commit 11cd8628aa
6 changed files with 64 additions and 17 deletions

View File

@ -10,6 +10,8 @@ import FirebaseMessaging
import FirebaseCore import FirebaseCore
extension AppDelegate { extension AppDelegate {
///
static var haveBeenShownAPNS = false
func registerAPNS() { func registerAPNS() {
FirebaseApp.configure() FirebaseApp.configure()
@ -20,8 +22,8 @@ extension AppDelegate {
center.requestAuthorization(options: [.badge, .sound, .alert]) { grant, error in center.requestAuthorization(options: [.badge, .sound, .alert]) { grant, error in
if !grant { if !grant {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
if let date = UserDefaults.standard.object(forKey: kSPApnsAlertDefaultsKey) as? Date { if let date = UserDefaults.standard.object(forKey: kSPApnsAlertDefaultsKey) as? Date {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
#if DEBUG #if DEBUG
self.showApnsAlert() self.showApnsAlert()
#else #else
@ -30,8 +32,15 @@ extension AppDelegate {
} }
#endif #endif
} }
UserDefaults.standard.set(Date(), forKey: kSPApnsAlertDefaultsKey) } else {
Self.haveBeenShownAPNS = true
SPAPPTool.sceneDelegate?.retryHandleOpenAppMessage()
} }
UserDefaults.standard.set(Date(), forKey: kSPApnsAlertDefaultsKey)
} else {
Self.haveBeenShownAPNS = true
SPAPPTool.sceneDelegate?.retryHandleOpenAppMessage()
} }
} }
UIApplication.shared.registerForRemoteNotifications() UIApplication.shared.registerForRemoteNotifications()

View File

@ -18,11 +18,14 @@ extension SceneDelegate {
guard let url = URLContexts.first?.url else { guard let url = URLContexts.first?.url else {
return return
} }
var result = false
#if canImport(FacebookCore) #if canImport(FacebookCore)
ApplicationDelegate.shared.application(UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation]) result = ApplicationDelegate.shared.application(UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation])
#endif #endif
if !result {
handleOpenAppMessage(webpageURL: url)
}
} }
@ -38,16 +41,44 @@ extension SceneDelegate {
extension SceneDelegate { extension SceneDelegate {
static var hasOpenMessage = false static var hasOpenMessage = false
///
static var isNeedRetry = false
private static var webpageURL: URL?
func handleOpenAppMessage(webpageURL: URL?) { func handleOpenAppMessage(webpageURL: URL?) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { guard SPNetworkReachabilityManager.manager.isReachable == true, AppDelegate.haveBeenShownAPNS, SPAPPTool.isAppOpen else {
if let webpageURL = webpageURL {
SceneDelegate.webpageURL = webpageURL
}
Self.isNeedRetry = true
return
}
Self.isNeedRetry = false
SceneDelegate.webpageURL = nil
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self._handleOpenAppMessage(webpageURL: webpageURL) self._handleOpenAppMessage(webpageURL: webpageURL)
} }
} }
///
func retryHandleOpenAppMessage() {
guard Self.isNeedRetry else { return }
handleOpenAppMessage(webpageURL: SceneDelegate.webpageURL)
}
private func _handleOpenAppMessage(webpageURL: URL?) { private func _handleOpenAppMessage(webpageURL: URL?) {
if !SPAPPTool.isAppOpen { return } if !SPAPPTool.isAppOpen { return }
if Self.hasOpenMessage { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
Self.hasOpenMessage = false
}
Self.hasOpenMessage = true
//URL //URL
var statUrlStr: String? var statUrlStr: String?
var data: [String : Any]? var data: [String : Any]?
@ -63,14 +94,7 @@ extension SceneDelegate {
statUrlStr = pasteStr statUrlStr = pasteStr
} }
} }
if Self.hasOpenMessage {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
Self.hasOpenMessage = false
}
Self.hasOpenMessage = true
if data == nil { if data == nil {

View File

@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SPAPPTool.appDelegate = self
self.registThirdparty(application, didFinishLaunchingWithOptions: launchOptions) self.registThirdparty(application, didFinishLaunchingWithOptions: launchOptions)

View File

@ -15,7 +15,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return } guard let windowScene = (scene as? UIWindowScene) else { return }
SPAPPTool.sceneDelegate = self
SPAPPTool.windowScene = windowScene SPAPPTool.windowScene = windowScene
/// ///
@ -25,6 +25,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
/// ///
// NotificationCenter.default.addObserver(self, selector: #selector(loginStateDidChangeNotification), name: SPLoginManager.loginStateDidChangeNotification, object: nil) // NotificationCenter.default.addObserver(self, selector: #selector(loginStateDidChangeNotification), name: SPLoginManager.loginStateDidChangeNotification, object: nil)
if let webpageURL = connectionOptions.userActivities.first?.webpageURL {
self.handleOpenAppMessage(webpageURL: webpageURL)
}
window = UIWindow(windowScene: windowScene) window = UIWindow(windowScene: windowScene)
@ -60,7 +63,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func sceneWillEnterForeground(_ scene: UIScene) { func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground. // Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background. // Use this method to undo the changes made on entering the background.
handleOpenAppMessage(webpageURL: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.handleOpenAppMessage(webpageURL: nil)
}
} }
func sceneDidEnterBackground(_ scene: UIScene) { func sceneDidEnterBackground(_ scene: UIScene) {
@ -131,7 +137,7 @@ extension SceneDelegate {
@objc private func handleOpenApp() { @objc private func handleOpenApp() {
setTabBarController() setTabBarController()
handleOpenAppMessage(webpageURL: nil) retryHandleOpenAppMessage()
} }
@ -147,6 +153,8 @@ extension SceneDelegate {
/// ///
@objc private func reachabilityDidChangeNotification() { @objc private func reachabilityDidChangeNotification() {
retryHandleOpenAppMessage()
let localizedData = SPLocalizedManager.shared.localizedData ?? [:] let localizedData = SPLocalizedManager.shared.localizedData ?? [:]
if SPNetworkReachabilityManager.manager.isReachable == true { if SPNetworkReachabilityManager.manager.isReachable == true {

View File

@ -90,9 +90,12 @@ class SPApnsAlertView: UIView {
@objc private func handleLaterButton() { @objc private func handleLaterButton() {
self.removeFromSuperview() self.removeFromSuperview()
AppDelegate.haveBeenShownAPNS = true
SPAPPTool.sceneDelegate?.retryHandleOpenAppMessage()
} }
@objc private func handleAllowButton() { @objc private func handleAllowButton() {
AppDelegate.haveBeenShownAPNS = true
self.removeFromSuperview() self.removeFromSuperview()
SPAPPTool.openApnsSetting() SPAPPTool.openApnsSetting()
} }

View File

@ -12,6 +12,9 @@ class SPAPPTool: NSObject {
///app ///app
static var isAppOpen = true static var isAppOpen = true
static var appDelegate: AppDelegate?
static var sceneDelegate: SceneDelegate?
static var windowScene: UIWindowScene? static var windowScene: UIWindowScene?
static var mainTabBarController: SPTabBarController? static var mainTabBarController: SPTabBarController?