问题描述:
用wda获取ios的ip信息的过程中,发现ios15及以上的iPhone获取到的ip为169.254.xxx.xxx根本不是真实的ip地址,自己一番控制变量法+大佬一番指点后发现是ios15以上默认网卡发生了变化,导致获取不到真实ip,这里需要找大佬改一下oc文件
解决办法:
修改文件appium-webdriveragent/WebDriverAgentLib/Categories/XCUIDevice+FBHelpers.m
- (NSString *)fb_wifiIPAddress
{
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = getifaddrs(&interfaces);
if (success != 0) {
freeifaddrs(interfaces);
return nil;
}
NSString *address = nil;
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family != AF_INET) {
temp_addr = temp_addr->ifa_next;
continue;
}
NSString *interfaceName = [NSString stringWithUTF8String:temp_addr->ifa_name];
if(![interfaceName containsString:@"en"]) {
temp_addr = temp_addr->ifa_next;
continue;
}
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
break;
}
freeifaddrs(interfaces);
return @"address";
}
修改为
- (NSString *)fb_wifiIPAddress {
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
NSDictionary *dic = [addresses count] ? addresses : nil;
return dic[@"en0/ipv4"];
}
添加代码
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <net/if.h>
#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"
获取IP的python代码
import wda
c = wda.USBClient()
print(c.status())