1

I need to check periodically a web page. My idea is to set a local notification periodically, for example every 20 minutes. When notification leaves, device should load the web page, check a condition and if the condition is true, device should rang, otherwise nothing.

(NOTIFICATION) -> (LOAD WEB PAGE) -> [VERIFY CONDITION]-|if true|-> (RING)

Is this technically possible to do? How can I load a web page while app isn't running?

My sketch of code was like this:

func check () {

    pageCode = // find a way to load the page

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_MSEC * 100))
    dispatch_after(delayTime, dispatch_get_main_queue()){       

        let readCode = self.pageCode.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].outerHTML")

        if letturaCodice?.containsString("Some text") == true {
            ringPhone()
        }
    }
}
Matte.Car
  • 2,007
  • 4
  • 23
  • 41

3 Answers3

0

Not possible using UILocalNotification, you could do it with a remote notification though.

Apple has a guide here: See - "Using Push Notifications to Initiate a Download" https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

A remote notification platform I've used before is called OneSignal, it's completely free and allows you to schedule notifications. https://onesignal.com

Edit: Doesn't answer question about ringing, not sure about that bit sorry!

Ollie
  • 1,926
  • 1
  • 20
  • 35
0

Option 1:

You could build the app to use background app refresh function that you could cause an alert if lets say you do not get an http status code of 200. If you use this option you could build in the function to have a custom tone to play that you build into the app.

Option 2:

Use a server side script todo the checking and have it fire off a push notification. This method would depend on phone settings as to how the notification would function.

MwcsMac
  • 6,810
  • 5
  • 32
  • 52
  • It's an app just for me so I'm searching a very easy method or nothing, no server implementation or similar. Option 1 could be interesting but I'm not the manager of the web page so I can't modify it, I just can check it periodically and control if it's displaying a determined element or not, if displayed the device should rang, but there's no interaction with http status... – Matte.Car Feb 16 '16 at 16:28
  • Ok so background method you can do what you need. Sounds like you maybe looking at do some http parsing then to get what you are looking for. [See my Stack question on html parsing](http://stackoverflow.com/questions/30246349/swift-parse-html-table) – MwcsMac Feb 16 '16 at 16:44
  • See this [Background Post](http://stackoverflow.com/questions/35435161/send-local-notification-after-termination-app-swift-2) it might head off another question with the background task portion. – MwcsMac Feb 16 '16 at 17:10
  • The code is outdated now because the project I was using was modified. HTTPTask would now be "Kanna.HTML". See in [Kana XML/HTML Parse](https://github.com/tid-kijyun/Kanna) Give me an example of what you might be looking for in the html and I can update my answer with some code. – MwcsMac Feb 16 '16 at 19:09
0

You can try to use Grand Central Dispatch to run a background process and inside it add notification observer.

UPD: Like this:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
    () -> Void in
    // catch notifications here
}
pomo_mondreganto
  • 2,028
  • 2
  • 28
  • 56