How do I detect which iOS device my user is using?

心不动则不痛 提交于 2019-12-19 06:45:10

问题


I'm searching for a way to detect the device my app is running on. I am not interested in software version. I searched many questions but none of them (surprisingly) satisfy my needs for following reasons:

Solution 1:

NSString *deviceType = [UIDevice currentDevice].model;

This does not work because it gives me just "iPad". I want to know whether it is iPad, iPad 2, new iPad, iPhone 3GS, iPhone4 etc.

Solution 2: Not testing for device type, checking for individual capabilities

This does not apply because I want this data to collect user statistics, not to perform any device specific operation.

Solution 3: Using UIDeviceHardware found here

This code looks pretty outdated and seems to access private data on the device. There is even debate whether an app using this will get approved or not. More importantly, I have no idea how it works :) An alternative is also found here

Are any of the last two I mentioned safe to use? Are they future compliant? Do they comply with Apple approval rules?

Or is there any other method to get around this?

Thanks in advance.


回答1:


Apple uses such an API in it's sample code. To Quote the LargeImageDownsizing example project, in the file LargeImageDownsizingViewController.m beginning with line 83:

Choosing appropriate resulting image size and tile size can be done, but is left as an exercise to the developer. Note that the device type/version string (e.g. "iPhone2,1" can be determined at runtime through use of the sysctlbyname function:

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:NSASCIIStringEncoding];
free(machine);

_platform in my case would be iPod4,1 for an iPod touch 4th gen.

This code is what is at the root of both of the Github examples you posted.




回答2:


Technically, [UIDeviceHardware platform] should work fine and you should send it to your server as it is. You should also send the iOS version along with the platform id to save you from trouble if the platform id would change in the future OS.

Because the function sysctlbyname being used here is well-documented, so I don't think Apple can use the rule number 2.5 against you. However, there still the rule number 17.1:

17.1 Apps cannot transmit data about a user without obtaining the user's prior permission and providing the user with access to information about how and where the data will be used

It is arguable if the device platform is about a user or not. My advice is that you should submit the app assuming you are not violating 17.1 and never ever mention about that. It is highly likely that the reviewer will not know what you are doing behind the scene.




回答3:


The UIDeviceHardware you mentioned is good enough but a bit outdated. You can try out this Made it more apple-style, so may by they will like it more.



来源:https://stackoverflow.com/questions/12224508/how-do-i-detect-which-ios-device-my-user-is-using

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