hsc*_*ler 7 xcode ios swift watchos swiftui
我是 SwiftUI 开发新手,所以我学习了官方 SwiftUI-Landmark 教程。创建 watchOS 应用程序一章提到,在添加 WatchOSApp 目标时,应选中模板中的“包含通知场景”复选框。然而,在我的 Xcode V 14.2 中没有这样的复选框。难道我做错了什么?以后有可能添加这个场景吗?
我还在苹果开发者论坛中发布了这个问题。但直到现在还没有人回答。
非常感谢大家的回答。地标教程现在也已更新,不再需要“包含通知场景”复选框。
事实上,Xcode 14+ 中缺少该复选框(截至 2023 年 1 月)。在 Xcode 13 中,当您选择该复选框时,它会创建其他文件,这些文件是:NotificationView.swift、NotificationController.swift、ComplicationController.swift、PushNotificationPayload.apns,以及用于启动通知和复杂功能的两个方案。
幸运的是,完成本教程不需要复杂的操作,因此您只需创建 3 个文件和一个方案,然后再继续第5 节 \xe2\x80\x94\n创建自定义通知界面。
\n我在博客文章中提供了带有屏幕截图的详细说明,但这里是您必须执行的操作的简短描述:
\n在WatchLandmarks Watch App文件夹中创建一个新的 SwiftUI View 文件。命名它NotificationView.swift:
import SwiftUI\n\nstruct NotificationView: View {\n var body: some View {\n Text("Hello, World!")\n }\n}\n\nstruct NotificationView_Previews: PreviewProvider {\n static var previews: some View {\n NotificationView()\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n不要对其存根内容感到困惑。该文件将在 Apple 教程的下一部分中进行修改。现在,您只需要这些文件处于在 Xcode 13 中创建的状态。
\nNotificationController.swift在同一文件夹中创建一个名为的 Swift 文件:
import WatchKit\nimport SwiftUI\nimport UserNotifications\n\nclass NotificationController: WKUserNotificationHostingController<NotificationView> {\n\n override var body: NotificationView {\n return NotificationView()\n }\n\n override func willActivate() {\n // This method is called when watch view controller is about to be visible to user\n super.willActivate()\n }\n\n override func didDeactivate() {\n // This method is called when watch view controller is no longer visible\n super.didDeactivate()\n }\n\n override func didReceive(_ notification: UNNotification) {\n // This method is called when a notification needs to be presented.\n // Implement it if you use a dynamic notification interface.\n // Populate your dynamic notification interface as quickly as possible.\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n在同一文件夹中,创建一个空文件(新文件屏幕的底部)并将其命名为PushNotificationPayload.apns. 粘贴其内容:
{\n "aps": {\n "alert": {\n "body": "Test message",\n "title": "Optional title",\n "subtitle": "Optional subtitle"\n },\n "category": "myCategory",\n "thread-id": "5280"\n },\n \n "WatchKit Simulator Actions": [\n {\n "title": "First Button",\n "identifier": "firstButtonAction"\n }\n ],\n \n "customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you\'ll be able to select them when choosing to debug the notification interface of your Watch App."\n}\nRun Code Online (Sandbox Code Playgroud)\n现在您需要创建一个方案来使用刚刚创建的自定义视图运行通知。
\n (Notification)到方案名称中以便清楚起见。单击“确定”。在此状态下,您可以轻松地继续本教程。
\n| 归档时间: |
|
| 查看次数: |
597 次 |
| 最近记录: |