概述
- Fruta:Building a Feature-Rich App with SwiftUI(創建一個共享代碼庫來構建一個提供小部件和App Clip的多平臺應用程序)
*舉例說明用途
[Fruta示例應用程序]為macOS、iOS和iPadOS構建了一個應用程序:
1.實現了swift平臺的小部件或應用程序剪輯等特性。
2.用戶可以點冰沙,保存喜歡的飲料,收集獎勵,瀏覽食譜。
特點:
1.示例應用程序的Xcode項目包括小部件擴展,用戶可以在iOS主屏幕或macOS通知中心添加小部件,查看自己的獎勵或喜歡的奶昔
2.Xcode項目還包括一個App Clip目標。有了App Clip,用戶無需安裝完整的應用程序,就可以在他們的iPhone或iPad上發現并立即啟動應用程序的一些功能
[Fruta示例應用程序]利用了Apple和PassKit (Apple Pay和Wallet)的登錄功能,提供了一種流線化的用戶體驗,并通過將共享代碼和本地化資產打包成Swift包來促進代碼重用。*
配置示例代碼項目
To build this project for iOS 14.2 beta 3, use Xcode 12.2 beta 3\. The runtime requirement is iOS 14.2 or later. To build this project for macOS 11 Big Sur beta 10, use Xcode 12.2 beta 3.
1. To run on your devices, including on macOS, set your team in the targets’ Signing & Capabilities panes. Xcode manages the provisioning profiles for you.
2. To run on an iOS or iPadOS device, open the iOSClip.entitlements file and update the value of the [`Parent Application Identifiers Entitlement`](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_parent-application-identifiers) to match the iOS app’s bundle identifier.
3. To enable the in-app-purchase flow, edit the Fruta iOS “Run” scheme, and select `Configuration.storekit` for StoreKit Configuration.
4. Make a note of the App Group name suffix on the iOS target’s Signing and Capabilities tab in Project Settings. Append this value to the App Group name string in line 27 of `Mode.swift` so it’s identical to that on the Signing and Capabilities tab.
5. The Xcode project includes playgrounds that are configured to run on iOS. To change a playground’s platform, select it in the Project navigator, open the File inspector, and select the desired platform. Next, select the scheme that matches the platform before you build and run the playground.
要為ios14.2 beta 3構建這個項目,使用Xcode 12.2 beta 3。運行時要求是iOS 14.2或更高版本。要為macOS 11 Big Sur beta 10構建這個項目,使用Xcode 12.2 beta 3。
要在您的設備(包括macOS)上運行,請在目標的簽名與能力窗格中設置您的團隊。Xcode為您管理配置文件。
要在iOS或iPadOS設備上運行,請打開iOSClip。權限文件,并更新父應用程序標識符權限的值,以匹配iOS應用程序的bundle標識符。
要啟用in-app-purchase流程,編輯Fruta iOS“運行”方案,并選擇Configuration。用于storekit配置的storekit。
在項目設置中,在iOS目標的簽名和功能選項卡上記下應用程序組名的后綴。將這個值附加到FrutaModel.swift的第27行中的App組名字符串,這樣它就和簽名和功能標簽上的相同了。Xcode項目包括可以在iOS上運行的playgrounds。要更改playgrounds的平臺,請在項目導航器中選擇它,打開文件檢查器,并選擇所需的平臺。接下來,在創建和運行操場之前,選擇與平臺匹配的方案。
使用SwiftUI創建一個共享代碼庫
為了創建一個適用于多個平臺的應用程序定義,該項目定義了一個符合app協議的結構。因為@main屬性在結構定義之前,系統識別該結構作為進入應用程序的入口點。它的計算body屬性返回一個窗口組場景,其中包含應用程序向用戶顯示的視圖層次結構。SwiftUI以適合平臺的方式管理場景及其內容的展示。
@main
struct FrutaApp: App {
@StateObject private var model = FrutaModel()
@StateObject private var store = Store()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
.environmentObject(store)
}
.commands {
SidebarCommands()
}
}
}
提供一個App Clip
在iOS和iPadOS上,[Fruta應用程序]為那些沒有安裝完整應用程序的用戶提供了一些功能。該應用的Xcode項目包含一個App Clip,并重用跨所有平臺共享的代碼來構建應用剪輯,而不是復制代碼。
在共享代碼中,項目使用活動編譯條件構建設置排除未定義APPCLIP值的目標代碼。
例如,只有App Clip目標顯示一個App Store覆蓋層來提示用戶獲取完整的應用。
VStack(spacing: 0) {
Spacer()
orderStatusCard
Spacer()
if presentingBottomBanner {
bottomBanner
}
#if APPCLIP
Text("App Store Overlay")
.hidden()
.appStoreOverlay(isPresented: $presentingAppStoreOverlay) {
SKOverlay.AppClipConfiguration(position: .bottom)
}
#endif
}
.onChange(of: model.hasAccount) { _ in
#if APPCLIP
if model.hasAccount {
presentingAppStoreOverlay = true
}
#endif
}
更多信息,可以看
SwiftUI 學習 Creating an App Clip with Xcode
Swift 學習 Choosing the Right Functionality for Your App Clip
創建一個小部件
為了讓用戶在iOS主屏幕或macOS通知中心看到應用程序的小部件,
Xcode項目包含了小部件擴展的目標。兩者都使用在所有目標之間共享的代碼。
有關更多信息,請參見WidgetKit。
This sample project is associated with WWDC 2020 sessions
10637: Platforms State of the Union,
10146: Configure and Link Your App Clips,
10120: Streamline Your App Clip,
10118: Create App Clips for Other Businesses,
10096: Explore Packages and Projects with Xcode Playgrounds,
10028: Meet WidgetKit.