【问题标题】:Location service Turn off Notification定位服务关闭通知
【发布时间】:2013-12-05 11:46:36
【问题描述】:

我的应用使用CoreLocation 并且应用正在注册后台位置更新

当用户从设置中关闭定位服务时,我想显示UILocalNotification

iOS 6.1.3 及更低版本关闭定位服务后,我的应用程序能够在后台运行代码,因此我可以向用户显示UILocalNotification .

但是在 iOS 7 中,一旦我关闭了定位服务,我的应用就无法在后台执行单行代码。

即使在 iOS 7 中,我也注册了后台应用刷新。

【问题讨论】:

    标签: ios objective-c core-location


    【解决方案1】:

    如果服务被禁用,您可以设置委托方法并显示 UILocalNotification:

    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
    

    每次用户在设置中禁用/启用位置服务时都会调用它。 您也可以使用此方法找出是否已禁用/启用位置服务

    [CLLocationManager locationServicesEnabled]
    

    我没有测试它,但也许你觉得它有用。

    【讨论】:

    • 我也用这个测试过。但是一旦我关闭位置服务,这个方法也不会在 iOS 7 中被调用。
    • 看看这个帖子link。我认为如果您在 beginBackgroundTaskWithExpirationHandler 中添加对 [CLLocationManager locationServicesEnabled] 的检查:如果应用程序在后台,它可以工作。
    • 在 iOS 7 后台定时器的寿命只有 3 分钟。用户在此时间段后关闭位置服务怎么办?
    • 在 didUpdateToLocation: 方法的那篇文章中,检查它是否在后台,如果是 sendBackgroundLocationToServer: 被调用,则调用 beginBackgroundTaskWithExpirationHandler:。所以 didUpdateToLocation: 方法每次都会被调用,即使它处于后台模式超过 3 分钟,对吧?
    • 这个链接只有正确的。但是如果我在 5 分钟或更长时间内没有收到 LocationUpdate 并且用户停止关闭位置服务。在这种情况下,这种方法就没有用了。
    【解决方案2】:

    添加@property (nonatomic,strong) 可达性 *internetReachable;

    然后在 AppDelegate.h 文件中,

    在didFinishedLaunching方法的AppDelegate.m文件中添加这行代码:

    [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(NetWorkCheangeNoti:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[ReachabilityreachabilityForInternetConnection] 保留]; [internetReachable startNotifier];

    在 AppDelegate.m 中实现该方法,例如:

    -(void)NetWorkCheangeNoti:(Reachability *) 信息 { NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 开关(互联网状态) { 案例不可达: {

            NSLog(@"The internet is down.");
        }
            break;
    
        case ReachableViaWiFi:
        {
            //NSLog(@"The internet is working via WIFI");
        }
            break;
    
        case ReachableViaWWAN:
        {
            //NSLog(@"The internet is working via WWAN!");
        }
            break;
    
        default:
            break;
    }
    

    }

    试试这个..!!

    【讨论】:

    • 这个答案是开箱即用的。问题是关于位置服务而不是互联网,
    • 你想说什么?它尊重位置吗?
    猜你喜欢
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    相关资源
    最近更新 更多