LoginSignup
34
36

More than 5 years have passed since last update.

バックグラウンドで、iBeaconsのイベントを受けて通信したりする

Last updated at Posted at 2013-11-19

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に追記をします。
スクリーンショット 2013-11-19 17.05.35.png
Required background modesにApp registers for location updatesを。

これでバックグラウンドでもビーコンのイベントで通信してくれます。

34
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
36