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

走远了吗. 提交于 2019-11-27 07:53:53

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.

Use sysctlbyname to get the platform string, then compare it with the IPSW prefix strings listed here. It looks like the only currently known iPad Mini is "iPad2,5"

Example:

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) {
    // do something special on the iPad Mini    
}

free(machine);

There is no reliable, future-proof way to detect the iPad mini's pixel density. Other answers suggest looking at the hw.machine string. But we don't know (as of the time I write this) what the strings will be for the iPad mini cellular models (though we can make an educated guess: iPad2,6 will probably have GSM and iPad2,7 will probably have CDMA).

It's fine to look at the hw.machine string for analytics. But it's dangerous to let it affect your app's user interface, because even the iPad2,5 string for the current iPad mini is subject to change.

When the iPad 2 came out, the wifi model's string was iPad2,1. Later (when they released the iPad 3), they changed the iPad 2 hardware and changed the hw.machine string to iPad2,4, but they still called it the iPad 2. The same thing could happen with the iPad mini - or even with the iPad 2 again! For example, Apple could release yet another version of the iPad 2 hardware and give it the machine string iPad2,8.

import CoreTelephony.framework. Try this on device

#import <sys/utsname.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

NSString*
machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                          encoding:NSUTF8StringEncoding];
}

This solves my purpose up to now. But as I don't have any iPad mini I don't know what it gonna print for iPad mini. Could you let me know?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!