Skip to content

Instantly share code, notes, and snippets.


Ben Bahrenburg benbahrenburg

View GitHub Profile
benbahrenburg / deviceOwnerAuthentication.swift
Created Jan 1, 2017
Use LAContext to check if the device has a passcode
View deviceOwnerAuthentication.swift
public func devicePasscodeEnabled() -> Bool {
return LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)
View TimeZoneManager.swift
class TimeZoneManager {
static let shared = TimeZoneManager()
private init() {}
private var continents = [String]()
private var timeZonesList = [String: [String]]()
var timeZones: [String: [String]] {
get {
for timeZone in TimeZone.knownTimeZoneIdentifiers {
benbahrenburg / CryptoKitHelpers.swift
Last active Jul 13, 2022
CryptoKit ChaChaPoly Helper Functions
View CryptoKitHelpers.swift
import UIKit
import CryptoKit
struct CryptoKitHelpers {
static func encrypt(secret: String, image: UIImage, compressionRatio: CGFloat = 0.9) throws -> Data? {
guard let encryptionKey = getKey(secret: secret) else { return nil }
guard let contentData = UIImageToDataJPEG2(image: image, compressionRatio: compressionRatio) else {
return nil
return try ChaChaPoly.seal(contentData, using: encryptionKey).combined
benbahrenburg / gist:1129364
Created Aug 6, 2011
Tell git to globally ignore .DS_Store
View gist:1129364
git config --global core.excludesfile ~/.gitignore
echo .DS_Store >> ~/.gitignore
benbahrenburg / UIImageToDataTests.swift
Last active Mar 6, 2022
Swift Playground for testing memory associated with converting UIImage to Data
View UIImageToDataTests.swift
import UIKit
import ImageIO
import MobileCoreServices
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func report_memory() -> UInt64 {
var taskInfo = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
benbahrenburg / KeyChainAccessGroupHelper.swift
Last active Jul 5, 2021
KeyChainAccessGroupHelper - Making it easier to deal with Keychain Access Groups
View KeyChainAccessGroupHelper.swift
// KeyStorage - Simplifying securely saving key information
// KeyChainAccessGroupHelper.swift
// Created by Ben Bahrenburg on 12/30/16.
// Copyright © 2017 All rights reserved.
import Foundation
import Security
benbahrenburg / CLLocationManager+Combine.swift
Created Jun 11, 2021 — forked from malhal/CLLocationManager+Combine.swift
A Combine location publisher for CLLocationManager.
View CLLocationManager+Combine.swift
// Requirements: a NSLocationWhenInUseUsageDescription entry in Info.plist
// Usage: @State var locator = CLLocationManager.publishLocation()
// and
// .onReceive(locator) { location in
// Improvements needed: Move requestWhenInUseAuthorization into its own publisher and perhaps have a combineLatest pipeline for both authorized and valid location.
// A configuration param to init(), e.g. so each manager can have the same distanceFilter.
import Foundation
import Combine
import CoreLocation
benbahrenburg / UIImageToDataIO.swift
Created Mar 5, 2017
Using ImageIO and Swift to convert a UIImage to Data
View UIImageToDataIO.swift
func UIImageToDataIO(image: UIImage, compressionRatio: CGFloat, orientation: Int = 1) -> Data? {
return autoreleasepool(invoking: { () -> Data in
let data = NSMutableData()
let options: NSDictionary = [
kCGImagePropertyOrientation: orientation,
kCGImagePropertyHasAlpha: true,
kCGImageDestinationLossyCompressionQuality: compressionRatio
let imageDestinationRef = CGImageDestinationCreateWithData(data as CFMutableData, kUTTypeJPEG, 1, nil)!
View OIDExternalUserAgentASWebAuthenticationSession.swift.swift
import UIKit
import SafariServices
import AuthenticationServices
import AppAuth
import Reachability
class OIDExternalUserAgentASWebAuthenticationSession: NSObject, OIDExternalUserAgent {
private let presentingViewController: UIViewController
private var externalUserAgentFlowInProgress: Bool = false
private var authenticationViewController: ASWebAuthenticationSession?
benbahrenburg / escape-json.js
Created Dec 25, 2020 — forked from matthewmueller/escape-json.js
Escape JSON strings before trying to run JSON.parse
View escape-json.js
Escape JSON
var escapeJSON = exports.escapeJSON = function(json) {
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
var meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',