gpt4 book ai didi

iphone - 如何以编程方式获取iphone的IP地址

转载 作者:IT老高 更新时间:2023-10-28 11:48:43 26 4
gpt4 key购买 nike

我通过使用 Mongoose 在 iphone 中启动并运行了一个网络服务器.但问题是如何获取我的 iphone/ipad 的 IP 地址,让用户知道他们可以在哪里访问服务器。我发现 [NSHostaddress] 可以完成这项工作,但我正在为应用商店开发,这是未记录的方法。

最佳答案

#include <ifaddrs.h>
#include <arpa/inet.h>

// Get the INTERNAL ip address

- (NSString *)getIPAddress {

NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

}

}

temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;

}

https://web.archive.org/web/20160527165909/http://www.makebetterthings.com/iphone/how-to-find-ip-address-of-iphone/

关于iphone - 如何以编程方式获取iphone的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6807788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com