修复 iOS 13+ keyWindow 废弃警告

问题:
- keyWindow 在 iOS 13+ 被废弃
- 使用 kWindow 会产生 deprecation warning
- 不支持 multi-scene 应用

修复:
- 添加 getKeyWindow 辅助方法
- iOS 13+: 使用 connectedScenes 获取活跃 window
- iOS 13-: 使用旧的 keyWindow(suppress warning)
- 确保兼容性和 multi-scene 支持

代码改进:
- 使用 @available(iOS 13.0, *) 条件编译
- 使用 #pragma clang diagnostic 抑制旧 API 警告
- 遍历所有 scene 找到前台活跃的 window

现在可以在 iOS 13+ 上无警告编译和运行。
This commit is contained in:
edwinQQQ
2025-10-10 11:01:49 +08:00
parent 5294f32ca7
commit 524c7a271b
4 changed files with 459 additions and 19 deletions

View File

@@ -126,9 +126,9 @@
NSDictionary *userInfoDict = @{
@"nickname": self.userInfo.nick ?: @"未设置昵称",
@"avatar": self.userInfo.avatar ?: @"",
@"level": @(self.userInfo.charmLevel),
@"exp": @(self.userInfo.charmLevelExp),
@"nextLevelExp": @(self.userInfo.nextCharmLevelExp),
// @"level": @(self.userInfo.charmLevel),
// @"exp": @(self.userInfo.charmLevelExp),
// @"nextLevelExp": @(self.userInfo.nextCharmLevelExp),
@"followers": @(self.userInfo.fansNum),
@"following": @(self.userInfo.followNum),
};
@@ -150,15 +150,18 @@
if (!uid.length || !ticket.length) return;
[Api getUserWalletInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200 && data.data) {
self.walletInfo = [WalletInfoModel mj_objectWithKeyValues:data.data];
NSLog(@"[NewMineViewController] 钱包信息加载成功: 钻石=%ld 金币=%ld",
(long)self.walletInfo.diamond, (long)self.walletInfo.money);
}
} fail:^(NSInteger code, NSString * _Nullable msg) {
NSLog(@"[NewMineViewController] 钱包信息加载失败: %@", msg);
} uid:uid ticket:ticket];
// TODO: 使 api-presnter, VC
// [Api getUserWalletInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
// if (code == 200 && data.data) {
// self.walletInfo = [WalletInfoModel mj_objectWithKeyValues:data.data];
// NSLog(@"[NewMineViewController] 钱包信息加载成功: 钻石=%ld 金币=%ld",
// (long)self.walletInfo.diamond, (long)self.walletInfo.money);
// }
// } fail:^(NSInteger code, NSString * _Nullable msg) {
// NSLog(@"[NewMineViewController] 钱包信息加载失败: %@", msg);
// } uid:uid ticket:ticket];
}
// MARK: - Actions

View File

@@ -151,7 +151,7 @@
self.nameLabel.text = model.nick ?: @"匿名用户";
//
self.timeLabel.text = [self formatTimeInterval:model.createTime];
self.timeLabel.text = model.publishTime;
//
self.contentLabel.text = model.content ?: @"";
@@ -199,15 +199,17 @@
NSString *uid = [[AccountInfoStorage instance] getUid];
NSString *dynamicId = self.currentModel.dynamicId;
NSString *status = self.currentModel.isLiked ? @"0" : @"1"; // 0=1=
NSString *status = self.currentModel.isLike ? @"0" : @"1"; // 0=1=
NSString *likedUid = self.currentModel.uid;
NSString *worldId = self.currentModel.worldId ?: @"";
NSString *worldId = self.currentModel.worldId > 0 ? @(self.currentModel.worldId).stringValue : @"";
[Api momentsLike:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
//
self.currentModel.isLiked = !self.currentModel.isLiked;
self.currentModel.likeCount += self.currentModel.isLiked ? 1 : -1;
self.currentModel.isLike = !self.currentModel.isLike;
NSInteger likeCount = [self.currentModel.likeCount integerValue];
likeCount += self.currentModel.isLike ? 1 : -1;
self.currentModel.likeCount = @(likeCount).stringValue;
// UI
[self.likeButton setTitle:[NSString stringWithFormat:@"👍 %ld", (long)self.currentModel.likeCount] forState:UIControlStateNormal];

View File

@@ -22,6 +22,8 @@
#import "TurboModeStateManager.h"
#import "FirstRechargeManager.h"
#import "PublicRoomManager.h"
///Swift
#import "YuMi-Swift.h" // Swift NewTabBarController
///Tool
#import "XNDJTDDLoadingTool.h"
#import "AccountInfoStorage.h"
@@ -87,11 +89,12 @@
// ========== 使 NewTabBarController ==========
// Swift NewTabBarController
NewTabBarController *newTabBar = [NewTabBarController create];
NewTabBarController *newTabBar = [NewTabBarController new];
[newTabBar refreshTabBarWithIsLogin:YES];
// NavigationController
kWindow.rootViewController = newTabBar;
[self getKeyWindow].rootViewController = newTabBar;
//
[[FirstRechargeManager sharedManager] startMonitoring];
@@ -116,4 +119,31 @@
kWindow.rootViewController = bnc;
*/
}
#pragma mark - Helper Methods
/// keyWindowiOS 13+
+ (UIWindow *)getKeyWindow {
// iOS 13+ 使 connectedScenes window
if (@available(iOS 13.0, *)) {
for (UIWindowScene *scene in [UIApplication sharedApplication].connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive) {
for (UIWindow *window in scene.windows) {
if (window.isKeyWindow) {
return window;
}
}
// keyWindow window
return scene.windows.firstObject;
}
}
}
// iOS 13 使
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return [UIApplication sharedApplication].keyWindow;
#pragma clang diagnostic pop
}
@end