简体   繁体   中英

No visible @interface for 'CLLocationManager' declares the selector 'requestAlwaysAuthorization'

We are making an app to be compatible with iOS 8, but at the same time, some of our developers do not have Xcode 6 yet, so they are getting this error when trying to call

[self.locationManager requestAlwaysAuthorization];

Even if it is inside an if

if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
    [self.locationManager requestAlwaysAuthorization];
}

How can we solve this to compile on Xcode 5?

The following is the proper way to deal with this. This assumes that your app has a "Deployment Target" of iOS 7.x or earlier and you need to compile the project with different values for the "Base SDK" (such as iOS 8 under Xcode 6 and iOS 7 under Xcode 5):

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    // Being compiled with a Base SDK of iOS 8 or later
    // Now do a runtime check to be sure the method is supported
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    } else {
        // No such method on this device - do something else as needed
    }
#else
    // Being compiled with a Base SDK of iOS 7.x or earlier
    // No such method - do something else as needed
#endif

Accepted answer didn't work for my particular situation. Due to build enviroment limitations (Phonegap/Cordova) I'm stuck on compiling against the iOS7 SDK only.

I implemented the following (as suggested in comments):

 if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    // Use performSelector: so compiler won't blow up on this
    [self.locationManager performSelector:@selector(requestAlwaysAuthorization)];
}    

It might show compiler warnings, but atleast it works in that specific case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM