4

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

Nekto
  • 17,837
  • 1
  • 55
  • 65
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98

1 Answers1

2

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.

propstm
  • 3,461
  • 5
  • 28
  • 41
  • Now I added the UIDevice-Hardware.h and the UIDevice-Hardware.m to my project, i #import "UIDevice-Hardware.h" the file in my .m file, then I added the mylabel.text = [[UIDevice currentDevice] platformString] to - (void)viewDidLoad and my app crashes:( What is my mistake? must i do anything with the - (NSString *) platform; - (NSString *) hwmodel; - (NSUInteger) platformType; - (NSString *) platformString;? – David Gölzhäuser Oct 23 '12 at 12:05
  • for your instance you would want `iDevice.text = [[UIDevice currentDevice] platformString];` That had just been a generic label namei wrote. It was probably crashing because my variable name hadn't been defined. – propstm Oct 23 '12 at 12:26
  • I have iDevice.text = [[UIDevice currentDevice] platformString]; – David Gölzhäuser Oct 23 '12 at 12:34
  • It is too difficult to describe so i made a picture:[link](http://www.mediafire.com/view/?dg7u30c89p1ck3a) – David Gölzhäuser Oct 23 '12 at 12:52
  • Could you create a postbin or gist of the code you're using? I'm going to try to recreate this real quick. – propstm Oct 23 '12 at 12:56
  • I've added a screenshot using the category to my original response Let me know if you can log your device instead of doing the assignment to iDevice – propstm Oct 23 '12 at 13:09
  • When i add NSLog(@"Current Device: %@", [[UIDevice currentDevice]platformString]); in ViewDidLoad, my app crashes too:( – David Gölzhäuser Oct 23 '12 at 13:14
  • Could you please make a gist of the UIDevice-Hardware.h file. – propstm Oct 23 '12 at 13:29
  • I found this [link](https://github.com/eagle-dan1349/EDDeviceHardware) on Web but I get 2 errors when I putt this in my project. You might could glimps on it if you like, I couldn't find the mistakes. When i try to install the project on my iDevice I works but i cant implement in in my project. – David Gölzhäuser Oct 23 '12 at 14:53
  • I looked at your error again. Do you still see this same error? It basically says `[UIDevice platformString]: unrecognized selector sent to instance.` So i'm wondering somewhere instead of `[[UIDevice currentDevice] platformString]` you have `[UIDevice platformString]` so that's the error. It's tough to debug without seeing your whole project. This is a great resource for understanding how to debug xcode errors: http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – propstm Oct 23 '12 at 14:59
  • I checked everything and there is no [UIDevice platformString] but i have this in my project : float currentVersion = 6.0; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion) could this cose the problem? – David Gölzhäuser Oct 23 '12 at 15:22
  • It may be a bit slow I'd say the best way to check would be to comment out entire blocks of code, and slowly uncomment your code until you can isolate what's causing the issue. – propstm Oct 23 '12 at 15:24