1. 添加位置权限请求逻辑适配iOS 14+ NEHotspotNetwork要求 2. 新增NetworkExtension框架依赖并更新Xcode项目配置 3. 修复构建脚本中的日志标识与应用名大小写问题 4. 更新启动图标配置与iOS项目设置 5. 优化WiFi SSID获取的兼容逻辑与日志输出
102 lines
4.2 KiB
Plaintext
102 lines
4.2 KiB
Plaintext
#import <Foundation/Foundation.h>
|
||
#import <SystemConfiguration/CaptiveNetwork.h>
|
||
#import <NetworkExtension/NetworkExtension.h>
|
||
#import <CoreLocation/CoreLocation.h>
|
||
|
||
static CLLocationManager *locationManager = nil;
|
||
|
||
extern "C" {
|
||
void RequestLocationPermission() {
|
||
if (locationManager != nil) return;
|
||
|
||
locationManager = [[CLLocationManager alloc] init];
|
||
|
||
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
|
||
if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
|
||
status == kCLAuthorizationStatusAuthorizedAlways) {
|
||
NSLog(@"[WiFiHelper] Location permission already granted (status: %d)", (int)status);
|
||
// 位置权限已获得但还需要启动定位才能让WiFi API工作
|
||
[locationManager startUpdatingLocation];
|
||
// 短暂启动后停止(不需要持续定位,只是激活权限链)
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
||
[locationManager stopUpdatingLocation];
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (status == kCLAuthorizationStatusNotDetermined) {
|
||
NSLog(@"[WiFiHelper] Requesting WhenInUse location permission");
|
||
[locationManager requestWhenInUseAuthorization];
|
||
} else {
|
||
NSLog(@"[WiFiHelper] Location permission denied/restricted (status: %d)", (int)status);
|
||
}
|
||
}
|
||
|
||
const char* GetWiFiSSID() {
|
||
// 确保位置已启动至少一次(NEHotspotNetwork 的隐式要求)
|
||
if (locationManager != nil && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
|
||
[locationManager startUpdatingLocation];
|
||
}
|
||
|
||
__block NSString *resultSSID = nil;
|
||
__block BOOL finished = NO;
|
||
|
||
if (@available(iOS 14.0, *)) {
|
||
NSLog(@"[WiFiHelper] Using NEHotspotNetwork.fetchCurrent");
|
||
|
||
[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable network) {
|
||
if (network && network.SSID.length > 0) {
|
||
resultSSID = network.SSID;
|
||
}
|
||
finished = YES;
|
||
}];
|
||
|
||
// 用 CFRunLoop 等待,不阻塞主线程事件处理
|
||
NSTimeInterval deadline = [[NSDate date] timeIntervalSinceReferenceDate] + 3.0;
|
||
while (!finished && [[NSDate date] timeIntervalSinceReferenceDate] < deadline) {
|
||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
|
||
}
|
||
|
||
if (resultSSID != nil) {
|
||
NSLog(@"[WiFiHelper] NEHotspotNetwork SSID: %@", resultSSID);
|
||
} else {
|
||
NSLog(@"[WiFiHelper] NEHotspotNetwork returned nil (finished=%d)", finished);
|
||
}
|
||
}
|
||
|
||
// Fallback
|
||
if (resultSSID == nil) {
|
||
NSLog(@"[WiFiHelper] Fallback to CNCopyCurrentNetworkInfo");
|
||
NSArray *interfaces = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
|
||
if (interfaces != nil) {
|
||
for (NSString *interfaceName in interfaces) {
|
||
NSDictionary *info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName);
|
||
if (info && info[(NSString *)kCNNetworkInfoKeySSID]) {
|
||
resultSSID = info[(NSString *)kCNNetworkInfoKeySSID];
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (locationManager != nil) {
|
||
[locationManager stopUpdatingLocation];
|
||
}
|
||
|
||
if (resultSSID == nil) {
|
||
NSLog(@"[WiFiHelper] No WiFi SSID found.");
|
||
return NULL;
|
||
}
|
||
|
||
NSLog(@"[WiFiHelper] SSID: %@", resultSSID);
|
||
|
||
const char *utf8String = [resultSSID UTF8String];
|
||
size_t len = strlen(utf8String) + 1;
|
||
char *result = (char *)malloc(len);
|
||
if (result == NULL) return NULL;
|
||
memcpy(result, utf8String, len);
|
||
|
||
return result;
|
||
}
|
||
}
|