Skip to content

Instantly share code, notes, and snippets.

@steffex
Last active January 5, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steffex/dc3ee8602f97ca1cd655 to your computer and use it in GitHub Desktop.
Save steffex/dc3ee8602f97ca1cd655 to your computer and use it in GitHub Desktop.
Set different initial viewcontroller in storyboard

I'm using this peace of code to check if the device that is running my app can take pictures. If the device doesn't have a camera, it will start the app with a different viewcontroller that displays a message.

There are 2 things you need to check in order to get it working:

  • the storyboard name in the call storyboardWithName:@"Main" bundle:nil
  • the viewcontroller id for the call instantiateViewControllerWithIdentifier:@"noCamera"

If you have any questions, just drop me an email.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if (![self deviceHasCamera]) {
// the storyboard name is the part before .storyboard
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
// set the rootviewController to the viewcontroller in the storyboard above.
self.window.rootViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"noCamera"];
}
return YES;
}
- (BOOL)deviceHasCamera
{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if deviceHasCamera() == false {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryboard.instantiateViewControllerWithIdentifier("noCamera")
}
return true
}
func deviceHasCamera() -> Bool {
return UIImagePickerController.isSourceTypeAvailable(.Camera)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment