Files
real-e-party-iOS/YuMi/Global/YUMIMacroUitls.h
edwinQQQ 4256e01820 refactor: 统一获取 keyWindow 的实现,简化 Swift 代码
主要变更:
1. 移除 Swift 中重复实现的 getKeyWindow() 方法,统一调用 ObjC inline 函数 kGetKeyWindow()。
2. 更新 EPLoginManager 中的相关调用,确保一致性和简洁性。

此更新旨在提升代码的可维护性,减少冗余实现,确保跨语言调用的一致性。
2025-10-20 16:12:50 +08:00

127 lines
4.7 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// YMMacro.h
// YUMI
//
// Created by YUMI on 2021/9/10.
//
///一些宏
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "../Tools/Bundle/NSBundle+Localizable.h"
#ifndef YUMIMacroUitls_h
#define YUMIMacroUitls_h
#import "../Tools/Bundle/YMLanguageConfig.h"
//iPhoneX系列设备(刘海屏设备)
#define iPhoneXSeries \
({BOOL isPhoneXSeries = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneXSeries = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneXSeries);})
#define KScreenWidth [[UIScreen mainScreen] bounds].size.width
#define KScreenHeight [[UIScreen mainScreen] bounds].size.height
// 兼容 iOS 13+ 的状态栏高度获取
#define statusbarHeight ({\
CGFloat height = 0;\
if (@available(iOS 13.0, *)) {\
UIWindowScene *windowScene = (UIWindowScene *)[[[UIApplication sharedApplication] connectedScenes] allObjects].firstObject;\
height = windowScene.statusBarManager.statusBarFrame.size.height;\
} else {\
height = [[UIApplication sharedApplication] statusBarFrame].size.height;\
}\
height;\
})
#define kStatusBarHeight statusbarHeight
#define kSafeAreaBottomHeight (iPhoneXSeries ? 34 : 0)
#define kSafeAreaTopHeight (iPhoneXSeries ? 24 : 0)
#define kNavigationHeight (kStatusBarHeight + 44)
#define kTabBarHeight (iPhoneXSeries ? 49.0+34.0 : 49.0)
#define kScreenScale ((CGFloat)KScreenWidth / (CGFloat)375)
#define kScreenHeightScale ((CGFloat)KScreenHeight / (CGFloat)812)
#define kHalfScreenHeight KScreenHeight * 0.65
#define kGetScaleWidth(width) kRoundValue(width)
#define kRoundValue(value) round(kScreenScale * value)
#define kWeakify(o) try{}@finally{} __weak typeof(o) o##Weak = o;
#define kStrongify(o) autoreleasepool{} __strong typeof(o) o = o##Weak;
/// 兼容 iOS 13+ 的 keyWindow 获取Swift & ObjC 通用)
/// 使用此函数统一获取 keyWindow避免在各处重复实现
/// 自动处理 iOS 13+ 的 UIWindowScene 和旧版本的兼容性
static inline __attribute__((unused)) UIWindow * _Nullable kGetKeyWindow(void) {
UIWindow *window = nil;
if (@available(iOS 13.0, *)) {
for (UIWindowScene *scene in [UIApplication sharedApplication].connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive) {
for (UIWindow *w in scene.windows) {
if (w.isKeyWindow) {
return w;
}
}
if (scene.windows.firstObject) {
return scene.windows.firstObject;
}
}
}
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
window = [UIApplication sharedApplication].keyWindow;
#pragma clang diagnostic pop
}
return window;
}
// 兼容旧代码:保留宏入口
#define kWindow kGetKeyWindow()
#define kImage(image) [UIImage imageNamed:image]
///UIFont
#define kFontLight(font) [UIFont systemFontOfSize:kGetScaleWidth(font) weight:UIFontWeightLight]
#define kFontRegular(font) [UIFont systemFontOfSize:kGetScaleWidth(font) weight:UIFontWeightRegular]
#define kFontMedium(font) [UIFont systemFontOfSize:kGetScaleWidth(font) weight:UIFontWeightMedium]
#define kFontSemibold(font) [UIFont systemFontOfSize:kGetScaleWidth(font) weight:UIFontWeightSemibold]
#define kFontBold(font) [UIFont systemFontOfSize:kGetScaleWidth(font) weight:UIFontWeightBold]
#define kFontHeavy(font) [UIFont systemFontOfSize:kGetScaleWidth(font) weight:UIFontWeightHeavy]
///内置版本号
#define PI_App_Version @"1.0.0"
///渠道
#define PI_App_Source @"appstore"
#define PI_Test_Flight @"TestFlight"
#define ISTestFlight 0
///正式环境
#define API_HOST_URL @"https://api.epartylive.com"
///测试环境
#define API_HOST_TEST_URL @"http://beta.api.epartylive.com" // http://beta.api.epartylive.com http://beta.api.pekolive.com | http://beta.api.molistar.xyz
#define API_Image_URL @"https://image.hfighting.com"
#define YMLocalizedString(key) \
[NSBundle ymLocalizedStringForKey:(key)]
#define isMSRTL() [YMLanguageConfig isRTLanguage:[NSBundle getLanguageText]]
///是否是中文
#define isMSZH() [[NSBundle getLanguageText] hasPrefix:@"zh"]
///是否是英文
#define isMSEN() [[NSBundle getLanguageText] hasPrefix:@"en"]
///是否土耳其语
#define isMSTR() [[NSBundle getLanguageText] hasPrefix:@"tr"]
///是否葡萄牙语
#define isMSPT() [[NSBundle getLanguageText] hasPrefix:@"pt"]
///是否西班牙语
#define isMSES() [[NSBundle getLanguageText] hasPrefix:@"es"]
///是否俄语
#define isMSRU() [[NSBundle getLanguageText] hasPrefix:@"ru"]
///是否乌兹别克语
#define isMSUZ() [[NSBundle getLanguageText] hasPrefix:@"uz"]
#endif /* YUMIMacroUitls_h */