Skip to content

Instantly share code, notes, and snippets.

View roymckenzie's full-sized avatar

Roy McKenzie roymckenzie

View GitHub Profile
@roymckenzie
roymckenzie / KeyboardAvoidable.swift
Last active September 6, 2022 19:42
Easy way to get your view controllers to respect appearance of the keyboard.
// KeyboardAvoidable
// Roy McKenzie
protocol KeyboardAvoidable: class {
func addKeyboardObservers(customBlock: ((CGFloat) -> Void)?)
func removeKeyboardObservers()
var layoutConstraintsToAdjust: [NSLayoutConstraint] { get }
}
var KeyboardShowObserverObjectKey: UInt8 = 1
@roymckenzie
roymckenzie / PLToggleControl.swift
Last active May 10, 2016 00:36
PLToggleControl
@IBDesignable class PLToggleControl: UIControl {
var items = [String]()
var labels = [UILabel]()
var activeLineView = UIView()
@IBInsepctable var activeTextColor: UIColor = .whiteColor()
@IBInspectable var inactiveTextColor: UIColor = .grayColor()
@IBInspectable var buttonColor: UIColor = .blackColor()
@IBInspectable var activeLineColor: UIColor = .blackColor()
import UIKit
typealias KeyboardHeightDuration = (height: CGFloat, duration: Double)
// New notification type
let RemoveKeyboardNotifications = "RemoveKeyboardNotifications"
protocol KeyboardAvoidable: class {
var layoutConstraintsForKeyboard: [NSLayoutConstraint] { get }
func addKeyboardObservers()
@roymckenzie
roymckenzie / fastfile.rb
Last active October 25, 2016 01:00
Date based version number generator for fast lane (YYYYMMDD.0)
platform :ios do
lane :beta do
# ...
increment_build_number(xcodeproj: "ProjectName.xcodeproj",
build_number: date_based_build_number)
end
# Generate todays date formatted for version
def today_formatted
Date.today.strftime('%Y%m%d')
@roymckenzie
roymckenzie / PrivateDatabaseSubscription.swift
Last active January 27, 2017 17:36
Private Database Subscription
import CloudKit
// Create subscription with an internal identifier
let subscription = CKDatabaseSubscription(subscriptionID: "internalSubscriptionIdentifier")
// Create an operation to save the subscription
let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription],
subscriptionIDsToDelete: nil)
// Add the operation to the private database
CKContainer.default()
@roymckenzie
roymckenzie / CloudKitBasics.swift
Last active August 10, 2022 12:01
Basic Subscription and Fetch manager and setup for CloudKit
import UIKit
import CloudKit
import RealmSwift
/// Handles common methods for subscriptions across multiple databases
enum CloudKitDatabaseSubscription: String {
case `private`
case `public`
}
final class DataDisplayViewController: UIViewController {
private var realmNotificationToken: NotificationToken?
override func viewDidLoad() {
super.viewDidLoad()
startRealmNotification()
}
@roymckenzie
roymckenzie / CloudKitAppDelegate.swift
Last active January 30, 2017 17:19
AppDelegate implementation for CloudKit
import UIKit
import UserNotifications
import CloudKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
@roymckenzie
roymckenzie / CloudKitSyncable.swift
Last active January 27, 2017 22:00
Protocol for Realm Objects that I want to sync with CloudKit
protocol CloudKitSyncable {
var id: String { get }
var recordID: CKRecordID { get }
var synced: Date? { get }
var modified: Date { get }
var deleted: Date? { get }
var record: CKRecord { get }
var recordChangeTag: String? { get }
var recordOwnerName: String? { get }
}
@roymckenzie
roymckenzie / NotificationPermissionController.swift
Last active February 24, 2017 23:00
Easily request permissions in iOS 9 and iOS 10
import UIKit
import UserNotifications
import PropellerPromise
struct NotificationPermissionController {
static var permissionsEnabled: Bool {
guard let settings = UIApplication.shared.currentUserNotificationSettings else {
return false
}