2026-04-28 16:35:51 +08:00
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
#import <SystemConfiguration/CaptiveNetwork.h>
|
2026-07-06 16:49:18 +08:00
|
|
|
|
#import <NetworkExtension/NetworkExtension.h>
|
|
|
|
|
|
#import <CoreLocation/CoreLocation.h>
|
|
|
|
|
|
|
|
|
|
|
|
static CLLocationManager *locationManager = nil;
|
2026-04-28 16:35:51 +08:00
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2026-07-06 16:49:18 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-28 16:35:51 +08:00
|
|
|
|
const char* GetWiFiSSID() {
|
2026-07-06 16:49:18 +08:00
|
|
|
|
// 确保位置已启动至少一次(NEHotspotNetwork 的隐式要求)
|
|
|
|
|
|
if (locationManager != nil && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
|
|
|
|
|
|
[locationManager startUpdatingLocation];
|
|
|
|
|
|
}
|
2026-04-28 16:35:51 +08:00
|
|
|
|
|
2026-07-06 16:49:18 +08:00
|
|
|
|
__block NSString *resultSSID = nil;
|
|
|
|
|
|
__block BOOL finished = NO;
|
2026-04-28 16:35:51 +08:00
|
|
|
|
|
2026-07-06 16:49:18 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}];
|
2026-04-28 16:35:51 +08:00
|
|
|
|
|
2026-07-06 16:49:18 +08:00
|
|
|
|
// 用 CFRunLoop 等待,不阻塞主线程事件处理
|
|
|
|
|
|
NSTimeInterval deadline = [[NSDate date] timeIntervalSinceReferenceDate] + 3.0;
|
|
|
|
|
|
while (!finished && [[NSDate date] timeIntervalSinceReferenceDate] < deadline) {
|
|
|
|
|
|
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
|
2026-04-28 16:35:51 +08:00
|
|
|
|
}
|
2026-07-06 16:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
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];
|
2026-04-28 16:35:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-06 16:49:18 +08:00
|
|
|
|
if (resultSSID == nil) {
|
|
|
|
|
|
NSLog(@"[WiFiHelper] No WiFi SSID found.");
|
2026-04-28 16:35:51 +08:00
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-06 16:49:18 +08:00
|
|
|
|
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);
|
2026-04-28 16:35:51 +08:00
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|