How do I detect an iPad's interfaceRotation at the start? - objective-c

I'm having problems getting my iPad app to detect its interfaceOrientation in the first UIViewController I initialize (in code). In fact, if I trace for application.statusBarOrientation, that too returns 1 (UIInterfaceOrientationPortrait) even if I launched in landscape.
If I trace self.interfaceOrientation in my first UIViewController, it remains 1 until it gets to viewWillDisappear... Which is unfortunately too late!
Here's some code (even though there's not much to see):
In my appDelegate I have this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// show loading screen first
[window addSubview:loadingScreenViewController.view];
[window makeKeyAndVisible];
NSLog(#"applicationDidBecomeActive:statusBarOrientation = %d", application.statusBarOrientation);
return YES;
}
which traces 1 (portrait), even though I clearly see the status bar is landscape... and in the first view controller I have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"self.interfaceOrientation = %d", self.interfaceOrientation);
}
which also traces 1, even in landscape mode.
Any ideas? Stumped here!
Thanks :)
:-Joe

Here is the answer... Somewhat: (from Apple Dev Forums): ....
"The app is always loaded as if the device is portrait, and then if the device is really landscape the app is told that the device has rotated. This is done so that nibs and code only need to create their UI in one orientation. Otherwise it might be necessary to have two UI layouts for each nib." .... it's not the answer I'd have liked, but that's how iOS works unfortunately!

What does the app delegate report in applicationDidFinishLaunching? Because if it reports the correct value, you can always access the delegate to check the orientation.

Related

iOS 6 Game Center Crash on Authentication in landscape mode cocos2d

