【问题标题】:Is There any way to get current location without using the CLLocationManager delegates in my application是否有任何方法可以在我的应用程序中不使用 CLLocationManager 委托来获取当前位置
【发布时间】:2013-10-20 14:12:47
【问题描述】:

我想在我的库中提供一种返回当前位置的方法。但我不能这样做,因为位置会更新并进入 CLLocationManager 委托方法。所以有什么方法可以返回用户的位置在我的库中调用一个方法之后。

提前致谢。

【问题讨论】:

    标签: iphone ios ipad geolocation cllocationmanager


    【解决方案1】:

    您可以将block 参数添加到您的库方法中,该方法将在收到位置更新时调用。然后可以将该块保存在一个属性中(如果多个对象可能同时等待,则可以保存到一个数组中)。

    附言你真的应该使用CLLocationManager。任何其他确定位置的方法都将不太准确/同样(如果不是更多)耗时。


    你可以定义一个像这样的块(取决于你想通过多少个位置):

    typedef void(^LocationUpdateBlock)(CLLocation *location);
    

    然后你添加一个公共接口供感兴趣的类注册(简单版):

    property (copy, nonatomic) LocationUpdateBlock locationUpdateBlock;
    

    然后在你的委托方法中:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        if (self.locationUpdateBlock != nil) {
            self.locationUpdateBlock([locations lastObject]);
        }
    }
    

    【讨论】:

    • +1 另外,绕过CLLocationManager 将绕过内置的位置收集权限过程,并且根据App Store Review Guidelines 的§4.1,“在收集之前未通知并获得用户同意的应用程序,传输或使用位置数据将被拒绝。”就像你说的那样,使用CLLocationManager 并提供一个使用异步调用的完成块的 API 是可行的方法。
    • @Rob 即使库使用CLLocationManager,用户仍将获得权限对话框。这完全没问题,违反了任何准则。如果真的有可能,那将是一个非常大的隐私漏洞。
    • @St3fan 抱歉,我不清楚。这正是我的观点,你想使用CLLocationManager,这样你就会得到确认。我只是想提供另一个原因,为什么 Wain 建议使用 CLLocationManager 而不是试图绕过它是个好主意。
    • @Wain 抱歉,我没有得到您的解决方案,您能否详细解释一下,该块将如何通过一些示例代码出现在此处。
    猜你喜欢
    • 2011-11-18
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2019-09-02
    • 2011-03-12
    相关资源
    最近更新 更多