1

私はiOS開発にかなり慣れていないので、地域監視を可能にするアプリを作成しようとしています. iOS リマインダー アプリに似ています。

地域の監視を開始するところまでコードがあり、地域に出入りするときに通知する必要があります。ただし、それは私がその地域にいるときだけ教えてくれます。私が去るとき、それは私に教えてくれません。

ご存知のように、私はこれを iPhone でテストしており、間違いなく半径 100 m の外に出ました (家から車で 10 分間運転しました)。私がその地域にいるというメッセージをループし続けましたが、その地域を出たときに変更されることはありませんでした。私はどこが間違っているのか本当に困惑しています。誰でも助けることができますか?また、好奇心から、私はiOS開発に慣れていないので、これは正しい方法ですか? または、これを達成するためのより効率的な方法はありますか? アドバイス、チュートリアル、ハウツーは大歓迎です。みんなありがとう!リージョンの監視を開始するための私のコードとメソッドは以下のとおりです。他のコードが必要な場合はお知らせください。

(IBAction)addRegion:(id)sender {
    if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
        // Create a new region based on the center of the map view.
        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(_mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude);
        region = [[CLCircularRegion alloc] initWithCenter:coord
                                           radius:Radius
                                       identifier:[NSString stringWithFormat:@"%f, %f", _mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude]];


        // Start monitoring the newly created region.
        [_locationManager startMonitoringForRegion:(CLRegion *)region];
    }
    else {
     NSLog(@"Region monitoring is not available.");
    }
}

(IBAction)stopMonitoringRegion:(id)sender {
   [_locationManager stopMonitoringForRegion:(CLRegion *)region];
}

(void)locationManager:(CLLocationManager *)manager
     didDetermineState:(CLRegionState)state forRegion:(CLRegion *)reg {

   if (state == CLRegionStateInside) {
       //call didEnterRegion
       //[_locationManager stopMonitoringForRegion:region];
       [self locationManager:_locationManager didEnterRegion:region];
   }
   if (state == CLRegionStateOutside) {
       //call didExitRegion
       //[_locationManager stopMonitoringForRegion:region];
       [self locationManager:_locationManager didExitRegion:region];
   }
}

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError: %@", error);
}


(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

//requestStateForRegion
if (region == nil) {
    //do nothing
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}else{
[_locationManager requestStateForRegion:region];
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}
}


(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region  {
    NSLog(@"didEnterRegion %@ at %@", region.identifier, [NSDate date]);
}


(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"didExitRegion %@ at %@", region.identifier, [NSDate date]);
}

(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
    NSLog(@"monitoringDidFailForRegion %@: %@ Error: %@", region.identifier, [NSDate date], error);
}
4

0 に答える 0