How do you detect iPhone v. iPhone 3G using Obj C

前端 未结 4 1769
暖寄归人
暖寄归人 2021-01-15 05:31

There is a model property of the device, but it doesn\'t distinguish between iPhone and iPhone 3G. Is there a better way?

4条回答
  •  遥遥无期
    2021-01-15 05:55

    The specifically interesting parts of the project Stephan posted are these:

    The string values you're likely to see:

    /*
     Platforms
     iPhone1,1 -> iPhone 1G
     iPhone1,2 -> iPhone 3G 
     iPod1,1   -> iPod touch 1G 
     iPod2,1   -> iPod touch 2G 
    */
    

    How to get one of those values:

    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    
    NSString *platform = [NSString stringWithCString:machine encoding: NSUTF8StringEncoding];
    
    free(machine);
    

    You can then check the beginning of platform for @"iPhone" or @"iPod" and tell if you have a device with GPS or not. I wouldn't recommend matching the whole string for what you want, because the next time a device comes out (such as this summer, most likely) you won't be able to match it without changing code.

    Also, if you haven't seen them, Erica Sadun (the project author) has some excellent articles on iPhone dev at ArsTechnica.com and at least one book about it.

提交回复
热议问题