Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

objective c - Is it possible to detect that your iOS app is running on an iPad mini at runtime?

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

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).

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).


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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Edit: now that answer is fixed as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...