Skip to content

Instantly share code, notes, and snippets.

View MojtabaHs's full-sized avatar

Seyed Mojtaba Hosseini Zeidabadi MojtabaHs

View GitHub Profile
@MojtabaHs
MojtabaHs / ViewRepresentableHelper.swift
Created September 24, 2020 13:20
Representing both AppKit and UIKit Views inside the SwiftUI
#if os(macOS)
public typealias ViewRepresentable = NSViewRepresentable
public typealias NativeView = NSView
#elseif os(iOS)
public typealias ViewRepresentable = UIViewRepresentable
public typealias NativeView = UIView
#endif
public protocol ViewRepresentableHelper: ViewRepresentable {
associatedtype ViewType: NativeView
@MojtabaHs
MojtabaHs / UserDefaultsStorage.swift
Last active March 1, 2024 09:55
A property. wrapper for store/restore variables backed by the given user defaults.
import Foundation
@propertyWrapper
public struct UserDefaultStorage<T: Codable> {
private let key: String
private let defaultValue: T
private let userDefaults: UserDefaults
public init(key: String, default: T, store: UserDefaults = .standard) {
@MojtabaHs
MojtabaHs / SettingsBundleStorage.swift
Created November 6, 2020 17:39
A property wrapper for easy access to the settings bundle.
@propertyWrapper
public struct SettingsBundleStorage<T> {
private let key: String
private let userDefaults: UserDefaults = .standard
public init(key: String) {
self.key = key
setBundleDefaults(plist: .root)
@MojtabaHs
MojtabaHs / InlineObjectModifierOperator.swift
Created November 7, 2020 10:17
A simple operator to modify the object right at the initialization part.
infix operator ..
@inline(__always)
func ..<T: EmptyInitializable>(lhs: T, rhs: (T)->()) -> T {
rhs(lhs)
return lhs
}
/* Example:
@MojtabaHs
MojtabaHs / nextFirstResponder.swift
Created December 4, 2020 22:24
Finds and returns the next responder with the given condition.
extension UIResponder {
func nextFirstResponder(where condition: (UIResponder) -> Bool ) -> UIResponder? {
guard let next = next else { return nil }
if condition(next) { return next }
else { return next.nextFirstResponder(where: condition) }
}
}
@MojtabaHs
MojtabaHs / IranianNationalCodeValidator.swift
Created December 15, 2020 15:14
The logic to validate Iranian National Code
func validateNationalCode(_ code: String) -> Bool {
let stringCode = code.trimmingCharacters(in: CharacterSet.decimalDigits.inverted)
if code.allSatisfy({ $0 == code.first }) { return false }
guard stringCode.count == 10 else { return false }
var convertedCode = code.compactMap { Int(String($0)) }
guard convertedCode.count == 10 else { return false }
switch identifier {
// MARK: - iPhone Identifiers
case "iPhone1,1": return "iPhone"
case "iPhone1,2": return "iPhone 3G"
case "iPhone2,1": return "iPhone 3GS"
case "iPhone3,1": return "iPhone 4"
case "iPhone3,2": return "iPhone 4 GSM Rev A"
case "iPhone3,3": return "iPhone 4 CDMA"
case "iPhone4,1": return "iPhone 4S"

Since iOS 12, the system can read the one-time password from the received SMS automatically and add it above the keyboard, So users can use the code with only one touch. There are rules and limitations for doing this as I tried to list most of them below:

  1. The message must contain the Code prase. Some other words like passcode and password are ok too in English.
  2. The Code phrase (with or without :) must be separate with only one space with your numbers.

example:

Code 1111111
Code: 111111