【问题标题】:startMonitoringForRegion when App close应用关闭时的 startMonitoringForRegion
【发布时间】:2015-05-04 02:10:25
【问题描述】:

我正在使用startMonitoringForRegion 监控特定区域。 当应用程序未运行时,当用户通过比较 AppDelegate 中的[launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"] 进入/离开特定位置时,我已成功生成本地通知。

但现在我无法理解如何知道用户进入了某个位置或离开了某个位置。 我无法找到检查响应的方法。我可以在警报正文中显示响应吗? 我在互联网上搜索,但找不到任何在 AppDelegate 中编写了一些代码的教程。

【问题讨论】:

    标签: ios cllocationmanager


    【解决方案1】:

    您可以使用位置管理器委托方法

    - (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
    
        if (state == CLRegionStateInside) {
    
            // We entered a region
             NSLog(@"Inside region");
     if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){
    
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.alertBody = @"Region Detected";
                notification.soundName = @"Default";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    
    
        } else if (state == CLRegionStateOutside) {
    
           // We are outside region
            NSLog(@"Outside region");
        }
    }
    

    在应用程序中

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PerformAction" object:nil userInfo:nil];
    }
    

    在你的 ViewController viewDidLoad 方法中:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:@"PerformAction" object:nil];
    

    在您的同一个 viewController 中添加以下方法并执行您的操作

    -(void)performAction
    {
      // perform your action
    }
    

    【讨论】:

    • 在 AppDelegate 我必须写这个?我在didFinishLaunchingWithOptions 方法内的AppDelegate 中比较[launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]。那么我需要在didFinishLaunchingWithOptions 中编写你的代码吗??
    • 但你为什么要在 appdelegate 中编写代码?您可以确定是否使用此委托方法用户是否在监控区域中
    • 当我的应用程序未运行并且我输入该位置时,应用程序会自行重新启动。所以我必须在 AppDelegate 中写一些代码来说明下一步该做什么。请阅读here
    • 这样你就可以创建一个本地通知来通知用户。
    • 是的,我正在生成本地通知,但我不知道天气用户输入或退出的位置以及区域->标识符是什么,以便我知道该位置并执行一些进一步的任务。跨度>
    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多