iPhone

iOS Navigation Bar behind status bar after closing full screen MPMoviePlayer

I had a problem with an iOS app I am creating. I have a view that has a UINavigationBar at the top and a table view  that displays a full screen video on didselectrowatindexpath. Once the video is playing if the device is rotated and the done button is pressed the video will disappear but the navigation bar has moved to the top of the screen displaying behind the status bar. This leaves a gap between the nav bar and the top of the table view.

Solution

I tried a number of things to fix it including setting the status bar to hidden and then not hidden once video had exited full screen. I also tried the same with the navigation bar.

The fix that actually worked in the end did follow these lines. I added observers to MPMoviePlayer notifications and hid the nav bar when the video entered full screen mode. Once the video exited full screen mode I showed the nav bar again.

 
Below are how you add the observers that are required. I placed them in the viewDidLoad method.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didExitFullScreen:)
                                                 name:MPMoviePlayerDidExitFullscreenNotification
                                               object:nil];
 
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didEnterFullScreen:)
                                                 name:MPMoviePlayerDidEnterFullscreenNotification
                                               object:nil];

I then created the methods to hide and show the navigation bar. These methods can include any other UI updates you might need to do such as refreshing a table view.

-(void)didEnterFullScreen:(id)sender{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
 
-(void)didExitFullScreen:(id)sender{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
 }

After creating these the Navigation bar behaved as expected and no longer displayed behind the status bar.
There is still one more step to the process and that is removing the observers to the notifications. I added these to the dealloc method

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidEnterFullscreenNotification object:nil];

iOS – Check if wifi is being used

In an app I was making recently I wanted to check if the user was using a wifi connection. I used the method below to check and just checked whether it returned true or false.

- (BOOL)networkCheck{
 Reachability *currentReachability = [[Reachability reachabilityForInternetConnection] retain];
 NetworkStatus networkStatus = [currentReachability currentReachabilityStatus];
 [currentReachability release];
switch (networkStatus)
 {
 case NotReachable: {
 NSLog(@"Not connected");
 return false;
 break;
 }
 
case ReachableViaWWAN:{
 NSLog(@"Cellular Network");
 return false;
 break;
 }
 
case ReachableViaWiFi:{
 NSLog(@"Wifi");
 return true;
 break;
 }
 }
 
return false;
}

Upgrading iPhone apps to run full screen on iPhone 5

As you probably know already, the iPhone 5 is taller than the previous iPhone 4S and iPhone 4. When I first heard about the change is size my first thought was how long is it going to take to update all my apps to work on these devices.

Turns out that so far it hasn’t been hard at all. I have just had to create a new Default.png to cater for the iPhone 5 and the rest is done automatically.
The new default image is 640px wide and 1136px tall and needs to be called Default-568h@2x.png. 

One thing to be aware of is that as much as this process is very easy, it might not work for every single app as some apps have positions and layouts hard coded. The apps that have standard layouts without hard coded values tend to be the ones that benefit from this automatic upgrade.

ShareKit 0.2.1 Twitter not working

I added ShareKit to my project and got the following error:

Twitter Send Status Error: {"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/update.json"}

The following is how I fixed it:
Around line 54 you will see the following code:

self.authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
self.requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
self.accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];

These need to be changed to https://api.twitter.com/oauth rather than https://twitter.com.

NGTabBarController for iOS

A custom TabBarController which can be positioned on the bottom, top, left or top. Utilizes iOS 5 Containment API if possible, but works on iOS 4 too. The TabBar is fully customizable with a tintColor or background image as well as the possibility to show/hide the item highlight and the possibility to change the text colors, have image-only tabBar items etc.
Download the source code here

