
import SwiftUI
import WhatsNewKit
struct ContentView : View {
var body : some View {
NavigationView {
// ...
}
. whatsNewSheet ( )
}
} AppleのSwiftパッケージマネージャーを使用して統合するには、 Package.swiftへの依存関係として以下を追加します。Swift:
dependencies: [
. package ( url : " https://github.com/SvenTiigi/WhatsNewKit.git " , from : " 2.0.0 " )
]または、Xcodeプロジェクトに移動してから、 Swift Packagesを選択し、「+」アイコンをクリックしてWhatsNewKitを検索します。
whatsnewkitの動作を確認するには、アプリケーションのサンプルをご覧ください。 Example/Example.xcodeprojを開き、「例」スキームを実行するだけです。

WhatsNewView手動で提示したい場合はsheet(whatsNew:)モディファイアを使用できます。
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
)
}
}自動プレゼンテーションモードを使用すると、SwiftUI環境を介して新機能を単純に宣言でき、WhatsNewKitは対応するWhatsNewView提示するように注意します。
最初に、 WhatsNewViewを提示する場所を表示するビューに.whatsNewSheet()モディファイを追加します。
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 ( )
}
} .whatsNewSheet() Modifierは、 WhatsNewEnvironmentを利用して、現在のバージョンのユーザーに提示するオプションのWhatsNewオブジェクトを取得しています。したがって、 environment修飾子を介してWhatsNewEnvironment簡単に構成できます。
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 、現在のバージョンのユーザーに提示する必要がある一致するWhatsNewオブジェクトを決定するように注意します。
前の例に見られるように、 WhatsNewVersionStore指定し、 WhatsNewCollectionを提供することにより、 WhatsNewEnvironmentを初期化できます。
// 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 " ,
// ...
)
}
)さらに、 WhatsNewEnvironmentには、パッチバージョン用のフォールバックが含まれています。たとえば、ユーザーがバージョン1.0.1をインストールし、バージョン1.0.0のWhatsNewのみを宣言した場合、環境はバージョン1.0.0に自動的にフォールバックし、必要に応じてWhatsNewViewをユーザーに提示します。
WhatsNewEnvironmentの動作をさらにカスタマイズしたい場合は、簡単にサブクラス化して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は、ユーザーに提示されたバージョンの保存と取得を担当するプロトコルタイプです。
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には、3つの事前定義された実装が付属しています。
// 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 ( )領域やコアデータなどのユーザー関連設定を保存するための特定の実装が既にある場合は、既存の実装をWhatsNewVersionStoreプロトコルに簡単に採用できます。
NSUbiquitousKeyValueWhatsNewVersionStoreを使用している場合は、Xcodeプロジェクトの「署名と機能」セクションでiCloudキー価値ストレージ機能を有効にしてください。

次のセクションでは、アプリの特定のバージョンの新しい機能を説明するために、 WhatsNew structを初期化する方法について説明します。
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 、特定の機能をアプリに導入したバージョンを指定します。
// 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 ( ) WhatsNew.Titleは、機能の上にレンダリングされるタイトルテキストを表します。
// 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** "
)
) WhatsNew.Featureは、アプリの特定の機能を説明し、一般的に画像、タイトル、および字幕で構成されています。
let feature = WhatsNew . Feature (
image : . init (
systemName : " wand.and.stars "
) ,
title : " New Design " ,
subtitle : . init (
try AttributedString (
markdown : " An awesome new _Design_ "
)
)
) WhatsNew.PrimaryAction使用すると、提示されたWhatsNewViewを却下するために使用されるプライマリボタンの動作を構成できます
let primaryAction = WhatsNew . PrimaryAction (
title : " Continue " ,
backgroundColor : . blue ,
foregroundColor : . white ,
hapticFeedback : . notification ( . success ) ,
onDismiss : {
print ( " WhatsNewView has been dismissed " )
}
)注:HapticFeedbackはiOSでのみ実行されます
WhatsNew.PrimaryActionの上に表示されるWhatsNew.SecondaryActionは、 WhatsNewインスタンスを初期化するときにオプションで提供でき、追加のビューを表示したり、カスタムアクションを実行したり、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
// ...
}
)注:HapticFeedbackはiOSでのみ実行されます
WhatsNewKitを使用すると、提示されたWhatsNewViewのレイアウトをさまざまな方法で調整できます。
最も簡単な方法は、 WhatsNew.Layout.defaultインスタンスを変異させることです。
WhatsNew . Layout . default . featureListSpacing = 35自動プレゼンテーションスタイルを使用する場合、WhatsNewenVironmentを初期化するときにデフォルトのレイアウトを提供できます。
. environment (
. whatsNew ,
. init (
defaultLayout : WhatsNew . Layout (
showsScrollViewIndicators : true ,
featureListSpacing : 35
) ,
whatsNew : self
)
)または、whatsnewviewを自動または手動で提示するときに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
)
) UIKitまたはAppKitを使用する場合、 WhatsNewViewControllerを使用できます。
let whatsNewViewController = WhatsNewViewController (
whatsNew : WhatsNew (
version : " 1.0.0 " ,
// ...
) ,
layout : WhatsNew . Layout (
contentSpacing : 80
)
) WhatsNewインスタンスのバージョンが提示されていない場合にのみWhatsNewViewControllerを提示したい場合は、便利なFailable Initializerを利用できます。
// 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 )