SPAlert

Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets. Support SwiftUI.

1523
89
Swift

SPAlert

Popup from Apple Music & Feedback in AppStore. Contains Done, Heart, Error and other. Supports Dark Mode and SwiftUI.
I tried to recreate Apple’s alerts as much as possible. You can find these alerts in the AppStore after feedback and after you add a song to your library in Apple Music.

For get alert from switch silent mode, check library SPIndicator.

Community

Navigate

Installation

Ready for use on iOS 11+.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/ivanvorobei/SPAlert", .upToNextMajor(from: "4.2.0"))
]

CocoaPods:

CocoaPods is a dependency manager. For usage and installation instructions, visit their website. To integrate using CocoaPods, specify it in your Podfile:

pod 'SPAlert'

Manually

If you prefer not to use any of the dependency managers, you can integrate manually. Put Sources/SPAlert folder in your Xcode project. Make sure to enable Copy items if needed and Create groups.

Quick Start

For the best experience, I recommend presenting alerts by calling the class functions SPAlert. These functions are updated regularly and show the alerts as Apple way:

SPAlert.present(title: "Added to Library", preset: .done)

For using a custom image:

SPAlert.present(title: "Love", message: "We'll recommend more like this in For You", preset: .custom(UIImage.init(named: "heart")!))

For showing a simple text message:

SPAlert.present(message: "Something going wrong", haptic: .error)

Usage

Duration

To change the duration of present time, create an alert view and call present method with custom duration:

let alertView = SPAlertView(title: "Complete", preset: .done)
alertView.duration = 4
alertView.present()

If you don’t want to dismiss alert in time, disable dismissInTime. After it, duration property will be ignored.

alertView.dismissInTime = false

In this case, you should dismiss the alert manually.

Dismiss

If you tap the alert, it will disappear. This can be disabled:

alertView.dismissByTap = false

Also, you can manually dismiss all alerts, simply call this:

SPAlert.dismiss()

Layout

For customise layout and margins, use layout property. You can manage margins for each side, icon size and space between image and titles:

alertView.layout.iconSize = .init(width: 24, height: 24)
alertView.layout.margins.top = 12
alertView.layout.spaceBetweenIconAndTitle = 8

Haptic

To manage haptic, you should pass it in present method:

alertView.present(duration: 1.5, haptic: .success, completion: nil)

You can remove duration and completion, because they have default values.

Spinner

I added the preset .spinner, to use it simply call this:

let alertView = SPAlertView(title: "Please, wait", preset: .spinner)
alertView.present()

By default this preset dismissInTime is disabled and the alert needs to be manually dismissed. You can do it only for one view or dismiss all alerts:

// For one alert
alertView.dismiss()

// For all alerts
SPAlert.dismiss()

Colors

For change color of icon, simple set tint color for any preset:

alertView.iconView?.tintColor = .systemRed

//If you set custom image, don't forget set rendering mode:
UIImage(systemName: "pencil.and.outline")!.withRenderingMode(.alwaysTemplate)

Global Appearance

Also you can change some default values for alerts. For example you can change default duration and corner radius for alert with next code:

SPAlertView.appearance().duration = 2
SPAlertView.appearance().cornerRadius = 12

It will apply to all alerts. I recommend setting it in the app delegate, but you can change it in runtime.

SwiftUI

Use like system alert only show message tips:

Button("Show alert") {
    showAlert = true
}.SPAlert(isPresent: $showAlert, message: "this is message only")

or show message, title, image and other configuration:

Button("Show alert") {
    showAlert = true
}.SPAlert(
    isPresent: $showAlert, 
    title: "Alert title", 
    message: "Alert message",
    duration: 2.0, 
    dismissOnTap: false, 
    preset: .custom(UIImage(systemName: "heart")!), 
    haptic: .success, 
    layout: .init(), 
    completion: {
        print("Alert is destory")
    })

Russian Community

Я веду телеграм-канал, там публикую новости и туториалы.

С проблемой помогут в чате.

Видео-туториалы выклыдываю на YouTube:

Tutorials on YouTube