2016-05-20 69 views
0

我遇到了问题,我的应用程序。我正在使用信标,但当我在后台运行时,它们似乎不会进入区域。信标正在退出但没有进入。当应用程序在前台运行时,它们会进入。以下是我的代码。提前致谢。ios beacons monitring背景

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:@"BEC26202-A8D8-4A94-80FC-9AC1DE37DAA6"]; 
    NSString *regionIdentifier = @"us.iBeaconModules"; 
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID identifier:regionIdentifier]; 
    NSLog(@"%@",beaconUUID); 

     [self.locationManager requestAlwaysAuthorization]; 


    self.locationManager.delegate = self; 
    self.locationManager.pausesLocationUpdatesAutomatically = NO; 
    self.locationManager.allowsBackgroundLocationUpdates=YES; 
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; 
    [self.locationManager startUpdatingLocation]; 
     self.beaconRegion.notifyOnEntry=YES; 
     self.beaconRegion.notifyOnExit=YES; 
     self.beaconRegion.notifyEntryStateOnDisplay=YES; 
    [self.locationManager startMonitoringForRegion:self.beaconRegion]; 



    return YES; 
} 

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    [manager startRangingBeaconsInRegion:(CLBeaconRegion*)region]; 
    [self.locationManager startUpdatingLocation]; 
    CLBeaconRegion *test = (CLBeaconRegion*)region; 
    NSLog(@"You entered the region. %@", test.minor); 
    [self sendLocalNotificationWithMessage:@"You enterd the region."]; 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.alertBody = @"Are you forgetting something?"; 
    notification.soundName = @"Default"; 
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 
} 

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { 

    CLBeaconRegion *test = (CLBeaconRegion*)region; 

    NSLog(@"You exited the region.%@", test.minor); 
    [self sendLocalNotificationWithMessage:@"You exited the region. "]; 

} 

-(void)sendLocalNotificationWithMessage:(NSString*)message { 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.alertBody = message; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
} 

- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region 
{ 
    [self.locationManager requestStateForRegion:self.beaconRegion]; 
} 

-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region 
{ 
    if (state == CLRegionStateInside) 
    { 
     //Start Ranging 
     [manager startRangingBeaconsInRegion:self.beaconRegion]; 
    } 
    else 
    { 
     //Stop Ranging here 
    } 
} 

回答

0

请加在你的plist NSLocationAlwaysUsageDescription

NSLocationAlwaysUsageDescription 下面这个应用需要定位服务工作

+0

我得到相同的结果。 –

+0

只需在didFinishLaunchingWithOptions locationManager = [[CLLocationManager alloc] init]中添加此项; [locationManager requestWhenInUseAuthorization]; [locationManager requestAlwaysAuthorization]; [self.locationManagerForIbeacan setRegions:@ [region]]; [self.locationManagerForIbeacan startMonitoringBeacons]; – user3306145

+0

仍然无法正常工作 –