1

I have been working on an iBeacon app and i'm trying to make it so it posts to a slack room when the user has exited the region for more than 15 minutes. I have tried to start a timer, I've tried to use a start time and then an end time(that was working but I had no way of stopping the loop if the user returned to a beacon). I have all the slack api code, I just need help with code that counts how long someone has been out of the beacon region and if its more than 15 minutes, do some code. Here is the code.. it goes through the loop, but if the user enters back in to a region it does not pick it up.

func locationManager(manager: CLLocationManager!,
    didExitRegion region: CLRegion!) {
        outOfRegion = true


        let start = NSDate(); // <<<<<<<<<< Start time
        let viewController:ViewController = window!.rootViewController as! ViewController
        var names = viewController.theName
        NSLog("You exited the region")


        sendLocalNotificationWithMessage("You exited the region", playSound: true)

        println(locationManager)
        while (outOfRegion) {


            println(outOfRegion)

            let end = NSDate();   // <<<<<<<<<<   end time

            let timeInterval: Double = end.timeIntervalSinceDate(start); // <<<<< Difference in seconds (double)


            println("Time to evaluate problem: \(timeInterval) seconds");
            manager.startMonitoringForRegion(region as! CLBeaconRegion)
            manager.startUpdatingLocation()

            if (timeInterval > 60) {

                //posting to slack API
                var channel = "#botspam"
                let username = "spyBot"
                let text = names + " has left the office"
                let image = ":eyes:"
                //payload array
                let str = "payload={\"channel\": \"\(channel)\", \"username\": \"\(username)\", \"text\": \"\(text)\", \"icon_emoji\": \"\(image)\"}"

                // converting to JSON
                let strData = (str as NSString).dataUsingEncoding(NSUTF8StringEncoding)
                let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData

                //Fusionary web hook
                let url = NSURL(string: "///////")
                var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)

                request.HTTPMethod = "POST"
                request.HTTPBody = strData
                //checking to see if array is valid JSON
                var error : NSError? = nil
                if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: &error) {
                    let results = NSString(data:data, encoding:NSUTF8StringEncoding)
                    println(results)
                }
                else
                {
                    println("data invalid")
                    println(error)
                }
                break;

            }
            println("bye")

        }



       }
Garrett O'Grady
  • 536
  • 5
  • 9

2 Answers2

1

Not sure if you'll be able to achieve this with an iOS app only—iOS heavily restricts background processing for apps. Unless you're a music or location app and simply need to run in the background continuously, you'll only be granted background execution time when certain events happen—e.g. entering range of a beacon. To my knowledge, there's no way to make an iOS app wake up "in 15 minutes", so I'd be curious to know what exactly do you mean by "that was working".

If we consider adding more layers to the stack, I'd try to implement such use case as follows:

  1. The user exits the beacon region, the app gets woken up (didExitRegion) and it sends a message to an external server: User X exited the region on [timestamp here].
  2. The server schedules an action to happen in 15 minutes from the received timestamp, to post to Slack.
  3. If the user goes back into the region before time runs out, the app gets woken up (didEnterRegion) and sends a request to the server to cancel posting to Slack.
  4. If the user doesn't go back into the region before time runs out, the server posts to Slack.
heypiotr
  • 2,139
  • 2
  • 15
  • 22
0

first of all, check this topic and make sure you do something on locationManager:didEnterRegion --> locationManager:didEnterRegion not called when a beacon is detected

Moreover, iBeacon won't call your method instantly when you enter region. Unfortunately, this process is similar to notifications - it happens when it decides to.

Good luck.

Community
  • 1
  • 1
EdgarY
  • 164
  • 1
  • 1
  • 7