如何在iPhone SDK中以编程方式获取设备名称?

7
我可以获取设备的MAC地址和IP地址,但无法获取设备名称。你有什么想法可以获取更多信息吗?例如设备的“网络工具”?

嗨Dhaval,你找到解决方案了吗?如果有的话,请分享。 - iBhavik
@i-bhavik,我没有得到设备名称,只获取了设备信息。 - Dhaval
有人在这方面有进展吗?我也希望我的应用程序能像iNet一样找到设备名称。我可以ping,可以读取ARP表,但无法找到机器名称。任何帮助将不胜感激! - Jelle
3个回答

11
NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);
NSLog(@"name: %@", [[UIDevice currentDevice] name]);
NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]);
NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]);
NSLog(@"model: %@", [[UIDevice currentDevice] model]);
NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);

它可以提供任何当前设备的信息,但我需要获取网络中不同连接设备的名称/ MAC地址/ IP地址。我可以获取MAC和IP,但无法获取设备名称。 - Dhaval
把这个作为新答案!它能够正确地运行,而且非常简单。我所做的唯一修改就是将数据解析成 NSString 变量。 - user19003768
第一行代码不再起作用(请参见https://dev59.com/wmDVa4cB1Zd3GeqPgLpR) - Michael Dorner

3

在iOS 4.1及以上版本中,您可以这样做:如果您正在寻找SSID名称...

导入

- (id)fetchSSIDInfo
{
    NSArray *ifs = (id)CNCopySupportedInterfaces();
    NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
        NSLog(@"%s: %@ => %@", __func__, ifnam, info);
        if (info && [info count]) {
            break;
        }
        [info release];
    }
    [ifs release];
    return [info autorelease];
}

谢谢,但我想获取通过我的设备ping的不同连接设备,而不是网络信息。就像我获取到我的设备名称“iPhone模拟器”这种类型的其他连接到路由器的设备信息。如果您有任何想法,请告诉我,好吗? - Dhaval
@Dhaval:尝试在设备上使用此代码,而不是在iPhone模拟器上。当您在设备上使用时,您将获得路由器SSID、Mac地址等信息。 - Deepak

1
#import <ifaddrs.h>
#import <arpa/inet.h>

- (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;

} 

我已经获取了IP和MAC地址,但是没有获取到设备名称。如何获取设备名称? - Dhaval

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接