Skip to content

Instantly share code, notes, and snippets.

View mlcollard's full-sized avatar

Michael L. Collard mlcollard

View GitHub Profile
@mlcollard
mlcollard / MoveKeyboard.swift
Last active February 2, 2018 21:16
iOS: Move field when keyboard appears
//
// Moving keyboard for item
//
// Note: Comments with this label are to explain semantics of iOS
// Do not use comments like these in your projects
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@mlcollard
mlcollard / ActiveViewViewController.swift
Created January 31, 2018 16:49
iOS: Find active view for keyboard check
//
// Moving keyboard for item
//
// Note: Comments with this label are to explain semantics of iOS
// Do not use comments like these in your projects
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@mlcollard
mlcollard / LoadBundlePlistStruct.swift
Last active February 6, 2018 17:05
iOS: Load plist from bundle into a struct
/* Read the classRecord instance from the bundle plist Class489.plist */
// url of resource file
// Note: Assumes you have added a Class489.plist dictionary
guard let classURL = Bundle.main.url(forResource: "class489", withExtension:"plist") else {
print("Error: Unable to form path")
return
}
// data in resource file
@mlcollard
mlcollard / LoadURLPlistRecord.swift
Last active February 7, 2018 19:26
iOS: Load plist from remote URL into Dictionary
/* Read the classRecord instance from the bundle plist Class489.plist */
// url of resource file
guard let classURL = URL(string: "https://gist.githubusercontent.com/mlcollard/7a50ac639316ff3c375e835f297ccf71/raw/199faedf8e2d3b69fa98d0db31602e333d844225/class489.plist") else {
print("Error: Unable to form path")
return
}
// perform an asynchronous URL GET request and process when downloaded
let task = URLSession.shared.dataTask(with: classURL) {
@mlcollard
mlcollard / SavePlistToUserFile.swift
Last active February 7, 2018 19:28
iOS: Save the contents of a Plist to a user's document directory file
// data to save
// Note: This will work with any data, even Codable struct/class (which I suggest using)
let userData = [ "Firstname":"John", "Middle":"I", "LastName":"Doe" ]
// URL for file UserData.plist in user document directory for this app
let userDataURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("UserData").appendingPathExtension("plist")
// save the userData to the userDataURL
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
@mlcollard
mlcollard / class489.plist
Created February 5, 2018 13:56
iOS: Plist for downloading
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DepartmentName</key>
<string>CS</string>
<key>DepartmentNumber</key>
<string>3460</string>
<key>Name</key>
<string>iOS Development</string>
@mlcollard
mlcollard / CleanViewController.swift
Created February 14, 2018 17:30
iOS: Cleaned up ViewController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
@mlcollard
mlcollard / MasterDetail-AppDelegate.swift
Last active February 16, 2018 18:40
iOS: Master Detail Starting Files
//
// AppDelegate.swift
// SwiftMasterDetailApp
//
// Created by Michael Collard on 2/6/18.
// Copyright © 2018 Michael Collard. All rights reserved.
//
import UIKit
@mlcollard
mlcollard / KeyboardAppears.swift
Last active February 27, 2018 20:57
iOS: Setup notification for keyboard appearing
// shifts the view up for space for the keyboard
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillShow, object: nil, queue: nil) {
notification in
print("\(notification.description)")
// find the size of the keyboard
guard let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else {
print("ERROR: Unable to get keyboard size")
@mlcollard
mlcollard / KeyboardDisappears.swift
Last active February 27, 2018 20:58
iOS: Setup notification for keyboard disappearing
// shifts the view up for space for the keyboard
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil) {
notification in
print("\(notification.description)")
// move back to original position
self.view.frame.origin.y = 0
}