【问题标题】:How to get the device name programmatically in iPhone sdk?如何在 iPhone sdk 中以编程方式获取设备名称?
【发布时间】:2012-06-25 10:19:21
【问题描述】:

我得到了设备的 Mac 和 IP 地址。但是,不要获取设备名称。

如果可能的话,知道如何获取更多信息,例如设备的“网络实用程序”吗?

【问题讨论】:

  • 嗨 Dhaval,您找到解决方案了吗?那么请分享
  • @i-bhavik 我没有得到设备名称任何设备信息。
  • 有人在这方面有什么进展吗?我还希望我的应用程序能够找到设备名称,就像 iNet 一样。我可以 ping,我可以读取 ARP 表,但我找不到机器名称。任何帮助将不胜感激!

标签: iphone ip-address device mac-address


【解决方案1】:

在 iOS 4.1+ 上,您可以这样做:如果您正在寻找 SSID Name..

进口

- (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:尝试在设备上使用此代码,而不是在 iPhone 模拟器中。当您在设备上使用它时,您将获得路由器 SSID、Mac 地址等。
【解决方案2】:
#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,但是没有得到设备名。如何得到呢?
【解决方案3】:
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,但没有得到设备名称。
  • 让这个成为新的答案!它工作正常,而且简单。我所做的唯一修改是将数据解析为NSString 变量。
  • 第一行不再起作用(见stackoverflow.com/questions/9396187/…
猜你喜欢
  • 2014-01-17
  • 1970-01-01
  • 2011-01-01
  • 2018-10-26
  • 2015-03-13
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多