4

I've done a lot of searching around SO and Google to try to get an answer for this but I haven't been able to put all the pieces of the jigsaw together so any help/pointers would be great.

I'd like to build an app that checks a web service when it enters an iBeacon region. Simple example use case would be this:

  1. user walks into an iBeacon equipped store with app in background

  2. rather than notify the user, the app makes a request to a web server to check if the user ID is a member (the app has been granted permission to send a user ID to the server by the user).

3a. server sends back "true" response

4a. app sends "We have a special members promotion today" notification to lock screen.

OR

3b. server sends back "false" response

4b. app stays silent (no notification to user)

FYI this answer seems to suggest it's possible to send a URL request in the background: Running URL Requests in the Background

Community
  • 1
  • 1
James
  • 1,292
  • 1
  • 14
  • 29

2 Answers2

5

You've pretty much identified everything you need to do.

When your CLLocationManagerDelegate's didEnterRegion: method is called, you can start a background task, and then kick off a network request. Once you've received a response, you can deliver a local notification if necessary, and then end the background task.

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    // If not already performing a background task for this region...

    UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }];

    // perform network request - if successful, display notification. when finished, end background task
}

Although I should note I wouldn't actually place background task management and web request code directly inside my didEnterRegion: method. I'd have a separate web API class which performs the requests, and just make calls to that from within didEnterRegion:.

James Frost
  • 6,960
  • 1
  • 33
  • 42
  • 4
    One other important note is that iOS only gives your app 5 seconds to run in the background from the time you enter the region. So if that web service call responds in 6 seconds, you won't be able to take action on it. (There are other ways to get more background running time, but they are not automatic or guaranteed.) Bottom line is that you had better make that web service fast, and you better have a plan for what happens when you don't get a response. – davidgyoung Jan 22 '14 at 17:06
  • Thanks @James-Frost - it's reassuring that this seems possible. Since posting the above, I was actually thinking I could just use push notifications to send a notification directly to the device when necessary (from which I can launch the app etc) which might get around the need for the app to receive a response. i.e. trigger the process from the phone and then let the server handle all the logic. – James Jan 22 '14 at 17:06
  • @davidgyoung Indeed - that's why it probably makes sense to explicitly start a background task, ask it should give you longer to execute. – James Frost Jan 22 '14 at 17:14
  • @James - You could certainly just tell your server when you enter the range, and then it can determine whether or not to push a notification to the device. – James Frost Jan 22 '14 at 17:15
  • Hi @davidgyoung Thanks for your comment (and all your other help on SO - I have read/learned a lot of your answers!). Your point about a 5sec window is why I started thinking about letting the server/push notifications handle the response. This seems to be a pretty simple solution so that I can send more useful responses to the user and not worry about the 5sec window. Rather than "We have a promotion" type of message I could send "Hi John, [we know you love golf so] here's 50% off golf balls.". If the request doesn't get through for some reason it just fails silently and the user isn't bugged – James Jan 22 '14 at 17:20
  • I've used push notifications, too. The advantage is that you don't get bitten by the 5 sec window. The disadvantage is that push notifications take their own sweet time to be delivered over the air -- it can be minutes after being sent. – davidgyoung Jan 22 '14 at 19:37
0

This might help too (short answer is 'yes' but your logic has to finish in 5 seconds): https://apple.stackexchange.com/questions/174787/can-ibeacon-range-region-monitoring-trigger-dynamic-data-based-notification/174873#174873

Community
  • 1
  • 1
Daniel Iversen
  • 469
  • 1
  • 6
  • 12