From e431a54a89884b5e8e68c4eff0a87242c10476bb Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 2 Jun 2025 02:23:59 +0800 Subject: [PATCH] :bug: Fix NSE, get token from flutter side in native iOS --- ios/Runner.xcodeproj/project.pbxproj | 11 ++++++++- ios/Runner/AppDelegate.swift | 3 +++ ios/Runner/Info.plist | 5 ++++ ios/Runner/NotifyDelegate.swift | 13 +++------- ios/Runner/Services/DataExchange.swift | 31 ++++++++++++++++++++++++ ios/SolianNotificationService/Info.plist | 5 ++++ pubspec.yaml | 2 +- 7 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 ios/Runner/Services/DataExchange.swift diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 7123e13..d03e7fc 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 54; + objectVersion = 77; objects = { /* Begin PBXBuildFile section */ @@ -113,6 +113,7 @@ isa = PBXFileSystemSynchronizedBuildFileExceptionSet; membershipExceptions = ( CloudFile.swift, + DataExchange.swift, ); target = 73CDD6792DEC00480059D95D /* SolianNotificationService */; }; @@ -447,10 +448,14 @@ inputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-input-files.xcfilelist", ); + inputPaths = ( + ); name = "[CP] Copy Pods Resources"; outputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-output-files.xcfilelist", ); + outputPaths = ( + ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh\"\n"; @@ -486,10 +491,14 @@ inputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist", ); + inputPaths = ( + ); name = "[CP] Embed Pods Frameworks"; outputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist", ); + outputPaths = ( + ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 1f24ab4..ffe8a23 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -1,6 +1,8 @@ import Flutter import UIKit +import shared_preferences_foundation + @main @objc class AppDelegate: FlutterAppDelegate { let notifyDelegate = NotifyDelegate() @@ -10,6 +12,7 @@ import UIKit didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { UNUserNotificationCenter.current().delegate = notifyDelegate + GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index 49bb2b5..ac3c1d7 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -66,5 +66,10 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + NSUserActivityTypes + + INStartCallIntent + INSendMessageIntent + diff --git a/ios/Runner/NotifyDelegate.swift b/ios/Runner/NotifyDelegate.swift index b325ea6..21c4a59 100644 --- a/ios/Runner/NotifyDelegate.swift +++ b/ios/Runner/NotifyDelegate.swift @@ -16,17 +16,12 @@ class NotifyDelegate: UIResponder, UNUserNotificationCenterDelegate { return } - var token: String = "" - if let tokenJson = UserDefaults.standard.string(forKey: "dyn_user_tk"), - let tokenData = tokenJson.data(using: String.Encoding.utf8), - let tokenDict = try? JSONSerialization.jsonObject(with: tokenData) as? [String: Any], - let tokenValue = tokenDict["token"] as? String { - token = tokenValue - } else { + var token: String? = UserDefaults.standard.getFlutterToken() + if token == nil { return } - let serverUrl = "https://nt.solian.app" + let serverUrl = UserDefaults.standard.getServerUrl() let url = "\(serverUrl)/chat/\(metadata["room_id"] ?? "")/messages" let parameters: [String: Any?] = [ @@ -35,7 +30,7 @@ class NotifyDelegate: UIResponder, UNUserNotificationCenterDelegate { ] AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: HTTPHeaders( - [HTTPHeader(name: "Authorization", value: "AtField \(token)")] + [HTTPHeader(name: "Authorization", value: "AtField \(token!)")] )) .validate() .responseString { response in diff --git a/ios/Runner/Services/DataExchange.swift b/ios/Runner/Services/DataExchange.swift new file mode 100644 index 0000000..6cb7631 --- /dev/null +++ b/ios/Runner/Services/DataExchange.swift @@ -0,0 +1,31 @@ +// +// DataExchange.swift +// Runner +// +// Created by LittleSheep on 2025/6/2. +// + +import Foundation + +extension UserDefaults { + func getFlutterValue(forKey key: String) -> T? { + let prefixedKey = "flutter.\(key)" + return self.object(forKey: prefixedKey) as? T + } + + func getFlutterToken(forKey key: String = "dyn_user_tk") -> String? { + let prefixedKey = "flutter.\(key)" + guard let jsonString = self.string(forKey: prefixedKey), + let data = jsonString.data(using: .utf8), + let jsonObject = try? JSONSerialization.jsonObject(with: data), + let jsonDict = jsonObject as? [String: Any], + let token = jsonDict["token"] as? String else { + return nil + } + return token + } + + func getServerUrl(forKey key: String = "app_server_url") -> String { + return self.getFlutterValue(forKey: key) ?? "https://nt.solian.app" + } +} diff --git a/ios/SolianNotificationService/Info.plist b/ios/SolianNotificationService/Info.plist index 57421eb..a9e56d5 100644 --- a/ios/SolianNotificationService/Info.plist +++ b/ios/SolianNotificationService/Info.plist @@ -2,6 +2,11 @@ + NSUserActivityTypes + + INStartCallIntent + INSendMessageIntent + NSExtension NSExtensionPointIdentifier diff --git a/pubspec.yaml b/pubspec.yaml index 705fd6f..766a814 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 3.0.0+100 +version: 3.0.0+101 environment: sdk: ^3.7.2