Skip to content

Instantly share code, notes, and snippets.

View danielt1263's full-sized avatar

Daniel Tartaglia danielt1263

View GitHub Profile
@danielt1263
danielt1263 / AddressBook.h
Last active August 29, 2015 14:09
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.
#import <Foundation/Foundation.h>
extern NSString *addressBookErrorDomain;
typedef NS_ENUM(NSInteger, AddressBookError) {
AddressBookErrorOK,
AddressBookErrorNoAddressBook,
AddressBookErrorNoAccess
};
//
// UIViewController+GetImage.swift
//
// Created by Daniel Tartaglia on 4/25/16.
// Copyright © 2016 MIT License
//
import UIKit
import PromiseKit
//
// 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 January 10, 2017 20:56
App Store implementation
//
// AppStore.swift
//
// Created by Daniel Tartaglia on 1/10/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import Foundation
import StoreKit
//
// 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 April 12, 2017 18:49
Gists supporting the "Encapsulating the User" blog post.
//
// ViewController.swift
//
// Created by Daniel Tartaglia on 1/27/17.
// Copyright © 2017 Daniel Tartaglia. MIT License.
//
import UIKit
class ViewController: UIViewController
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
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)
}
}
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)
}
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)