
import SwiftUI
import WhatsNewKit
struct ContentView : View {
var body : some View {
NavigationView {
// ...
}
. whatsNewSheet ( )
}
} Untuk mengintegrasikan menggunakan Paket Swift Apple, tambahkan yang berikut ini sebagai ketergantungan ke Package.swift Anda.
dependencies: [
. package ( url : " https://github.com/SvenTiigi/WhatsNewKit.git " , from : " 2.0.0 " )
] Atau arahkan ke proyek XCODE Anda, lalu pilih Swift Packages , klik ikon "+" dan cari WhatsNewKit .
Lihatlah aplikasi contoh untuk melihat WhatsNewKit beraksi. Cukup buka Example/Example.xcodeproj dan jalankan skema "contoh".

Jika Anda ingin memberikan WhatsNewView secara manual, Anda dapat menggunakan sheet(whatsNew:) pengubah.
struct ContentView : View {
@ State
var whatsNew : WhatsNew ? = WhatsNew (
title : " WhatsNewKit " ,
features : [
. init (
image : . init (
systemName : " star.fill " ,
foregroundColor : . orange
) ,
title : " Showcase your new App Features " ,
subtitle : " Present your new app features... "
) ,
// ...
]
)
var body : some View {
NavigationView {
// ...
}
. sheet (
whatsNew : self . $whatsNew
)
}
} Mode presentasi otomatis memungkinkan Anda untuk hanya mendeklarasikan fitur baru Anda melalui lingkungan SwiftUi dan WhatsNewKit akan berhati -hati untuk menyajikan WhatsNewView yang sesuai.
Pertama tambahkan pengubah .whatsNewSheet() ke tampilan di mana WhatsNewView harus disajikan.
struct ContentView : View {
var body : some View {
NavigationView {
// ...
}
// Automatically present a WhatsNewView, if needed.
// The WhatsNew that should be presented to the user
// is automatically retrieved from the `WhatsNewEnvironment`
. whatsNewSheet ( )
}
} Modifier .whatsNewSheet() memanfaatkan lingkungan WhatsNewEnvironment untuk mengambil objek WhatsNew opsional yang harus disajikan kepada pengguna untuk versi saat ini. Oleh karena itu Anda dapat dengan mudah mengonfigurasi lingkungan WhatsNewEnvironment melalui pengubah environment .
extension App : SwiftUI . App {
var body : some Scene {
WindowGroup {
ContentView ( )
. environment (
. whatsNew ,
WhatsNewEnvironment (
// Specify in which way the presented WhatsNew Versions are stored.
// In default the `UserDefaultsWhatsNewVersionStore` is used.
versionStore : UserDefaultsWhatsNewVersionStore ( ) ,
// Pass a `WhatsNewCollectionProvider` or an array of WhatsNew instances
whatsNewCollection : self
)
)
}
}
}
// MARK: - App+WhatsNewCollectionProvider
extension App : WhatsNewCollectionProvider {
/// Declare your WhatsNew instances per version
var whatsNewCollection : WhatsNewCollection {
WhatsNew (
version : " 1.0.0 " ,
// ...
)
WhatsNew (
version : " 1.1.0 " ,
// ...
)
WhatsNew (
version : " 1.2.0 " ,
// ...
)
}
} WhatsNewEnvironment akan berhati -hati untuk menentukan objek WhatsNew yang cocok yang harus disajikan kepada pengguna untuk versi saat ini.
Seperti yang terlihat pada contoh sebelumnya, Anda dapat menginisialisasi WhatsNewEnvironment dengan menentukan WhatsNewVersionStore dan memberikan WhatsNewCollection .
// Initialize WhatsNewEnvironment by passing an array of WhatsNew Instances.
// UserDefaultsWhatsNewVersionStore is used as default WhatsNewVersionStore
let whatsNewEnvironment = WhatsNewEnvironment (
whatsNewCollection : [
WhatsNew (
version : " 1.0.0 " ,
// ...
)
]
)
// Initialize WhatsNewEnvironment with NSUbiquitousKeyValueWhatsNewVersionStore
// which stores the presented versions in iCloud.
// WhatsNewCollection is provided by a `WhatsNewBuilder` closure
let whatsNewEnvironment = WhatsNewEnvironment (
versionStore : NSUbiquitousKeyValueWhatsNewVersionStore ( ) ,
whatsNewCollection : {
WhatsNew (
version : " 1.0.0 " ,
// ...
)
}
) Selain itu, WhatsNewEnvironment termasuk fallback untuk versi patch. Misalnya ketika pengguna menginstal versi 1.0.1 dan Anda hanya telah mendeklarasikan WhatsNew untuk versi 1.0.0 Lingkungan akan secara otomatis mundur ke versi 1.0.0 dan menyajikan WhatsNewView kepada pengguna jika diperlukan.
Jika Anda ingin lebih menyesuaikan perilaku lingkungan WhatsNewEnvironment Anda dapat dengan mudah mensubskan dan mengganti fungsi whatsNew() .
class MyCustomWhatsNewEnvironment : WhatsNewEnvironment {
/// Retrieve a WhatsNew that should be presented to the user, if available.
override func whatsNew ( ) -> WhatsNew ? {
// The current version
let currentVersion = self . currentVersion
// Your declared WhatsNew objects
let whatsNewCollection = self . whatsNewCollection
// The WhatsNewVersionStore used to determine the already presented versions
let versionStore = self . whatsNewVersionStore
// TODO: Determine WhatsNew that should be presented to the user...
}
} WhatsNewVersionStore adalah tipe protokol yang bertanggung jawab untuk menyimpan dan mengambil versi yang telah disajikan kepada pengguna.
let whatsNewVersionStore : WhatsNewVersionStore
// Save presented versions
whatsNewVersionStore . save ( presentedVersion : " 1.0.0 " )
// Retrieve presented versions
let presentedVersions = whatsNewVersionStore . presentedVersions
// Retrieve bool value if a given version has already been presented
let hasPresented = whatsNewVersionStore . hasPresented ( " 1.0.0 " )WhatsNewKit hadir dengan tiga implementasi yang telah ditentukan:
// Persists presented versions in the UserDefaults
let userDefaultsWhatsNewVersionStore = UserDefaultsWhatsNewVersionStore ( )
// Persists presented versions in iCloud using the NSUbiquitousKeyValueStore
let ubiquitousKeyValueWhatsNewVersionStore = NSUbiquitousKeyValueWhatsNewVersionStore ( )
// Stores presented versions in memory. Perfect for testing purposes
let inMemoryWhatsNewVersionStore = InMemoryWhatsNewVersionStore ( ) Jika Anda sudah memiliki implementasi khusus untuk menyimpan pengaturan terkait pengguna seperti data ranah atau inti, Anda dapat dengan mudah mengadopsi implementasi yang ada ke protokol WhatsNewVersionStore .
Jika Anda memanfaatkan NSUbiquitousKeyValueWhatsNewVersionStore , harap pastikan untuk mengaktifkan kemampuan penyimpanan nilai kunci iCloud di bagian "Penandatanganan & Kemampuan" dari proyek XCODE Anda.

