top of page
  • Writer's pictureDon Peter

Introduction to using TipKit in SwiftUI apps


Introduction to using TipKit in SwiftUI apps

TipKit is a new beta framework introduced in iOS 17 that helps developers present tips and instructions to users in their apps. With TipKit, you can easily create and manage tips, set rules for when and how they are displayed.


In this blog post, we will walk you through the steps of getting started with TipKit in your SwiftUI apps.


Steps to using TipKit


1. Creating a Tip


To use TipKit, you first need to import the TipKit framework into your project, along with the SwiftUI framework. Then, you can create a new Tip object and specify its content, title, and other properties.


import SwiftUI
import TipKit


struct FavoriteBookTip: Tip {

    var title: Text {
        Text("Add as Favorite book")
    }


    var message: Text? {
        Text("Click on the fav icon to add the book to favourites")
    }

}



For a tip to be valid, it is mandatory to set its title.



2. Adding Rules


Next step is to add rules that must be met in order for the tip to be displayed. The rules property is an array of Predicate objects, each of which specifies a condition that must be met.


import SwiftUI
import TipKit


struct FavoriteBookTip: Tip {

    var title: Text {
        Text("Add as Favorite book")
    }


    var message: Text? {
        Text("Click on the fav icon to add the book to favourites")
    }

    var rules: Predicate<RuleInput...> {
        #Rule(Self.$isLoggedIn) { $0 == true }
    }    
}

In this case, the only rule is that the isLoggedIn property must be equal to true. This means that the tip will only be displayed if the user is logged in.


The #Rule syntax is used to create rules. In this case, the #Rule tag is used to create a rule that is based on the Self.$isLoggedIn property. The Self keyword refers to the current view, and the $isLoggedIn property is a property that gets the current user's login status.


3. Adding Tip to SwiftUI view


import SwiftUI
import TipKit


struct FavoriteBookTip: Tip {


    var title: Text {
        Text("Add as Favorite book")
    }


    var message: Text? {
        Text("Click on the fav icon to add the book to favourites")
    }

    var rules: Predicate<RuleInput...> {
        #Rule(Self.$isLoggedIn) { $0 == true }
    }    
}


@main
struct BookTips: App {
    var bookTip = FavoriteBookTip()


    var body: some Scene {
        WindowGroup {
            VStack {
                TipView(bookTip, arrowEdge: .bottom)

                Image(systemName: "fav_icon")
                    .imageScale(.large)
                Spacer()
            }
        }
    }
}
    

TipView is a user interface element that represents an inline tip that is provided by TipKit. It displays a tip with an arrow that points to the bottom of the screen. The arrowEdge parameter specifies the edge of the screen where the arrow should point.


In this case, the arrowEdge parameter is set to .bottom, so the arrow will point to the bottom of the screen. TipView takes FavoriteBookTip object as its argument and displays the tip with an arrow that points to the bottom of the screen.


4. Configuring Tip using TipsCenter


Once you have created a tip, you can configure TipsCenter.


TipsCenter is a key part of TipKit that provides several essential features. It allows tips and their associated events to persist between app launches, making it easier to test tips. A default shared instance of TipsCenter is provided, which is what I have added here.


TipsCenter.shared.configure(
    displayFrequency: .daily
)
    

The displayFrequency property specifies how often the tip should be displayed. In this case, the tip will be displayed once per day.


Once you have created your tip and configured TipsCenter, you can display it using the following code when testing.


TipsCenter.shared.showTips([bookTip])


Use-cases of TipKit


Here are a few examples of how you can use TipKit in your SwiftUI or UIKit based apps:

  • Display a tip when a user opens your app for the first time.

  • Show a tip when a user performs a specific action, such as taking a photo or adding a contact.

  • Give users tips on how to use your app's features.

  • Provide instructions on how to troubleshoot common problems.


Conclusion


TipKit is a powerful new framework that can help you improve the user experience of your SwiftUI or UIKit based apps. By using TipKit, you can easily create and manage tips, set rules for when and how they are displayed, and track their effectiveness.


To make sure your tips are effective, keep them short, instructional, and actionable.


Blog for Mobile App Developers, Testers and App Owners

 

This blog is from Finotes Team. Finotes is a lightweight mobile APM and bug detection tool for iOS and Android apps.

In this blog we talk about iOS and Android app development technologies, languages and frameworks like Java, Kotlin, Swift, Objective-C, Dart and Flutter that are used to build mobile apps. Read articles from Finotes team about good programming and software engineering practices, testing and QA practices, performance issues and bugs, concepts and techniques. 

bottom of page