我需要定期检查网页。 我的想法是定期设置本地通知,例如每 20 分钟一次。当通知离开时,设备应该加载网页,检查条件,如果条件为真,设备应该响铃,否则什么都不响。
(NOTIFICATION) -> (LOAD WEB PAGE) -> [VERIFY CONDITION]-|if true|-> (RING)
这在技术上可行吗?如何在应用未运行时加载网页?
我的代码草图是这样的:
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()
}
}
}
最佳答案
使用 UILocalNotification
是不可能的,不过您可以使用远程通知来完成。
Apple 在这里有一个指南:请参阅 - “使用推送通知启动下载” https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
我以前用过的一个远程通知平台叫做 OneSignal,它是完全免费的,可以让你安排通知。 https://onesignal.com
编辑: 不回答有关响铃的问题,对此不太确定抱歉!
关于swift - 在后台定期检查网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35436812/