iOS 如何设置闹钟和安排工作/通知

A.s*_*ALI 5 alarm ios swift

嗨,我是 iOS 新手,基本上我是 android 开发人员。但是现在我正在开发 iOS 应用程序,它只是 android 的 iOS 副本。让我告诉你我想要的应用程序:

我们的应用程序具有警报功能,会提醒我们的客户在特定日期您有此会议。例如,如果用户在 2019 年 1 月 1 日上午 9 点设置闹钟,则必须在当天和时间通知用户此会议。

我读了很多,发现在 iOS 中我们不能这样做,因为当应用程序在后台时它不能运行他自己的代码?所以我有两个基本问题:

我想要的是:

  1. 首先如何安排闹钟

  2. 如果设置了警报并且应用程序在后台/终止,那么如何生成通知以及当用户单击通知时将他带到特定视图?

  3. 如果应用程序在前台,那么如何将他带到想要的视图?此外,如果应用程序在特定视图上,如何在闹钟响起时更新视图本身?

我知道这些是需要太多编码的 3 个主要和主要部分。但我只想要方向。给我代码块的链接。我正在使用 xcode 9.2 和 swift 4.0。提前致谢 ...

Nat*_*jan 5

您必须安排本地通知,现在可以在UNUserNotificationCenter.

所以,

  1. 要从您的应用程序本地安排通知,请遵循此文档
  2. 对于处理通知和通知相关操作,请遵循此文档

要在您AppDelegate或您要处理UNUserNotificationCenter委托方法的地方处理通知,请添加以下代码:

class AppDelegate:NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate{

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {


    let center = UNUserNotificationCenter.current()
    center.delegate = self // Don't forgot to set delegate

    //To get permissions from user:
    let options: UNAuthorizationOptions = [.alert, .sound, .badge];
    center.requestAuthorization(options: options) {
        (granted, error) in
        if !granted {
            print("Something went wrong")
        }
    }

    return true
}
}
Run Code Online (Sandbox Code Playgroud)