Skip to content

Instantly share code, notes, and snippets.

View phatblat's full-sized avatar

Ben Chatelain phatblat

View GitHub Profile
@phatblat
phatblat / AppDelegate.swift
Last active May 4, 2024 12:34
Example of creating HKObserverQuery and enabling background delivery for multiple HKObjectType
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
let healthKitManager = HealthKitManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if onboardingComplete {
healthKitManager.requestAccessWithCompletion() { success, error in
if success { print("HealthKit access granted") }
else { print("Error requesting access to HealthKit: \(error)") }
}
@phatblat
phatblat / FileTypeForData.m
Created April 21, 2015 15:18
Inspect the first few bytes of an NSData to determine the file type
+ (NSString *)fileTypeForData:(NSData *)data
{
/*
http://www.garykessler.net/library/file_sigs.html
1F 8B 08 .‹.
GZ, TGZ GZIP archive file
50 4B 03 04 PK..
ZIP PKZIP archive file (Ref. 1 | Ref. 2)
@phatblat
phatblat / gist:b10a463ff40d9dc6a808
Created July 7, 2015 20:29
Darwin notifications in swift
// MARK: - Darwin Notifications
static private let defaultsOnPhoneChangedDarwinNotificationName = "UserDefaultsOnPhoneDidChangeDarwinNotification"
static private let defaultsOnWatchChangedDarwinNotificationName = "UserDefaultsOnWatchDidChangeDarwinNotification"
static var notificationNameForReceiving: String?
static var notificationNameForSending: String?
/// Sets up the broadcast and receiving darwin notification names as well as
/// as the necessary observer callbacks.
@phatblat
phatblat / simulating-joins.swift
Created June 22, 2023 21:10 — forked from mbalex99/simulating-joins.swift
How to Do SQL-like JOINs in Ditto
class DittoManager {
let ditto: Ditto
static let shared = DittoManager()
init() {
ditto = Ditto(identity: .onlinePlayground(appID: "e23ffe6b-70d1-4246-83a8-94cbd7a79a8d", token: "f058ac2a-4576-4af1-96f2-c4788d848cf8"))
try! ditto.startSync()
}
@phatblat
phatblat / POD_NAME.podspec
Created October 20, 2014 15:30
POC of a Pod which installs a run script into Xcode (CocoaPods 0.33 or less)
Pod::Spec.new do |s|
s.name = 'POD_NAME'
s.version = '1.0.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'Example of pod which installs a run script into the Xcode project (first target)'
s.homepage = 'https://github.com/phatblat/POD_NAME'
s.authors = { 'Ben Chatelain' => 'benchatelain@gmail.com' }
s.source = { :git => 'https://github.com/phatblat/POD_NAME.git', :tag => s.version.to_s }
s.ios.deployment_target = '6.0'
@phatblat
phatblat / MakeAutomaticVariables.md
Last active August 1, 2022 16:13
Make Automatic Variables Cheatsheet

Make Automatic Variables Cheatsheet

I can never remember all of the automatic variables that can be used in a Makefile so here is a table of them.

Variable Description
$@ File name of the target of the rule
$% Target member name, when the target is an archive member
$< Name of the first prerequisite
@phatblat
phatblat / gist:5432046
Created April 22, 2013 02:21
Bash script to extract the repo name out of a URL
#!/bin/bash
repo_url=git@github.com:phatblat/dotfiles.git
repo=${repo_url##*/}
echo ${repo%%.*}
@phatblat
phatblat / package-ida.sh
Created May 11, 2015 16:53
A quick script to package up an .ipa correctly since `xcodebuild -exportArchive` misses the required SwiftSupport and WatchKitSupport folders
#!/bin/bash -e
#
# package-ipa.sh
#
# Bundles an iOS app correctly, using the same directory structure that Xcode does when using the export functionality.
#
xcarchive="$1"
output_ipa="$2"
@phatblat
phatblat / AppDelegate.m
Last active May 14, 2020 08:17
Repost NSUserDefaultsDidChangeNotification to iOS extensions
#pragma mark - App Singleton
- (instancetype)init {
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil];
...
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
@phatblat
phatblat / UICollectionViewLayoutAttributes+CustomCell.swift
Last active April 15, 2020 20:07
Example of using Objective-C associated objects as the backing store for properties added via a Swift extension.
private var stringPropertyAssociationKey: UInt8 = 0
private var intPropertyAssociationKey: UInt8 = 0
// This is a replacement for a custom UICollectionViewLayoutAttributes subclass since
// layoutAttributesForElementsInRect() is crashing when a subclass is registered.
// https://devforums.apple.com/message/1120429#1120429
//
// http://stackoverflow.com/questions/25426780/swift-extension-stored-properties-alternative#answer-25428013
// http://stackoverflow.com/questions/24133058/is-there-a-way-to-set-associated-objects-in-swift#answer-25428409
extension UICollectionViewLayoutAttributes {