Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / UIViewController+askGeneral.swift
Created September 15, 2020 06:59
Ask a general question in Swift
import UIKit
extension UIViewController {
func ask(title: String?, question: String?, positiveButtonTitle: String = "Yes", negativeButtonTitle: String = "No", isDangerousAction: Bool = false, delegate: @escaping (_ agreed: Bool) -> Void) {
let alert = UIAlertController(title: title, message: question, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: positiveButtonTitle, style: isDangerousAction ? .destructive : .default) { (_) in
delegate(true)
})
alert.addAction(UIAlertAction(title: negativeButtonTitle, style: .cancel) { (_) in
delegate(false)
@foxicode
foxicode / DecodeEmojiString.swift
Created September 14, 2020 13:56
Decoding string with emojis
import Foundation
import Alamofire
class API {
func loadEmojiString(url: URL, delegate: @escaping (_ str: String?) -> Void) {
AF.request(url).responseData { (response) in
switch response.result {
case .success(let data):
let str = String(bytes: data, encoding: .utf8)
delegate(str)
@foxicode
foxicode / CheckEmojis.swift
Created September 14, 2020 13:21
Restricting UITextField from using emojis
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange, with: string)
if updatedText.containsEmoji {
return false
}
}
return true
}
@foxicode
foxicode / emojis.swift
Created September 14, 2020 12:24
Using emojis in Swift code
let 🌸 = 1
let 🌸🌸 = 2
func +(_ 🐶: Int, _ 🐮: Int) -> Int {
🐶 + 🐮
}
let 🌸🌸🌸 = +(🌸🌸, 🌸)
print(🌸🌸🌸)
@foxicode
foxicode / UIViewController+askDate.swift
Created September 13, 2020 06:34
Requesting date from a user (Swift, iOS)
import UIKit
extension UIViewController {
func askDate(title: String, delegate: @escaping (_ date: Date?) -> Void) {
_ = DatePickerPopup.createAndShow(in: self, title: title, delegate: delegate)
}
}
@foxicode
foxicode / DatePickerPopup.swift
Created September 13, 2020 06:32
Popup view with UIDatePicker (Swift, iOS)
import UIKit
import SnapKit
class DatePickerPopup: UIView {
private var frameView: UIView!
private var titleLabel: UILabel!
private var datePicker: UIDatePicker!
private var okButton: UIButton!
private var cancelButton: UIButton!
@foxicode
foxicode / UIViewController+askNumber.swift
Created September 13, 2020 05:15
Ask user for a number in Swift (iOS)
import UIKit
extension UIViewController {
func ask(title: String?, question: String?, placeholder: String?, keyboardType: UIKeyboardType = .default, delegate: @escaping (_ answer: String?) -> Void) {
let alert = UIAlertController(title: title, message: question, preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = placeholder
textField.keyboardType = keyboardType
}
alert.addAction(UIAlertAction(title: "OK", style: .default) { (_) in
@foxicode
foxicode / UIViewController+ask.swift
Created September 13, 2020 05:07
Ask user a question in Swift (iOS)
import UIKit
extension UIViewController {
func ask(title: String?, question: String?, placeholder: String?, keyboardType: UIKeyboardType = .default, delegate: @escaping (_ answer: String?) -> Void) {
let alert = UIAlertController(title: title, message: question, preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = placeholder
textField.keyboardType = keyboardType
}
alert.addAction(UIAlertAction(title: "OK", style: .default) { (_) in
@foxicode
foxicode / UIViewController+warningErrorLoc.swift
Last active September 13, 2020 05:04
Showing localised warning or error
import UIKit
extension UIViewController {
func show(error: String) {
let alert = UIAlertController(title: NSLocalizedString("error", comment: ""), message: error, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
func show(warning: String) {
@foxicode
foxicode / UIViewController+warningError.swift
Last active September 13, 2020 04:48
Showing warning or error popup
import UIKit
extension UIViewController {
func show(error: String) {
let alert = UIAlertController(title: "Error", message: error, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
func show(warning: String) {