How can I load different storyboards depending on the device? - ios

I am trying to:
detect device (iPhone 3.5", iPhone 4", iPad, ect)
load a different storyboard depending on what device and what size the application is running on.
I have watched some tutorials but I am still not getting it, can someone please type/show what code needs to go in the app delegate to achieve these goals.
Thanks!

Try something like this in your app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIStoryboard *storyboard = nil;
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568.0f)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone5s" bundle:nil];
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone4" bundle:nil];
}
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"iPad" bundle:nil];
}
[window setRootViewController:[storyboard instantiateInitialViewController]];
[window makeKeyAndVisible];
}

Related

Airplay IOS multiple view controller

I am trying to implement an application with a number of viewcontrollers presented through a hierarchy with different viewing on the iPad and Airplay device. I am using objective C and Storyboard.
I have updated the app delegate and I can see my first controller presented on both displays but then I get stuck when I push through the next view controllers. The iPad shows the next controllers but the Airplay gets stuck on the first one.
Does anyone have experience in building such an app and would be willing to share some lights on how to do this ?
This is my code snippet in my appdelegate:
//Managing connection and disconnection of screens.
-(void) screenConnectionStatusChanged {
if ([[UIScreen screens] count] == 1) {
secondaryWindow.hidden=YES;
secondaryWindow.rootViewController = nil;
} else {
UIScreen *newScreen = [[UIScreen screens] lastObject];
self.secondaryWindow = [[UIWindow alloc] initWithFrame:[newScreen bounds]];
self.secondaryWindow.screen = newScreen;
self.secondaryWindow.hidden = NO;
self.secondaryWindow.layer.contentsGravity = kCAGravityResizeAspect;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginViewController *vc = [storyboard instantiateInitialViewController];
[secondaryWindow setRootViewController:vc];
[secondaryWindow setHidden:NO];
[secondaryWindow makeKeyAndVisible];
}
}
Thanks

loading different storyboard causing error

I am trying to load different storyboard but am getting this error Application windows are expected to have a root view controller at the end of application launch. Can anyone help me out
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;
if (height == 480) {
storyboard = [UIStoryboard storyboardWithName:#"iPhone_4" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
}
if(height == 568)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone_5" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
if(height == 667)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone_6" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
if(height == 736)
{
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}
You are getting this error because compiler see the case when your function can return uninitialised storyboard variable. It might happened if non of the if statements is true. So it's better to rewrite this function so it always return initialised storyboard variable. My suggestion is to change the way how to determine device model. Take a look at this post for hint.
i think one or more of your storyboards doesn't have an entry point make sure to verify them all . you can check the official doc here entry point for storyboard

"Application windows are expected..." error when loading storyboard for Ipad

I'm loading different storyboards for Iphone and Ipad and the Iphone's storyboards load good but when I start the Ipad Simulator I got the "Application windows are expected..." error.
this is the code in AppDelegate:
#implementation AppDelegate
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;
if (height == 480) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Iphone3" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
}
if (height >= 1024 && height <= 2048 ) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Ipad" bundle:nil];
NSLog(#"Device has a 4inch Display.");
}
else {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Iphone4Up" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [self grabStoryboard];
// show the storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
In the console I see the NSLog message but the storyboard doesn't load and the "Application windows..." error appears.
I'm using some Scroll Views and some elements but i thinkg those are not the problem because there is an option for them "Adjust Scroll View Insets".
I uploaded some screen shots to show more about my project. Thanks for your time.
I figured it out. When you copy all stuff from the Iphone storyboard to the Ipad one the Initial View Controller checkbox is unchecked. Just check it and all will be good (:

iOS >> Setting UIWindow in AppDelegate When Using Multiple Storyboards

I wish to create an app with multiple storyboards to support iPhone5, iPhone4 (and below) and iPad screens.
I did the following:
I created 3 storyboards, one per each setting.
I cleared the "main storyboard" field in the project interface.
I cleared the "Main storyboard file base name" field in the app info.plist file.
I entered the following code to the AppDelegate "didFinishLaunchingWithOptions" method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard* appStoryboard = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if (IS_IPHONE_5) //a macro capturing the screen size
{
appStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone5" bundle:nil];
}
else
{
appStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone4" bundle:nil];
}
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
appStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
}
UIViewController* viewController = [appStoryboard instantiateInitialViewController];
[self.window setRootViewController:viewController];
[self.window makeKeyAndVisible];
return YES;
}
The application is running and not crashing, but I get a black screen.
What am I missing / doing wrong?
You didn't initialize the window.
Just add
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

get active storyboard

I'm using the code bellow to check if the user is logged in or logged out, and it's working fine. But because I'm getting the storyboard by name, I'm always sending the user to the iPhone "in" or "out" view controller. It's gonna work if I get the active storyboard. How can I fix the code?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL user = ...;
NSString *segue = user ? #"in" : #"out";
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"iPhone" bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:segue];
[self.window setRootViewController:viewController];
return YES;
}
If all you're looking to do is to get the viewController from a different storyboard depending on whether your app is running on an iPad vs. an iPhone, you could do this with an if statement:
NSString *storyboardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
storyboardName = #"iPad";
} else {
storyboardName = #"iPhone";
}
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
If you really need to get the "active" storyboard no matter the situation, this prior answer may help you:
UIStoryboard: What's the Correct Way to Get the Active Storyboard?

Resources