2016-05-20 104 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

仍然無法正常工作 –