是否可以检测到您的iOS应用程序在运行时在iPad mini上运行? [英] Is it possible to detect that your iOS app is running on an iPad mini at runtime?

查看:144
本文介绍了是否可以检测到您的iOS应用程序在运行时在iPad mini上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时检测不同的硬件对于分析(除了其他,甚至更有疑问的目的)非常有用。

Detecting different hardware at runtime is useful for analytics (among other, even more questionable purposes).

许多iOS应用程式创作者可能有兴趣知道有多少使用者在iPad mini上体验到他们的应用程式(而不是只知道有多少使用者在iPad上体验到他们的应用程式)

Many iOS app creators may be interested to know how many users are experiencing their app on an iPad mini (rather than just knowing how many users are experiencing their app on an iPad with 1024x768 screen resolution - which would also be interesting).

中有任何公共API。Cocoa touch / UIKit / ObjC / C 可以用来检测您的iOS应用程序是在运行时在iPad mini上运行吗?理想情况下,这种方法应该区分iPad 2& iPad mini(具有相同像素数,但像素密度不同)。

Is there any public API in Cocoa touch/UIKit/ObjC/C which could be used to detect that your iOS app is running on an iPad mini at runtime? Ideally, this method should distinguish between iPad 2 & iPad mini (which have the same number of pixels, but a different pixel density).

帖子脚本:人们会考虑在运行时检测iPad mini的一个坏主意。但是,我认为这是一个有效的问题,有确定的是或否答案。

Post Script: I realize many people will consider detecting iPad mini at runtime a bad idea. However, I think this is a valid question with a definite Yes or No answer. An answer which I think it is useful for the community to know.

推荐答案

如果没有调用,Boxel的答案将会很好未定义的行为,如果它没有多余的部分。一个, + [NSString stringWithCString:encoding:] 需要一个C字符串 - 也就是一个NUL终止的字符指针(否则它很可能转储核心)。此外,您不需要转换为 NSString - 因为 sysctlbyname()为您提供一个普通的旧C字符串(当然没有NUL终止符),你可以直接使用 strcmp()来保存几十个CPU周期:

Boxel's answer would be good if it didn't invoke undefined behavior and if it didn't have superfluous parts. One, + [NSString stringWithCString:encoding:] requires a C string - that is, a NUL-terminated char pointer (else it will most likely dump core). Also, you don't need the conversion to NSString - since sysctlbyname() provides you with a plain old C string (without the NUL terminator, of course), you can directly use strcmp() to save a few dozen CPU cycles:

#include <sys/sysctl.h>

size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size + 1);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
machine[size] = 0;

if (strcmp(machine, "iPad2,5") == 0) {
    /* iPad mini */
}

free(machine);

编辑:现在答案也是固定的。

now that answer is fixed as well.

这篇关于是否可以检测到您的iOS应用程序在运行时在iPad mini上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