Varying iPhone deployment target: Handling Framework constants
I am building an app that needs to work on multiple versions of iPhone OS and makes use of increased accuracy for Core Location in iOS4.
I have code that does the following:
if ([m_locationManager respondsToSelector:@selector(startMonitoringSignificantLocationChanges)]) {
m_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
}
else {
m_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
in this case, kCLLocationAccuracyBestForNavigation is a constant introduced in iOS 4, and startMonitoringSignificantLocationChanges is a method o开发者_如何学JAVAf m_locationManager only introduced in iOS4.
My problem is, that this code is failing on an iPhone 3.1.3 device. It is trying to link to kCLLocationAccuracyBestForNavigation, but it can't find the symbol. My question is, how do I only link to this symbol in the iOS4 version, but not the iPhone 3.1.3 version?
Thanks, C
I figured out the answer by myself. I'll put it here, so as to be helpful to others.
You need to weak-link to the Core Location framework so that the kCLLocationAccuracyBestForNavigation symbol is not loaded on phones which do not have older versions.
In order to specify which frameworks are weak-linked, you have to double-click on the target app and go to the 'general' tab and select the linking of the library. I simply set Core Location to 'Weak' and it works on iPhone 3.1.3.
You can also test for a constant with:
if(&kCLLocationAccuracyBestForNavigation != nil)
精彩评论