Question

Is there any way to detect what device the user is using, then using that information, make the application use a specific nib file?

Right now, the code that I have is

- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions{
  if(isiPhone5){
    self.RootViewController = @"RootViewController_iPhone5";
  }
  else{
    self.RootViewController = @"RootViewController_iPhone4";
  }
}

My other code for finding the device works and tells me what device the user is using, yet doesn't actually change the .xib file from RootViewController_iPhone4.xib to RootViewController_iPhone5.xib. Is there any way to do this?

I don't want to use auto layout, because I would like to have other custom things happen depending upon the device, such as different button names in the view controller if the device being used is an iPhone 4 verses an iPhone 5

Was it helpful?

Solution

Try this way..

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if (screenBounds.size.height ==568)
    {
          self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone5" bundle:nil];
    }
    else
    {
          self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone4" bundle:nil];
    }

Updated Answer

check this... Black Screen for screen detection

OTHER TIPS

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     if (IS_IPHONE_5)
    {
        self = [super initWithNibName:@"YourViewController~iphone5" bundle:nil];
    }
    else
    {
        self = [super initWithNibName:@"YourViewController~iphone" bundle:nil];
    }
    return self;
}

Try something like this

You can achieve this by the following way

create a macro in App delegate so that it will be used through out the Project. It will be based on the screen height for the device.

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

Now you can test the condition for iphone 4 and iphone 5

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){


    if(IS_IPHONE_5){


        self.window.RootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone5" bundle:nil];

    }
    else
    {
        self.window.RootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    }
    }
  }
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        // iPhone 4 Classic

        //first always target 4 for better performance & practice.
    }
    if(result.height == 568)
    {
        // iPhone 5
    }
}else {
//it's ipad
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top