SPAlert

4.2.0

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

What's New

Updated example app.

2022-01-17T10:51:48Z

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.

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.1.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 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 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

For change duration of present time, create 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 dimiss alert in time, disable dismissInTime. After it duration property will be ignored.

alertView.dismissInTime = false

In this case you shoud dismiss 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, simple 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

For manage haptic, you shoud pass it in present method:

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

You can remove duration and completion, its have default values.

Spinner

I added preset .spinner, for use it simple call this:

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

By default for this preset dismissInTime disabled and need manually dismiss alert. You can do it only for one view or dimiss 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 for all alerts. I recomend set it in 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, 
    present: .custom(UIImage(systemName: "heart")!), 
    haptic: .success, 
    layout: .init(), 
    completion: {
        print("Alert is destory")
    })

Russian Community

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

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

Tutorials on YouTube

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Sep 25 2022 05:46:28 GMT-0500 (GMT-05:00)