【问题标题】:Can I safely call an iOS 8 selector from iOS 7?我可以从 iOS 7 安全地调用 iOS 8 选择器吗?
【发布时间】:2014-10-20 07:31:26
【问题描述】:

我正在为 iOS 8 实现新的核心位置权限,但我想使用 Xcode 5.1.1 和 iOS 7.1 SDK 进行编译和发布。

具体来说,我想在 CLLocationManager 上调用“requestWhenInUseAuthorization”方法,该方法仅在 iOS 8 中可用。

这样做有什么风险?这是一个好习惯吗?我可以忽略“未声明的选择器”警告吗?

// Warning: Undeclared Selector
SEL requestWhenInUse = @selector(requestWhenInUseAuthorization); 
if( [self.sharedLocationManager respondsToSelector:requestWhenInUse] )
{
    // Warning: May cause leak because selector is unknown
    [self.sharedLocationManager performSelector:requestWhenInUse];
}
[self.sharedLocationManager startUpdatingLocation];

【问题讨论】:

    标签: ios core-location ios8 performselector


    【解决方案1】:

    您的代码很好。要禁止显示警告,请添加

    @interface CLLocationManager (iOS8Method)
    
    - (void)requestWhenInUseAuthorization;
    
    @end
    

    然后就可以调用方法了

    if( [self.sharedLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] )
    {
        [self.sharedLocationManager requestWhenInUseAuthorization];
    }
    

    还阅读了这个问题:performSelector may cause a leak because its selector is unknown

    【讨论】:

    • 看看它是否通过了 Apple API 测试会很有趣。
    • @Zaph 我不明白为什么会出现问题,因为它现在是公开记录的方法,@interface 只告诉编译此方法存在而不创建类别。
    • 我说这会很有趣。我确实看到了一个原因。一般而言,并非所有 iOS8 API 都适用于 iOS7 SDK。因此,Apple 要么验证了哪些是安全的,并且可以从 iOS7 SDK 中工作,并将该信息放入他们的测试工具中以明确允许它们,要么他们需要全部拒绝。鉴于 Apple 似乎希望应用程序迁移到最新的 IOS 版本,我打赌否认所有。
    猜你喜欢
    • 2015-09-09
    • 1970-01-01
    • 2015-11-28
    • 2021-01-10
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多