NGColoredViewController *vc1 = [[NGColoredViewController alloc] initWithNibName:nil bundle:nil];
NGColoredViewController *vc2 = [[NGColoredViewController alloc] initWithNibName:nil bundle:nil];
NGColoredViewController *vc3 = [[NGColoredViewController alloc] initWithNibName:nil bundle:nil];
NGColoredViewController *vc4 = [[NGColoredViewController alloc] initWithNibName:nil bundle:nil];
NGColoredViewController *vc5 = [[NGColoredViewController alloc] initWithNibName:nil bundle:nil];
 
vc1.ng_tabBarItem = [NGTabBarItem itemWithTitle:@"Home" image:image1];
vc2.ng_tabBarItem = [NGTabBarItem itemWithTitle:@"Images" image:image2];
vc3.ng_tabBarItem = [NGTabBarItem itemWithTitle:@"Live" image:image3];
vc4.ng_tabBarItem = [NGTabBarItem itemWithTitle:@"Contact" image:image4];
vc5.ng_tabBarItem = [NGTabBarItem itemWithTitle:@"Settings" image:image5];
 
NSArray *viewController = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5,nil];
 
NGTabBarController *tabBarController = [[NGTestTabBarController alloc] initWithDelegate:self];
 
tabBarController.animation = NGTabBarControllerAnimationMoveAndScale;
tabBarController.layoutStrategy = $isPhone() ? NGTabBarLayoutStrategyEvenlyDistributed : NGTabBarLayoutStrategyCentered;
tabBarController.itemPadding = 10.f;
tabBarController.showsItemHighlight = NO;
tabBarController.tintColor = [UIColor redColor];
tabBarController.viewControllers = viewController;
self.window.rootViewController = tabBarController;

NGTabBarController was created by Matthias Tretter (@myell0w).

iOS Launch Images

When creating a new universal IOS app you need to create a minimum of 6 launch images to support the iPhone and iPad (both including their retina displays).

For the iPhone you have it easy, only 2 are required.

Default.png – 320 x 480
Default@2x.png – 640 x 960 

For the iPad there are a few more:

Default-Portrait.png – 768 x 1004
Default-Portrait@2x.png – 1536 x 2008
Default-Landscape.png – 1024 x 748
Default-Landscape@2x.png – 2048 x 1496

You can specify images for Landscape Right and Left and also Portrait Upside Down using the names below:

Default-LandscapeLeft.png
Default-LandscapeRight.png
Default-PortraitUpsideDown.png

For more information on Custom images and icons visit the apple developer site

How to fix “duplicate symbol _OBJC_METACLASS_$” iPhone

I got this error while working on my latest iPhone app: duplicate symbol _OBJC_METACLASS_$

Turns out that after adding an NSManagedObject subclass for a Core Data entity I moved the files into a group (to make the files look neater). When I generated them again after updating the entity it create 2 sets of the files in the structure.

Basically I deleted both instances of the files and recreated them and that fixed the issue.

Move UITextFields when keyboard opens in iPhone app

While working on my latest app, I was looking for a solution to slide the screen up when the keyboard opens so that text fields aren’t hidden by it.

I came across this one: TPKeyboardAvoiding its a very simple drop in component. You copy a couple classes into your existing project, put all your elements into a scroll view with the class TPKeyboardAvoidingScrollView and call a simple method and you are done.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[scrollView adjustOffsetToIdealIfNeeded];
}

Thanks to this article I was able to find this solution.

How to change colour of UINavigationBar

By default the UINavigationBar on an iOS app is blue. Apple have some built-in styles that you can use

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];

The options available are:

  • UIBarStyleBlack
  • UIBarStyleBlackOpaque
  • UIBarStyleBlackTranslucent
  • UIBarStyleDefault

*UIBarStyleBlackOpaque and UIBarStyleTranslucent have been depreciated. You should use UIBarStyleBlack and set property ‘translucent’ to yes if needed*.

You can also change the colour to any colour you wish using one of the two lines of codes below and adjusting the colour values.

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:.7 green:.5 blue:.2 alpha:1];
 
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
1 2  Scroll to top