From ad18a47d21bd136d04725421ffceacbeef6c3998 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Fri, 5 Dec 2025 13:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E6=AC=A1=E6=80=A7=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=86=85=E8=B4=AD=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keyBoard/AppDelegate.m | 12 ++++++++---- keyBoard/Class/Login/VM/KBLoginVM.m | 19 ++++++++++++++++--- .../Class/Vender/FGIAPService/FGIAPManager.m | 4 ++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/keyBoard/AppDelegate.m b/keyBoard/AppDelegate.m index f6fdbbf..ca1c582 100644 --- a/keyBoard/AppDelegate.m +++ b/keyBoard/AppDelegate.m @@ -36,13 +36,17 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - // 1. 配置内购服务 - [[FGIAPManager shared] setConfigureWith:[IAPVerifyTransactionObj new]]; - /// 2:配置国际化(统一使用集中管理的语言列表) + /// 1:配置国际化(统一使用集中管理的语言列表) [KBLocalizationManager shared].supportedLanguageCodes = KBDefaultSupportedLanguageCodes(); - /// 3 : 处理token问题 + /// 2 : 处理token问题(包括卸载重装场景下的 token 清理) [[KBUserSessionManager shared] bootstrapIfNeeded]; + /// 3:如果当前已经是登录状态,则在启动时初始化一次内购服务。 + /// 对于“首次登录”的场景,会在登录成功(见 KBLoginVM)后再进行一次配置。 + if ([KBUserSessionManager shared].isLoggedIn) { + [[FGIAPManager shared] setConfigureWith:[IAPVerifyTransactionObj new]]; + } + // 首次安装/升级:重置“完全访问”记录,避免继承旧安装遗留在 Keychain 中的状态 static NSString *const kKBFullAccessRecordInitializedKey = @"KBFullAccessRecordInitialized"; diff --git a/keyBoard/Class/Login/VM/KBLoginVM.m b/keyBoard/Class/Login/VM/KBLoginVM.m index 8064837..3c0e4f9 100644 --- a/keyBoard/Class/Login/VM/KBLoginVM.m +++ b/keyBoard/Class/Login/VM/KBLoginVM.m @@ -6,6 +6,8 @@ #import "AppleSignInManager.h" #import "KBNetworkManager.h" #import "KBAuthManager.h" +#import "FGIAPManager.h" +#import "IAPVerifyTransactionObj.h" #import "KBAPI.h" #import "KBUser.h" @@ -15,6 +17,14 @@ @implementation KBLoginVM +/// 统一初始化内购(只执行一次)。既可以在“启动时已登录”的场景调用,也可以在“登录成功回调”里调用。 ++ (void)kb_configureIAPIfNeeded { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + [[FGIAPManager shared] setConfigureWith:[IAPVerifyTransactionObj new]]; + }); +} + + (instancetype)shared { static KBLoginVM *vm; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ vm = [KBLoginVM new]; }); return vm; @@ -64,7 +74,8 @@ return; } [[KBUserSessionManager shared] handleLoginSuccessWithUser:user]; - completion(true,nil); + [KBLoginVM kb_configureIAPIfNeeded]; + if (completion) completion(YES, nil); // 保存登录态到共享钥匙串;供 App 与扩展共享 // BOOL ok = [[KBAuthManager shared] saveAccessToken:user.token // refreshToken:nil @@ -99,7 +110,8 @@ return; } [[KBUserSessionManager shared] handleLoginSuccessWithUser:user]; - completion(true,nil); + [KBLoginVM kb_configureIAPIfNeeded]; + if (completion) completion(YES, nil); }]; } @@ -128,7 +140,8 @@ return; } [[KBUserSessionManager shared] handleLoginSuccessWithUser:user]; - completion(true,nil); + [KBLoginVM kb_configureIAPIfNeeded]; + if (completion) completion(YES, nil); }]; } diff --git a/keyBoard/Class/Vender/FGIAPService/FGIAPManager.m b/keyBoard/Class/Vender/FGIAPService/FGIAPManager.m index cbdcada..f63604a 100644 --- a/keyBoard/Class/Vender/FGIAPService/FGIAPManager.m +++ b/keyBoard/Class/Vender/FGIAPService/FGIAPManager.m @@ -19,6 +19,10 @@ } - (void)setConfigureWith:(id)verifyTransaction{ + // 只初始化一次,避免重复向 SKPaymentQueue 注册多个观察者。 + if (self->_iap != nil) { + return; + } self->_iap = [[FGIAPService alloc] initWithTransaction:verifyTransaction]; }