简体   繁体   English

当应用程序在后台无法在物理设备上运行时执行代码(SWIFT)

[英]Executing code, when the application is in background not working on physical device (SWIFT)

I am making an app, that is reliant on executing some minor code in the background and I ran into a peculiar problem. 我正在制作一个应用程序,它依赖于在后台执行一些次要代码,但遇到了一个特殊的问题。
The app has a timer, an NSTimer() . 该应用程序有一个计时器NSTimer() The principle behind all of this is similiar to that of timer (in Clock application, that is installed on all iOS devices), meaning, when the timer ends a UILocalNotification is displayed. 所有这些背后的原理与计时器(在所有iOS设备上安装的Clock应用程序中)相似,也就是说,当计时器结束时会显示UILocalNotification

Everything works as expected when I run it on an emulated device in Xcode, but when I test it on my iPhone, there are no notifications. 当我在Xcode的模拟设备上运行它时,一切都按预期运行,但是在iPhone上测试时,没有任何通知。 It is something along the lines of: 它遵循以下原则:

var end = timerHasEnded()
if end == true{
    println("the timer has ended")
}

and it was not working. 而且它没有用。 So I checked if the application even detects the background UIApplicationState by doing this and it does not on the physical device. 因此,我检查了应用程序是否通过执行此操作甚至检测到后台UIApplicationState ,并且它不在物理设备上。 Interestingly enough, it does so on an emulated device. 有趣的是,它是在模拟设备上执行的。

I tried running the timer on background thread, using QOS_CLASS_BACKGROUND and dispatch_async to no avail. 我尝试在后台线程上运行计时器,使用QOS_CLASS_BACKGROUNDdispatch_async无济于事。 Can anybody help me? 有谁能够帮助我?

Could you just schedule a UILocalNotification to appear after a delay, when the delay was the remaining time left in your alarm? 当延迟是警报中剩余的时间时,您是否可以安排UILocalNotification在延迟后出现?

var date: NSDate = /*time until your timer expires*/
let app = UIApplication.sharedApplication()
let oldNotifications = app.scheduledLocalNotifications
if oldNotifications.count > 0 {
    app.cancelAllLocalNotifications()
}

let alarm = UILocalNotification()
alarm.fireDate = date
alarm.timeZone = NSTimeZone.defaultTimeZone()
alarm.repeatInterval = 0
alarm.soundName = "myAlarmSound.caf"
alarm.alertBody = "Do something!"

app.scheduleLocalNotification(alarm)

I wrote this code from an Obj-C example provided by Apple. 我是从Apple提供的Obj-C示例中编写此代码的。

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序在后台或设备被锁定时运行代码 - Running the code when application is in the background or device is locked 启用物理设备块(Swift)时如何拦截 ios 旋转? - How intercept ios rotation when physical device block is enabled (Swift)? 在物理设备上测试我的应用程序(使用Swift)时崩溃:线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x120021088) - Crashed When testing my app (using swift) on a physical device, Thread 1: EXC_BREAKPOINT (code=1, subcode=0x120021088) Swift / SwiftUI - 在设备锁定/置于后台之前执行函数? - Swift / SwiftUI - Executing a function before the device locks / is put in the background? AVPlayer HLS在物理设备上不起作用 - AVPlayer HLS not working on physical device iOS swift 应用程序从 firestore 读取数据,数据显示在模拟器上但不在物理设备中 - iOS swift application read data from firestore, data shown on simulator but not in physical device 应用程序在后台时更改设备语言 - Changing the device language when application is in background 在 Swift 中,如何确定设备屏幕的物理尺寸? - In Swift, how to determine the physical size of a device screen? 使 NSTimer 在物理设备的后台运行 - Making NSTimer run in the background on a physical device Swift - 检测应用程序何时发送到后台但不是设备被锁定时 - Swift - Detecting when app is sent to background but not when device is locked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM