Skip to content

Instantly share code, notes, and snippets.

@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 / 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 {
@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 / gist:04b8ef20654432ae6549
Created January 8, 2015 21:08
Find the main app bundle from inside an extension bundle
NSBundle *bundle = [NSBundle mainBundle];
if ([[bundle.bundleURL pathExtension] isEqualToString:@"appex"]) {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
bundle = [NSBundle bundleWithURL:[[bundle.bundleURL URLByDeletingLastPathComponent] URLByDeletingLastPathComponent]];
}
NSURL *url = [bundle URLForResource:... withExtension:...];
@phatblat
phatblat / libgit2certs.m
Created November 11, 2014 17:39
An example of configuring libgit2 on iOS to use the root CA cert for GitHub
#import <git2/common.h>
NSString *const CACertificateFile_DigiCert = @"DigiCert High Assurance EV Root CA.pem";
NSString *const certFilePath = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:CACertificateFile_DigiCert];
NSLog(@"Loading certificate: %@", certFilePath);
const char *file = certFilePath.UTF8String;
const char *path = NULL;
int returnValue = git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, file, path);
@phatblat
phatblat / Info.plist
Last active September 13, 2016 05:39
Fabric frameworks
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Fabric</key>
<dict>
<key>APIKey</key>
<string>xxxxxxxxxxxx</string>
<key>Kits</key>
<array>
@phatblat
phatblat / gist:e354c5c7b21a9f557f88
Created October 24, 2014 16:42
iOS Model Identifier
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = calloc(1, size);
if (machine) {
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSLog(@"modelIdentifier: %@", @(machine));
free(machine);
}
@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 / sparse_checkout.sh
Last active August 29, 2015 14:06
Git sparse checkout
tmpdir=/tmp/phatblat-images
subdir=images/
remote_git=git://github.com/phatblat/phatblat.github.io.git
remote_https=https://github.com/phatblat/phatblat.github.io.git
mkdir $tmpdir
cd $tmpdir
git init
git remote add -f origin $remote_git || \
git remote set-url origin $remote_https && \
@phatblat
phatblat / crashlytics.rb
Created June 13, 2014 20:12
CrashlyticsFramework pod run script that works with local (:path) installs by dynamically detecting where the pod was installed
#!/usr/bin/env ruby
# crashlytics.rb
#
# Created by Ben Chatelain on 5/13/14.
# Alternate values make much of this script testable from outside Xcode
if ARGV.length > 0
args = ARGV.join(" ")
else