iBeacons関連。
ビーコンの領域を横断した時に発生したイベントで呼ばれるメソッド「- locationManager:didRangeBeacons:inRegion:」にリクエストを送るコードを書く
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
// サーバにリクエスト送る
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 送信エラーチェック
if (!aConnection) {
NSLog(@"connection error.");
}
}
-connectionDidFinishLoading:などは省略してます。
これだけではアプリを起動している時しか通信してくれないので、info.plistに追記をします。
Required background modesにApp registers for location updatesを。
これでバックグラウンドでもビーコンのイベントで通信してくれます。