I have developed a game in cocos2d and all game screens are in Landscape mode. I am trying to implement game Center but getting crash on authentication. I did not find answer of similar type of issues. please suggest right approach...
Crash issue:-'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'
I tried below solution but it also disturb game orientations, game starts work in portrait mode also, that i don't want:-
(NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow: (UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Make sure you selected landscape in Xcode summary page.
Also add these code in your viewcontroller
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Update this function in AppDelegate:
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskLandscape;
}
the solution for that is short, i spent a lot of time before finding it:
in the AppDelegate in the method didFinishLaunchingWithOptions put this line:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
obviously before call the login game center method, i put that before create the UIWindows

setStatusBarOrientation:animated: not working in iOS 6

I've used this code to force an orientation change back to portrait when the user is finished watching the video (it allows viewing in landscape mode), before popping the video view controller off the navigation controller:
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
This worked perfectly until iOS 5.1.1. I've even tried to use the new present/dismiss methods after reading in another post that those should be used now:
[self presentViewController:mVC animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];
The problem is it doesn't work at all. After I rotated the video viewer to landscape and then pop it, my settings view (table view controller) comes back, but also in landscape mode.
I've even tried the tip from Here
"The setStatusBarOrientation:animated: method is not deprecated outright. However it now works only if the supportedInterfaceOrientations method of the topmost full screen view controller returns 0. This puts the responsibility of ensuring that the status bar orientation is consistent into the hands of the caller."
So I've experimented with setting a flag to force supportedInterfaceOrientations to return 0 (before calling the first code block above) but it doesn't work either.
Does anybody have a solution for this?
Thanks for your time and effort.
setStatusBarOrientation method has changed behaviour a bit. According to Apple documentation:
The setStatusBarOrientation:animated: method is not deprecated
outright. It now works only if the supportedInterfaceOrientations
method of the top-most full-screen view controller returns 0
Your root view controller should answer false to the method shouldAutorotate in order that your app responds to setStatusBarOrientation:animated
From Apple Documentation: "if your application has rotatable window content, however, you should not arbitrarily set status-bar orientation using this method"
To understand that, put a breakpoint in the shouldAutorotate method and you will see that it is called juste after setting the status bar orientation.
Here is how I fixed.
https://stackoverflow.com/a/14530123/1901733
The current question is linked with the question from the url above.
The statusBarOrientation is a real problem in ios6.

Hiding the KeyWindow to cause a blank screenshot to be taken

I am trying to prevent the Apple implemmntation of taking a screenshot of the current screen contents when an app suspends into the background. I have found a piece of code that sort of works but it comes with a catch. What it does is that it clears the keywindow on the screen so when the snapshot is taken, it is of a blank screen. This is the code snippet for the functionality:
- (void)applicationWillResignActive:(UIApplication *)application
{
[ UIApplication sharedApplication ].keyWindow.hidden = YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[ UIApplication sharedApplication ].keyWindow.hidden = YES;
}
The trouble with the above code is that when the app returns to the foreground, the view is gone as it has become hidden and i cannot unhide it with a simple
[ UIApplication sharedApplication ].keyWindow.hidden = NO;
in the applicationWillEnterForeground method of the app delegate. Does anyone know of a way to regain back the hidden view once i have hidden it in the background methods? Right now it is a black screen as the view has been hidden. What exactly happens when you hide a keywindow before going to background and then coming back. is that keywindow you hid before no longer the keywindow? Can anyone point me in the correct direction?
Thanks
Just made a demo project and was able to reproduce your issue. Indeed, the keyWindow property of the application is nil when applicationWillEnterForeground: is called.
Many times, your application's delegate will have a reference to its window - this is usually the Xcode default template for many applications. I was able to resolve the issue by calling
self.window.hidden = NO;
Instead of [UIApplication sharedApplication.keyWindow.hidden = NO;. Assuming that, like most of the templates, your application delegate has a window reference.
Another alternative that worked for me is to call [self.window makeKeyAndVisible];.
All this was done on the iOS 6 simulator.
Hope this helps!

How to load a SplitViewController in a universal app

This is probably a straight forward problem, but I for some reason cannot get it to work.
I have an universal app, with icons on the start screen (TTLauncher) and use TTNavigator to push in view controllers.
On one of the icons a normal tableView is loaded (for the iPhone). As that is not nice for the iPad, I want to load that same tableView (if possible, as it has all the logic in it, I can adjust the code to include the required code for the splitview).
But how do I do that?
I created a UIViewController (called SplitViewController), with a XIB in which I included the SplitViewController, and I made the class for the RootViewContorller of the splitview my custom TableViewController..
I thought it would work if I added the view to the TTNavigator, but nothing happens:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"TEST!");
TTNavigator* navigator = [TTNavigator navigator];
[[navigator window] addSubview:splitViewController.view];
}
(Test is called)
I finally did try:
- (void)viewDidLoad
{
[super viewDidLoad];
//[self setView:splitViewController.view];
[window addSubview:splitViewController.view];
}
But still nothing happens. I think it is obvious that I don't understand how this works...
Any tips? Or how can I better describe this, I assume, simple problem?

How can I display a splash screen for longer on an iPhone?

