Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中安排任务?_Ios_Swift_Avspeechsynthesizer - Fatal编程技术网

Ios 如何在swift中安排任务?

Ios 如何在swift中安排任务?,ios,swift,avspeechsynthesizer,Ios,Swift,Avspeechsynthesizer,我正试图设置闹钟,但我想设置闹钟,而不是播放音乐 应用程序将读取我创建的待办事项列表 我使用AVSpeechSynthesizer类来执行演讲功能,它工作正常,但问题是,我使用计时器来安排任务,当应用程序进入后台时,计时器停止工作 这是我的密码: class Speaking : NSObject,AVSpeechSynthesizerDelegate { let speechSynthesizerObject = AVSpeechSynthesizer() let voice = AVSpe

我正试图设置闹钟,但我想设置闹钟,而不是播放音乐 应用程序将读取我创建的待办事项列表

我使用AVSpeechSynthesizer类来执行演讲功能,它工作正常,但问题是,我使用计时器来安排任务,当应用程序进入后台时,计时器停止工作

这是我的密码:

class Speaking : NSObject,AVSpeechSynthesizerDelegate {

let speechSynthesizerObject = AVSpeechSynthesizer()
let voice = AVSpeechSynthesisVoice(language: "en-au")

override init() {
    super.init()
    speechSynthesizerObject.delegate = self
}

func speak(string: String) {
    let utterance = AVSpeechUtterance(string: string)
    utterance.rate = 0.5
    utterance.voice = self.voice
    speechSynthesizerObject.speak(utterance)
}

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
    print("all done")

}    
}
报警功能

func setAlaram(){

    var date = self.appData.Alaram

    if(date < Date()){
        date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
    }

    let calendar = Calendar(identifier: .gregorian)

    var component = calendar.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
    component.second = 0
    var modifiedDateTime = calendar.date(from: component)!

   var snooz = Timer(fireAt: modifiedDateTime, interval: 0, target: self, selector: #selector(setSpeechRecognizeAsAlaram), userInfo: "first", repeats: false)
    RunLoop.main.add(snooz!, forMode: RunLoopMode.commonModes)
 }
任何帮助都将不胜感激

谢谢

你读过苹果公司关于如何在后台运行任务的说明吗

有一个章节是关于在后台引起用户注意的。有一个使用本地通知设置报警的示例

来自苹果网站


清单3-2调度报警通知

- (void)scheduleAlarmForDate:(NSDate*)theDate
{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray*    oldNotifications = [app scheduledLocalNotifications];

    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"alarmsound.caf";
        alarm.alertBody = @"Time to wake up!";

        [app scheduleLocalNotification:alarm];
    }
}


更新


当然,
UILocalNotification
也不推荐使用。新的替换是。

当您的应用程序挂起时,您不能在计划的时间执行任意代码。您要查找的是UILocalNotification(),但我不确定您是否可以从There is tere运行AVSpeechSynthesizer?是否有其他方法可以执行此操作。ULNotification将不允许执行此功能我希望AVSpeechSynthesizer说出脚本,但如何使用UNNotification?。我不认为我的应用程序属于苹果允许在后台执行的任何类别。有黑客吗?我会想办法在应用程序处于前台时将声音写入文件。然后在后台播放声音文件。
- (void)scheduleAlarmForDate:(NSDate*)theDate
{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray*    oldNotifications = [app scheduledLocalNotifications];

    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"alarmsound.caf";
        alarm.alertBody = @"Time to wake up!";

        [app scheduleLocalNotification:alarm];
    }
}