Bagian berikut menjelaskan bagaimana struct WhatsNew dapat diinisialisasi untuk menggambarkan fitur -fitur baru untuk versi aplikasi Anda.
let whatsnew = WhatsNew (
// The Version that relates to the features you want to showcase
version : " 1.0.0 " ,
// The title that is shown at the top
title : " What's New " ,
// The features you want to showcase
features : [
WhatsNew . Feature (
image : . init ( systemName : " star.fill " ) ,
title : " Title " ,
subtitle : " Subtitle "
)
] ,
// The primary action that is used to dismiss the WhatsNewView
primaryAction : WhatsNew . PrimaryAction (
title : " Continue " ,
backgroundColor : . accentColor ,
foregroundColor : . white ,
hapticFeedback : . notification ( . success ) ,
onDismiss : {
print ( " WhatsNewView has been dismissed " )
}
) ,
// The optional secondary action that is displayed above the primary action
secondaryAction : WhatsNew . SecondaryAction (
title : " Learn more " ,
foregroundColor : . accentColor ,
hapticFeedback : . selection ,
action : . openURL (
. init ( string : " https://github.com/SvenTiigi/WhatsNewKit " )
)
)
) WhatsNew.Version menentukan versi yang telah memperkenalkan fitur tertentu ke aplikasi Anda.
// Initialize with major, minor, and patch
let version = WhatsNew . Version (
major : 1 ,
minor : 0 ,
patch : 0
)
// Initialize by string literal
let version : WhatsNew . Version = " 1.0.0 "
// Initialize WhatsNew Version by using the current version of your bundle
let version : WhatsNew . Version = . current ( ) A WhatsNew.Title mewakili teks judul yang diberikan di atas fitur.
// Initialize by string literal
let title : WhatsNew . Title = " Continue "
// Initialize with text and foreground color
let title = WhatsNew . Title (
text : " Continue " ,
foregroundColor : . primary
)
// On >= iOS 15 initialize with AttributedString using Markdown
let title = WhatsNew . Title (
text : try AttributedString (
markdown : " What's **New** "
)
) A WhatsNew.Feature Jelaskan fitur spesifik dari aplikasi Anda dan umumnya terdiri dari gambar, judul, dan subtitle.
let feature = WhatsNew . Feature (
image : . init (
systemName : " wand.and.stars "
) ,
title : " New Design " ,
subtitle : . init (
try AttributedString (
markdown : " An awesome new _Design_ "
)
)
) WhatsNew.PrimaryAction memungkinkan Anda untuk mengonfigurasi perilaku tombol primer yang digunakan untuk mengabaikan WhatsNewView yang disajikan
let primaryAction = WhatsNew . PrimaryAction (
title : " Continue " ,
backgroundColor : . blue ,
foregroundColor : . white ,
hapticFeedback : . notification ( . success ) ,
onDismiss : {
print ( " WhatsNewView has been dismissed " )
}
)Catatan: HapticFeedback hanya akan dieksekusi di iOS
WhatsNew.SecondaryAction yang ditampilkan di atas WhatsNew.PrimaryAction dapat disediakan secara opsional saat menginisialisasi instance WhatsNew dan memungkinkan Anda untuk menyajikan tampilan tambahan, melakukan tindakan khusus atau membuka URL.
// SecondaryAction that presents a View
let secondaryActionPresentAboutView = WhatsNew . SecondaryAction (
title : " Learn more " ,
foregroundColor : . blue ,
hapticFeedback : . selection ,
action : . present {
AboutView ( )
}
)
// SecondaryAction that opens a URL
let secondaryActionOpenURL = WhatsNew . SecondaryAction (
title : " Read more " ,
foregroundColor : . blue ,
hapticFeedback : . selection ,
action : . open (
url : . init ( string : " https://github.com/SvenTiigi/WhatsNewKit " )
)
)
// SecondaryAction with custom execution
let secondaryActionCustom = WhatsNew . SecondaryAction (
title : " Custom " ,
action : . custom { presentationMode in
// ...
}
)Catatan: HapticFeedback hanya akan dieksekusi di iOS
WhatsNewKit memungkinkan Anda untuk menyesuaikan tata letak WhatsNewView yang disajikan dengan berbagai cara.
Cara yang paling sederhana adalah dengan bermutasi WhatsNew.Layout.default instance.
WhatsNew . Layout . default . featureListSpacing = 35Saat menggunakan gaya presentasi otomatis, Anda dapat menyediakan tata letak default saat menginisialisasi lingkungan WhatsNewen.
. environment (
. whatsNew ,
. init (
defaultLayout : WhatsNew . Layout (
showsScrollViewIndicators : true ,
featureListSpacing : 35
) ,
whatsNew : self
)
) Atau Anda dapat melewati WhatsNew.Layout .
. whatsNewSheet (
layout : WhatsNew . Layout (
contentPadding : . init (
top : 80 ,
leading : 0 ,
bottom : 0 ,
trailing : 0
)
)
) . sheet (
whatsNew : self . $whatsNew ,
layout : WhatsNew . Layout (
footerActionSpacing : 20
)
) Saat menggunakan UIKit atau AppKit , Anda dapat menggunakan WhatsNewViewController .
let whatsNewViewController = WhatsNewViewController (
whatsNew : WhatsNew (
version : " 1.0.0 " ,
// ...
) ,
layout : WhatsNew . Layout (
contentSpacing : 80
)
) Jika Anda ingin menyajikan WhatsNewViewController hanya jika versi instance WhatsNew belum disajikan, Anda dapat menggunakan inisialisasi yang dapat dikerjakan dengan kenyamanan.
// Verify WhatsNewViewController is available for presentation
guard let whatsNewViewController = WhatsNewViewController (
whatsNew : WhatsNew (
version : " 1.0.0 " ,
// ...
) ,
versionStore : UserDefaultsWhatsNewVersionStore ( )
) else {
// Version of WhatsNew has already been presented
return
}
// Present WhatsNewViewController
// Version will be automatically saved in the provided
// WhatsNewVersionStore when the WhatsNewViewController gets dismissed
self . present ( whatsNewViewController , animated : true )