views:

207

answers:

2

I'm writing an iPhone app against the Base 4.0 SDK, but I'm targeting OS 3.1.3 so OS 3 users can use the app.

I call:

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];

which is deprecated in iOS 4.0. I'm aware of this, and have measures in place to call the newer "withAnimation" version if we are running under iOS 4.0 or greater.

However, I'm getting a warning that I'm calling a deprecated SDK.

I'd like to disable this specific warning in this specific place. I want all other warnings (including the same deprecated warning in other locations)

Can this be accomplished in XCode?

+1  A: 

You might be able to use GCC pragmas. This should disable the deprecated warning for the enclosed line of code.

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
#pragma GCC diagnostic pop

I don't know if this will work with Clang, but it should work with GCC.

Basically, it saves the state of the warnings/errors, disables the deprecated warning, compiles the line, then restores the state of the diagnostics.

paxswill
Those pragmas aren't allowed inside functions, they would have to surround some helper function.
Georg Fritzsche
Thanks. This didn't actually work. It looks like the 'push' and 'pop' keywords are not valid, as they generated their own warnings.
David Coufal
+2  A: 

You can use NSInvocation to get around the warnings independent of the compiler used:

UIApplication *app = [UIApplication sharedApplication];
SEL sel = @selector(setStatusBarHidden:animated:);
NSMethodSignature *sig = [app methodSignatureForSelector:sel];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
BOOL b = YES;
[inv setTarget:app];
[inv setSelector:sel];
[inv setArgument:&b atIndex:2];
[inv setArgument:&b atIndex:3];
[inv invoke];

Or in a less error-tolerant way:

UIApplication *app = [UIApplication sharedApplication];
SEL sel = @selector(setStatusBarHidden:animated:);
IMP imp = [app methodForSelector:sel];
imp(app, sel, YES, YES);
Georg Fritzsche
Could the `[NSMethodSignature signatureWithObjCTypes:"v@cc"]` be replaced with `[[UIApplication sharedApplication] methodSignatureForSelector:@selector(setStatusBarHidden)]` to make it easier to read?
paxswill
Right, of course.
Georg Fritzsche
Works a treat. Thanks.
David Coufal

related questions