Calling the appropriate setStatusBarHidden per iOS version - iphone

Today my app approved, but I got emails from users says it crash. I figured out that
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
Is the problem, Because users have firmware 3.1.x this API is not working and app crash.
So I have replace it with
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=3.2)
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
else
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
My questions...
Is what I did the best solution?
Why XCODE did not warn me that SetStatusBarHidden withAnimation is not in 3.0 while I set my Traget OS firmware 3.0?
Do I have to check on every API to see if it is working with my Target OS?
Thank you

I'd recommend you to use the following snipplet of code instead of checking against the version of the os, rather check if a selector is currently available.
if([[UIApplication sharedApplication] respondsToSelector:#selector(setStatusBarHidden: withAnimation:)])
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];

Use only
[[UIApplication sharedApplication] setStatusBarHidden:YES];
instead of
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
It works with no warning

Unfortunately, if you are compiling with the 4.0 SDK using the simulator, the above solutions will give you a warning, which by default is treated as an error:
warning: 'setStatusBarHidden:animated:' is deprecated (declared at /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h:267)
So you can either set 'Treat warnings as errors' to false in the build settings, or you can use macros to conditionally include the correct code. I have my base SDK set to Device 4.0, my target os is 3.1, and am using this macro:
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];
#else
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
#endif
#endif
Note that setStatusBarHidden:withAnimation came available in 3.2.

That's probably the best thing to do, other than limiting your application to >=3.2. anyways, you xcode should give you a warning that the SetStatusBarHidden withAnimation message is not supported ("may not respond to").

Related

UIApplication openURL: Open Just iTunes App

How can I open up just the iTunes app. Not any subpages in the app like a song or video but just the app. Right now I am using: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms://music"]]; but sometimes this gives an error saying invalid hostname: music. When I use [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms://"]]; I get bad URL.
I have tested the following and it seems to be working fine
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms://?action=music"]];

disable automatic locking on iphone with iOS 4.3.3

I want to disable the auto lock on my iOS4.3.3 program. I found on the web the same answer several times for this and working for xcode3 but I cant find it to work with mine.. help pls? thks
This should work on iOS 4.3.3. If not then you're doing something wrong:
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
I did it in a ViewController like this.
-(void) viewDidAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
-(void) viewDidDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Need help with openURL:[NSURL URLWithString:#"tel:+91 (number)"]] from an iPhone app

I need to call a number +91 (number),but if i use tel:// url it does not make a call from a iphone app.
How to call a number coming in any format.
The iphone will dial a number using either of the formats listed below.
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel://15415551234"]];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel:15415551234"]];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel:1-541-555-1234"]];
Link for Apple documentation on the tel: url scheme

Call the official *Settings* app from my app on iPhone

At one point in my app, I would like to redirect the user to the official Settings app. If possible, I also want go straight to the Network section within the Settings app.
I think what I need is the Settings app's url scheme and the format to construct my request. But I doubt that calling such an official app is forbidden.
Can anyone can help me?
As noted in the comments below, this is no longer possible in iOS version 5.1 and after.
If you are on iOS 5.0, the following applies:
This is now possible in iOS 5 using the 'prefs:' url scheme. It works from a web page or from an app.
example urls:
prefs:root=General
prefs:root=General&path=Network
sample usage:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=General"]]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=General&path=Network"]]
From IOS 8 you can call the settings from within app with this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
It's also work in iOS version > 5.1, but you must add an URL schemes in URL types in Xcode:
Then you can use
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=WIFI"]];
It's can open system WiFi setting
now.
Other path please find in this answer: iOS Launching Settings -> Restrictions URL Scheme.
Bad news: As #Hlung and #jasongregori suggested, for iDevices whose OS version >= iOS 5.1 && < iOS 8.0, there is once again NO official/documented way to call the built-in Settings app from a third-party app. Period.
Calling the settings app from other app is possible only from iOS 8. So, use the following code
if([CLLocationManager locationServicesEnabled]&&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
//...Location service is enabled
}
else
{
if([[[UIDevice currentDevice] systemVersion] floatValue]<8.0)
{
UIAlertView* curr1=[[UIAlertView alloc] initWithTitle:#"This app does not have access to Location service" message:#"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[curr1 show];
}
else
{
UIAlertView* curr2=[[UIAlertView alloc] initWithTitle:#"This app does not have access to Location service" message:#"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Settings", nil];
curr2.tag=121;
[curr2 show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 121 && buttonIndex == 1)
{
//code for opening settings app in iOS 8
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
from iOS 8, you can redirect with
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
enjoy coding
Just an additional answer to add onto the one's addressing iOS8+. If you're supporting anything below 8, you should check to see if it's supported
BOOL canGoToSettings = (UIApplicationOpenSettingsURLString != NULL);
if (canGoToSettings)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
For settings in iOS 9 this is worked for me.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=Settings"]];
But make sure you add a url schemes in URL types in
Info tab in app targets.
For iOS 10 you can use:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"App-Prefs:root=Settings"]];
It is also working on iOS 9!

setApplicationIconBadgeNumber when called multiple times does not update the badge

I am working on an application where in I have to update the badge shown in the app icon multiple times. However, what I have noted is that, the setApplicationIconBadgeNumber api just works once during the lifetime of the app. I have tries using the UILocalNotification, and it works then but, I dont want to follow that route. Have you guys faced a similar problem. If yes, any pointers?
Regards
Nitin
This is a bug in iOS. It's still present today in 6.0.1, where I just fixed it with a work-around:
// Clear app badge number. Work-around for bug in iOS.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Might the problem be from where you are calling it?
Wrong:
// This is only called once during application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Correct:
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}