idoodler
idoodler

Reputation: 3545

How to exactly detect iDevice model?

I want to detect what iDevice the user has and then put the device name in a UILabel. With the following code the App detects only the iPhone/iPad/iPod I like to have iPhone 4/iPod 3G/iPad 1G... or the exact names (iPhone 3.1/iPod 2.0/ iPad 2.4)...

here is my code:

iDevice.text = [UIDevice currentDevice]. localizedModel;

I tried this to

iDevice.text = [UIDevice currentDevice]. model;

but alleyways it sayes iPhone and i like iPhone 3.1

Upvotes: 4

Views: 2092

Answers (1)

propstm
propstm

Reputation: 3629

Ok so it sounds like the method you will want to use is to use the category created by Erica Sadun located at https://github.com/erica/uidevice-extension/

Before I get into how to use it I'll pass on a bit of info about categories. Apple provides documentation on categories here http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.

Download the project from github, and add these two files to your project:

UIDevice-Hardware.h
UIDevice-Hardware.m

The methods you'll be using would be one of these:

- (NSString *) platform;
- (NSString *) hwmodel;
- (NSUInteger) platformType;
- (NSString *) platformString;

So you'll want to import UIDevice-Hardware.h into the file where you want to use the method. You would use the method to return an NSString value and assign the value to a label, so you'd do something similar to

mylabel.text = [[UIDevice currentDevice] platformString]

Here's another link that has a good introduction to categories: http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

EDIT: SAMPLE SCREENSHOT USING THE DEVICE SIMULATOR: enter image description here Note: also have #import "UIDevice-Hardware.h" above my @interface line.

Upvotes: 2

Related Questions