Wednesday, June 12, 2013

Different ways to differentiate between devices in iOS

There are many ways to figure out how to tell which device you are using, some are specific, some are general. They all serve their purpose. Here are some examples;

If you have a universal app and want to use a different nib for the ipad or iphone;
    NSString *nibName = [NSStringFromClass([self class]) stringByAppendingString:([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)? @"-iPhone" : @"-iPad"];
    self = [super initWithNibName:nibName bundle:nil];
* note be sure and name you nibs correctly.

If you have some specific code the you want to perform on either the iphone or ipad;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    //iphone code
}else{
    //ipad code
}

If you want a quick definition change depending on if it is an ipad or iphone;
    int total = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)? iPhone_Total :iPad_Total;

No comments:

Post a Comment