Reputation: 3435
There is an Obj-C file which is included into several projects with different deployment target. This file has following codeline:
[[UIApplication sharedApplication] openURL:url];
When I compile project targeting iOS 10, I get a warning:
'openURL:' is deprecated: first deprecated in iOS 10.0 - Please use openURL:options:completionHandler: instead
I tried to fix it with the following construction:
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}
but it still generates the same warning!
I do not want to switch this warning off globally, so what I ended with is monstrous
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] openURL:url];
#pragma clang diagnostic pop
}
So I wonder if I really need to have such an ugly code, or maybe I have missed something, and such situation could have been handled in another (more graceful) manner?
Upvotes: 1
Views: 578
Reputation: 318774
If you are targeting iOS 10 and later then there is no need to support the deprecated API. Just use the new one.
No need for an if/else
. Just do:
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
That's it. This updated API was added in iOS 10.0. Since you are supporting only iOS 10 and later, there is no need to try to use the deprecated API.
If this code is being used by some projects that support iOS 10 and later as well as some projects that need to support something older than iOS 10, then you need something like the following:
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
if (@available(iOS 10.0, *)) {
#endif
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
} else {
[[UIApplication sharedApplication] openURL:url];
}
#endif
The effect of this compiler directive is that when building in a project with a Deployment Target of iOS 10.0 or later, the compiled code simply becomes:
[[UIApplication sharedApplication] openURL:url options:@{}
When the code is built in a project with a Deployment Target earlier than iOS 10.0, the compiled code will be:
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}
Upvotes: 4