Skip to content

Instantly share code, notes, and snippets.

View benbahrenburg's full-sized avatar

Ben Bahrenburg benbahrenburg

View GitHub Profile
@benbahrenburg
benbahrenburg / UIImageToDataTests.swift
Last active January 26, 2024 19:49
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
benbahrenburg / UIImageToDataIO.swift
Created March 5, 2017 23:55
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)!
@benbahrenburg
benbahrenburg / gist:1129364
Created August 6, 2011 14:15
Tell git to globally ignore .DS_Store
View gist:1129364
git config --global core.excludesfile ~/.gitignore
echo .DS_Store >> ~/.gitignore
@benbahrenburg
benbahrenburg / geo.md
Last active January 16, 2023 09:50
iOS 8: Geo Location Permissions Fix
View geo.md

Fixing Geo Location Permissions in iOS8

In iOS8, Apple has changed how geo location permissions work. This gist outlines an approach to fix this so Geo will continue to work in iOS8.

Before getting started with the code change, we need to update the tiapp.xml with a few keys.

If you wish to have Geo always run, you need to add the below key. The key NSLocationAlwaysUsageDescription is used when requesting permission to use location services whenever the app is running. To enable this, add the below to the tiapp.xml:

NSLocationAlwaysUsageDescription Reason that appears in the authorization prompt

@benbahrenburg
benbahrenburg / deviceOwnerAuthentication.swift
Created January 1, 2017 03:52
Use LAContext to check if the device has a passcode
View deviceOwnerAuthentication.swift
public func devicePasscodeEnabled() -> Bool {
return LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)
}
@benbahrenburg
benbahrenburg / TimeZoneManager.swift
Created December 29, 2019 04:25
List all iOS timezones
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
benbahrenburg / CryptoKitHelpers.swift
Last active July 13, 2022 11:37
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
benbahrenburg / KeyChainAccessGroupHelper.swift
Last active July 5, 2021 21:18
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 bencoding.com. All rights reserved.
//
import Foundation
import Security
@benbahrenburg
benbahrenburg / CLLocationManager+Combine.swift
Created June 11, 2021 19:28 — 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
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?