【问题标题】:How to get iOS User-Agent Strings如何获取 iOS 用户代理字符串
【发布时间】:2014-07-17 07:10:20
【问题描述】:

我正在尝试获取设备的用户代理字符串 - 此处列出的字符串:http://www.enterpriseios.com/wiki/Complete_List_of_iOS_User_Agent_Strings

这是我正在使用的代码:

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectZero];
    NSString *uaString = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

我得到的结果是:

Mozilla/5.0(iPhone;CPU iPhone OS 7_1_1 像 Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D201

我想要得到的是:

苹果-iPhone4C1

我该怎么做?

【问题讨论】:

    标签: ios iphone user-agent


    【解决方案1】:

    您需要编写一个可以生成此字符串的函数,如下所示:

    #include sys/sysctl.h
    #include sys/utsname.h
    
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model =  (char*) malloc(sizeof(size_t));
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    NSLog(@"%@", deviceModel);
    
    // or
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSLog(@"%@", [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]);
    

    【讨论】:

    • 这给了我“iPhone3,1”
    • 是的 - 有本地方法吗?还是我需要编写一个将其转换为 Apple-iPhone3C2 的方法?
    • 到目前为止我还没有找到任何原生方式。到目前为止,您需要使用 uname(&systemInfo); 编写一个函数;
    【解决方案2】:

    为此,您需要访问 UIDevice 类的方法。 UIDevice 类的 model 属性可以给你这样的信息。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2011-12-11
    • 2012-08-31
    • 2018-12-18
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多