// // AppExtention.swift // Learn Genie // // Created by Suraj Kumar Mandal on 02/09/21. // import Foundation import UIKit extension UIApplication { class func isFirstLaunch() -> Bool { if !UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") { UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag") UserDefaults.standard.synchronize() return true } return false } } extension String { var htmlToAttributedString: NSAttributedString? { guard let data = data(using: .utf8) else { return NSAttributedString() } do { return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { return NSAttributedString() } } var htmlToString: String { return htmlToAttributedString?.string ?? "" } //  Stripping out HTML tags from a string public func trimHTMLTags() -> String? { guard let htmlStringData = self.data(using: String.Encoding.utf8) else { return nil } let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [ .documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue ] let attributedString = try? NSAttributedString(data: htmlStringData, options: options, documentAttributes: nil) return attributedString?.string } func stringBefore(_ delimiter: Character) -> String { if let index = firstIndex(of: delimiter) { return String(prefix(upTo: index)) } else { return "" } } func stringAfter(_ delimiter: Character) -> String { if let index = firstIndex(of: delimiter) { return String(suffix(from: index).dropFirst()) } else { return "" } } func toDateString( inputDateFormat inputFormat : String, ouputDateFormat outputFormat : String ) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = inputFormat let date = dateFormatter.date(from: self) dateFormatter.dateFormat = outputFormat return dateFormatter.string(from: date!) } } extension Data { init(fromArray values: [T]) { var values = values self.init(buffer: UnsafeBufferPointer(start: &values, count: values.count)) } func toArray(type: T.Type) -> [T] { let value = self.withUnsafeBytes { $0.baseAddress?.assumingMemoryBound(to: T.self) } return [T](UnsafeBufferPointer(start: value, count: self.count / MemoryLayout.stride)) } } extension UITextField { func datePicker(target: T, doneAction: Selector, cancelAction: Selector, datePickerMode: UIDatePicker.Mode = .date) { let screenWidth = UIScreen.main.bounds.width func buttonItem(withSystemItemStyle style: UIBarButtonItem.SystemItem) -> UIBarButtonItem { let buttonTarget = style == .flexibleSpace ? nil : target let action: Selector? = { switch style { case .cancel: return cancelAction case .done: return doneAction default: return nil } }() let barButtonItem = UIBarButtonItem(barButtonSystemItem: style, target: buttonTarget, action: action) return barButtonItem } let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216)) datePicker.datePickerMode = datePickerMode datePicker.maximumDate = Date() // iOS 14 and above if #available(iOS 14, *) { datePicker.preferredDatePickerStyle = .wheels datePicker.sizeToFit() } self.inputView = datePicker let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44)) toolBar.setItems([buttonItem(withSystemItemStyle: .cancel), buttonItem(withSystemItemStyle: .flexibleSpace), buttonItem(withSystemItemStyle: .done)], animated: true) self.inputAccessoryView = toolBar } } extension UIWindow { static var key: UIWindow? { if #available(iOS 13, *) { return UIApplication.shared.windows.first { $0.isKeyWindow } } else { return UIApplication.shared.keyWindow } } } extension String { var isValidEmail: Bool { NSPredicate(format: "SELF MATCHES %@", "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}").evaluate(with: self) } }