Skip to content

Instantly share code, notes, and snippets.

// 1. Implement URLSession:task:didReceiveChallenge:completionHandler:. (Be sure to call the completion handler with NSURLSessionAuthChallengeDisposition.PerformDefaultHandling and a nil NSURLCredential.)
// 2. Set a breakpoint in the callback + trigger an authentication challenge at runtime
// 3. In the debugger, inspect the callback's `challenge.protectionSpace` argument for the values to put in the following lines of code...
// 4. Put the following lines of code somewhere before your network requests (i.e. AppDelegate area, or other)
let basicAuthCredentials = NSURLCredential(user: "username", password: "password", persistence: .Permanent)
let foobarHttpsProtectionSpace = NSURLProtectionSpace(host: "foo.bar.com", port: 443, protocol: "https", realm: "This Probably Has A Value, Get It From The Delegate Callback", authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
NSURLCredentialStorage.sharedCredentialStorage().setDefaultCredential(basicAuthCredentials, forProtectionSpace: apidevHttpsProtectionSpace
@joshavant
joshavant / AppDelegate.swift
Last active June 11, 2016 15:11
Optimal Push Notification Pattern
class AppDelegate: UIResponder, UIApplicationDelegate {
private var currentUserNotificationTypes: UIUserNotificationType = .None
private func processUpdatedUserNotificationSettings(application: UIApplication, currentUserNotificationSettings notificationSettings: UIUserNotificationSettings?) {
let newUserNotificationTypes: UIUserNotificationType = notificationSettings?.types ?? .None
// only performs actions if the current value has a delta with the new value
if currentUserNotificationTypes.contains(.Alert) != newUserNotificationTypes.contains(.Alert) {
self.currentUserNotificationTypes = newUserNotificationTypes
enum ApplicationSupportFileServiceError: ErrorType {
case WriteFailed
case CouldntGetApplicationSupportDirectoryURL // it would be super weird for this to happen
}
typealias ApplicationSupportFileServiceKeyType = String // value represents a filename
struct ApplicationSupportFileService: KeyedItemPersistenceServiceable {
let fileManager: NSFileManager
@joshavant
joshavant / String+Replacement.swift
Created May 1, 2016 00:19
Replace characters from set with replacement string
import Foundation
extension String {
func replaceCharactersFromSet(characterSet: NSCharacterSet, replacementString: String) -> String {
let scanner = NSScanner(string: self)
scanner.charactersToBeSkipped = nil
let sanitizedString = NSMutableString(capacity: self.characters.count)
while(!scanner.atEnd) {
@joshavant
joshavant / gist:1a134eb4209af7e8b0befc79c76fec8a
Created April 26, 2016 00:20
In-Air Internet Connectivity Profile
$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=36 time=721.833 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=36 time=847.672 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=36 time=742.803 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=36 time=599.856 ms
Request timeout for icmp_seq 4
64 bytes from 8.8.8.8: icmp_seq=4 ttl=36 time=1066.764 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=36 time=647.643 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=36 time=668.060 ms
@joshavant
joshavant / RawRepresentableExtensions.swift
Created April 21, 2016 20:59
RawRepresentable, meet NSCoding
extension RawRepresentable where Self: NSCoding {
init?(rawValue: NSData) {
if let instance = NSKeyedUnarchiver.unarchiveObjectWithData(rawValue) as? Self {
self = instance
} else {
return nil
}
}
var rawValue: NSData {
@joshavant
joshavant / KeychainCoding.swift
Created April 19, 2016 22:17
Swifty Keychain Abstraction
protocol KeychainCoding {
init?(keychain: KeychainPersistable, identifier: KeychainPersistableIdentifer)
func encodeToKeychain(keychain: KeychainPersistable, identifier: KeychainPersistableIdentifer) throws
}
extension KeychainCoding where Self: NSCoding, Self: InstanceInitializable {
init?(keychain: KeychainPersistable, identifier: KeychainPersistableIdentifer) {
do {
if let data = try keychain.get(identifier) {
let unarchivedInstance = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Self
import UIKit
func namedModels() -> [ModelObject] {
let names: [String] = UIFont.familyNames()
return names.map(ModelObject.init)
}
class ModelObject: NSObject {
let name: String
init(name: String) { self.name = name }
@joshavant
joshavant / KeyboardMetricsService.swift
Last active March 11, 2016 21:47
A basic, thin wrapper on keyboard appearance handling. This is mostly just a starting point: this only updates its `var currentMetrics`, which leaves to the user the exercise of adding whatever update hooks make sense for their codebase (likely through subclassing).
//
// KeyboardMetricsService.swift
// CrowdRise
//
// Created by Josh Avant on 3/10/16.
// Copyright © 2016 CrowdRise. All rights reserved.
//
import UIKit
@joshavant
joshavant / custom_colors.md
Last active January 30, 2016 18:36
How to manage custom OS X color palettes in your Xcode project

How to manage custom OS X color palettes in your Xcode project

The following steps configure your Xcode project to check the status of your custom color palette in the system, during the build process.

When your custom color palette is not installed in the system, Xcode's build process will fail with an error, instructing you to run a script which will install the file.

When the installed color palette doesn't match what is in your project's root directory (according to an md5 file checksum comparison), Xcode's build process will warn you to update the .clr file.

This system allows you to keep your custom color palette file in your project directory, under source control. Whenever changes to the palette file are pushed to the repo, Xcode will notice them and notify you during the build process.