How can I display a splash screen for a longer period of time than the default time on an iPhone?
Read the Apple iPhone Human Interface Guidelines (HIG). The "splash screen" isn't supposed to be for branding or displaying a logo, it's supposed to look like the default condition of the app so it appears to start up quickly.
Making it stay there for longer would be a violation of the HIG.
The simplest way to do this is to create a UIImageView who's image is your Default.png. In your applicationDidFinishLaunching: method, add that image view to your window, and hide it when you'd like your splash screen to go away.
I needed to do this to block showing a table view until the data was loaded over the network. I used a variation of one I found here:
http://michael.burford.net/2008/11/fading-defaultpng-when-iphone-app.html
In the interface of your App Delegate:
#interface AppDelegate : NSObject
{
UIImageView *splashView;
}
In the implementation:
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// After this line: [window addSubview:tabBarController.view];
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default.png"];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
// Do your time consuming setup
[splashView removeFromSuperview];
[splashView release];
}
Make sure you have a Default.png in the resources
in your appDelegate , theres a method called applicationDidFinishedLaunching use a sleep function. Pass a digit in the sleep function for the no. of seconds you want to hold screen.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[window makeKeyAndVisible];
[window addSubview:viewController.view];
sleep(5);
return YES;
}
I searched so much for this thing and everybody gave their own complex point of view. I couldn't find a simple way that would just let me do it.
KISS ( Keep it simple and Smart :)
I avoided the actual as its offensive.
Even though it is against the guidelines but if you still want to do this than a better approach rather than sleeping thread will be
//Extend the splash screen for 3 seconds.
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
this way the main thread is not blocked and if it is listening for any notifications and some other network related stuff, it still carries on.
UPDATE FOR SWIFT:
NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow:3))
I did it pretty simply, by having my rootViewController push a modalViewController, loading from "Splash.nib" in a subclass of UIViewController I called "SplashViewController". The exact call was:
- (void) viewDidLoad {
SplashViewController *splashScreen = [[[SplashViewController alloc]
initWithNibName:#"SplashViewController" bundle:nil] autorelease];
[self presentModalViewController:splashScreen animated:NO];
//continue loading while MVC is over top...
When you launch the app, it pops right up, like a splash screen should. Then, the SplashViewController nib is just a full-screen UIImageView with a splash png, 320x480. After a 1-second NSTimer (anything more did seem to get in the way), it fires timerFireMethod, a custom method that just calls
[self dismissModalViewControllerAnimated:YES];
Then the modal VC just slides down and away, leaving my top tableView. The nice thing is, while the MVC is up, the underlying table can continue to load due to the independent nature of modal view controllers. So, I don't think this violates the HIGs, and actually does allow for faster launching. What would you rather look at, a cute picture, or an empty default view (snore)?
Yes, the simplest way is (remember to add your 'default.png' to targets -> [yourProjectName]: launch images in 'xCode'):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSThread sleepForTimeInterval:3.0];
}
Make your app take longer to load.
In all seriousness, Paul Tomblin is correct that this usually isn't a good idea. Default.png is a mechanism intended to make your app appear to load faster by holding an "empty" screenshot. Using it for a splash screen is a minor abuse, but intentionally making that splash screen appear for longer than it needs to is almost sick. (It will also degrade your user experience. Remember, every second the splash screen is visible is a second that the user is impatiently staring at your logo, swearing they'll switch to the first decent competitor they can find.)
If you're trying to cover for some sort of secondary loading--for example, if the interface has loaded and you're just waiting to get some data from the network--then it's probably okay, and Ben Gottlieb's approach is fine. I'd suggest adding a progress bar or spinner to make it clear to the user that something really is going on.
simply use sleep(time in seconds); in your applicationDidFinishedLaunching method
Here is my simple splash screen code. 'splashView' is an outlet for a view
that contains an image logo, UIActivityIndicator, and a "Load.." label (added
to my 'MainWIndow.xib' in IB). The activity indicator is set to 'animating' in IB,
I then spawn a separate thread to load the data. When done, I remove the
splashView and add my normal application view:
-(void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:splashView];
[NSThread detachNewThreadSelector:#selector(getInitialData:)
toTarget:self withObject:nil];
}
-(void)getInitialData:(id)obj {
[NSThread sleepForTimeInterval:3.0]; // simulate waiting for server response
[splashView removeFromSuperview];
[window addSubview:tabBarController.view];
}
Inside your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Sleep is code to stop the slash screen for 5MoreSeconds
sleep(5);
[self initializeStoryBoardBasedOnScreenSize];
return YES;
}
//VKJ
In Xcode 6.3, you can show the launch screen.xib and even put a indicator on it, first it will show the default launch screen it is replaced by the nib so the user doesn't know it changed and then if everything is loaded hide it :-)
func showLaunchScreen() {
// show launchscreen
launchView = NSBundle.mainBundle().loadNibNamed("LaunchScreen", owner: self, options: nil)[0] as! UIView
launchView.frame = self.view.bounds;
self.view.addSubview(launchView)
// show indicator
launchScreenIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) as UIActivityIndicatorView
launchScreenIndicator.center = CGPointMake(self.view.center.x, self.view.center.y+100)
launchScreenIndicator.hidesWhenStopped = true
launchScreenIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
launchView.addSubview(launchScreenIndicator)
self.view.addSubview(launchView)
launchScreenIndicator.startAnimating()
self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true) //or animated: false
}
func removeLaunchScreen() {
println("remove launchscreen")
self.launchView.removeFromSuperview()
self.launchScreenIndicator.stopAnimating()
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
Write an actual splash screen class.
Here's a freely usable splash screen that I recently posted in my iPhone in Action blog:
http://iphoneinaction.manning.com/iphone_in_action/2009/03/creating-a-splash-screen-part-one.html
The simplest way is to put your application's main thread into a sleep mode for desired period of time. Provided that "Default.png" exists in your application's bundle it will be displayed for as long as the main thread is asleep:
-(void)applicationDidFinishLaunching:(UIApplication *)application {
[NSThread sleepForTimeInterval:5];
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[window setBackgroundColor:[UIColor yellowColor]];
[window makeKeyAndVisible];
}
As you are already aware, it's a horribly bad idea to do but it should work just fine...
just make the window sleep for some seconds in applicationDidFininshLaunchings method
example: sleep(3)
According to the Apple HIG you should not do that. But if your application needs to do so for definite purpose, you can do:
import <unistd.h> in your AppDelegate.m
Write the following line at the first of the "application didFinishLaunchingWithOptions:" method
sleep(//your time in sec goes here//);
I have done this as below:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"LaunchScreen" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"splashView"];
self.window.rootViewController = viewController;
Note: LaunchScreen is the launch story borad and splashView is the storyboardIdentifier
For stylish splash screen tutorial check out this
http://adeem.me/blog/2009/06/22/creating-splash-screen-tutorial-for-iphone/
I agree it can make sense to have a splash screen when an app starts - especially if it needs to get some data from a web site first.
As far as following Apple HIG - take a look at the (MobileMe) iDisk app; until you register your member details the app shows a typical uitableview Default.png before very quickly showing a fullscreen view.
What I did is presented a modalview controller in the initial screen and then dissmiss it after several seconds
- (void)viewDidLoad
{
[super viewDidLoad];
....
saSplash = [storyboard instantiateViewControllerWithIdentifier:#"SASplashViewController"];
saSplash.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController: saSplash animated:NO];
}
-(void) dismissSASplash {
[saSplash dismissModalViewControllerAnimated:NO];
}
There are many options already posted here, but I ran into cocoapod today that allows you to display the contents of your LaunchScreen.xib as the initial view controller:
https://github.com/granoff/LaunchScreen (Also see this blog post from the author with more implementation details.)
This seems like a fairly straight-forward way to do this, and better than the vast majority of answers posted here. (Of course it wasn't possible until the introduction of LaunchScreen files in the first place.) It is possible to display an activity indicator (or anything else you want) on top of the view.
As for why you would want to do this, I'm surprised that no one has mentioned that there are often publisher and/or partner requirements around this sort of thing. It's VERY common in games, but advertising-funded applications as well.
Also note that this does act counter to the HIG, but then so does waiting to load any content after your application launches. Remember that the HIG are guidelines, and not requirements.
One final note: It's my personal opinion, that any time an initial screen like this is implemented, you should be able to tap to dismiss it.
Swift 2.0
Use following line in didFinishLaunchingWithOptions: delegate method:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSThread.sleepForTimeInterval(5.0)
return true
}
But I recommend this:
Put your image in a UIImageView full screen as a subview on the top of your main view thus covering your other UI. Set a timer to remove it after some seconds (possibly with effects) now showing your application.
import UIKit
class ViewController: UIViewController
{
var splashScreen:UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
self.splashScreen = UIImageView(frame: self.view.frame)
self.splashScreen.image = UIImage(named: "Logo.png")
self.view.addSubview(self.splashScreen)
var removeSplashScreen = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: "removeSplashImage", userInfo: nil, repeats: false)
}
func removeSplashImage()
{
self.splashScreen.removeFromSuperview()
}
}
There is default image (default.png) is shown when you start your app.
So you can add a new viewcontroller which will display the that default image in the application didFinishLoading.
So by this logic you display the default image for a bit longer.
Swift version:
Add this line in the AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSThread.sleepForTimeInterval(2.0)//in seconds
return true
}