Skip to content

Instantly share code, notes, and snippets.

Daniel Tartaglia danielt1263

View GitHub Profile
@danielt1263
danielt1263 / AddressBook.h
Last active Aug 29, 2015
Easily access a read-only array of all the people in the contacts database for iOS. (Because Apple STILL hasn't made an ABAddressBook class for iOS like they did for MacOS.
View AddressBook.h
#import <Foundation/Foundation.h>
extern NSString *addressBookErrorDomain;
typedef NS_ENUM(NSInteger, AddressBookError) {
AddressBookErrorOK,
AddressBookErrorNoAddressBook,
AddressBookErrorNoAccess
};
View UIViewController+GetImage.swift
//
// UIViewController+GetImage.swift
//
// Created by Daniel Tartaglia on 4/25/16.
// Copyright © 2016 MIT License
//
import UIKit
import PromiseKit
View UIColorExtensions.swift
//
// UIColorExtensions.swift
//
// Created by Daniel Tartaglia on 12/4/14.
// Copyright (c) 2014 Daniel Tartaglia. MIT License.
//
import UIKit
@danielt1263
danielt1263 / AppStore.swift
Last active Jan 10, 2017
App Store implementation
View AppStore.swift
//
// AppStore.swift
//
// Created by Daniel Tartaglia on 1/10/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import Foundation
import StoreKit
View FluxDispatcher
//
// Dispatcher.swift
// Flux
//
// Created by Daniel Tartaglia on 3/13/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import Foundation
@danielt1263
danielt1263 / encapsulating user code_1.swift
Last active Apr 12, 2017
Gists supporting the "Encapsulating the User" blog post.
View encapsulating user code_1.swift
//
// ViewController.swift
//
// Created by Daniel Tartaglia on 1/27/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import UIKit
class ViewController: UIViewController
View encapsulating the user code_2.swift
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as? UIImage)
if let data = image.flatMap({ UIImageJPEGRepresentation($0, 0.8)} ) {
api.upload(avatar: data).then { [weak self] in
self?.avatarView.image = image
self?.dismiss(animated: true, completion: nil)
}
.catch { [weak self] error in
View encapsulating the user code_3.swift
extension UIViewController
{
@discardableResult
func displayInformationAlert(title: String, message: String) -> Promise<Void> {
return Promise(queue: DispatchQueue.main) { fulfill, _ in
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in fulfill() }))
self.present(alert, animated: true, completion: nil)
}
}
View encapsulating the user code_5.swift
class ViewController: UIViewController
{
@IBAction func changeAvatar(_ sender: UITapGestureRecognizer) {
guard let senderView = sender.view else { fatalError("Tapped on viewless gesture recognizer?") }
let controller = UIImagePickerController()
controller.delegate = self
choiceIndexUsingActionSheet(title: "", message: "", choices: sourceOptions.map { $0.title }, onSourceView: senderView).then { index in
sourceOptions[index].action(controller)
self.present(controller, animated: true, completion: nil)
}
View encapsulating the user code_6.swift
class ImagePickerDelegate: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
let promise = Promise<UIImage>()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as? UIImage) {
promise.fulfill(image)
}
else {
promise.reject(UIImagePickerControllerError.missingImage)
You can’t perform that action at this time.