-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmoEaseApp.swift
More file actions
109 lines (96 loc) · 3.73 KB
/
Copy pathEmoEaseApp.swift
File metadata and controls
109 lines (96 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// EmoEaseApp.swift
// EmoEase
//
// Created by Randy on 16/12/2024.
//
import SwiftUI
@main
struct EmoEaseApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
TabView {
HomeView()
.tabItem {
Image(systemName: "heart.fill")
Text("记录")
}
DiaryView()
.tabItem {
Image(systemName: "book.fill")
Text("日记")
}
ReviewView()
.tabItem {
Image(systemName: "chart.bar.fill")
Text("回顾")
}
// 添加新的页面
ExploreView()
.tabItem {
Image(systemName: "magnifyingglass")
Text("探索")
}
}
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// 设置通知代理
UNUserNotificationCenter.current().delegate = self
// 请求通知权限
NotificationManager.shared.requestAuthorization()
return true
}
// 处理 URL Scheme
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
// 处理从 Widget 跳转
if url.scheme == "emoease" {
switch url.host {
case "morning":
// 发送通知以打开早安日记
NotificationCenter.default.post(name: NSNotification.Name("OpenMorningDiary"), object: nil)
return true
case "emotion":
// 发送通知以打开情绪记录
NotificationCenter.default.post(name: NSNotification.Name("OpenEmotionRecord"), object: nil)
return true
case "night":
// 发送通知以打开晚安日记
NotificationCenter.default.post(name: NSNotification.Name("OpenNightDiary"), object: nil)
return true
default:
return false
}
}
return false
}
// 当应用在前台时收到通知
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// 在前台也显示通知
completionHandler([.banner, .sound])
}
// 处理通知点击事件
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// 根据通知类型执行相应操作
switch response.notification.request.identifier {
case "morningDiary":
NotificationCenter.default.post(name: NSNotification.Name("OpenMorningDiary"), object: nil)
case "emotionRecord":
NotificationCenter.default.post(name: NSNotification.Name("OpenEmotionRecord"), object: nil)
case "nightDiary":
NotificationCenter.default.post(name: NSNotification.Name("OpenNightDiary"), object: nil)
default:
break
}
completionHandler()
}
}
// 新的探索视图
struct Themediary: View {
var body: some View {
Text("探索页面")
}
}