Skip to content

Instantly share code, notes, and snippets.

View bnickel's full-sized avatar

Brian Nickel bnickel

View GitHub Profile
#import <objc/runtime.h>
@interface UIViewController (SEUIIntentTarget_SWIZZLE)
- (UIResponder *)SEUI_originalNextResponder;
@end
@interface SEUIIntentTarget ()
@property (nonatomic, weak, nullable) UIViewController *owningViewController;
@end
@interface UIMenuController (Swizzle)
@end
@implementation UIMenuController (Swizzle)
+ (void)load
{
[self jr_swizzleMethod:@selector(setTargetRect:inView:) withMethod:@selector(SE_setTargetRect:inView:) error:NULL];
}
@import StacMan;
@import JRSwizzle;
NS_INLINE NSURL *TransformURLForForTier(NSURL *URL, SEAPIContext *context) {
if (context.mobileRoot || context.apiRoot) {
NSMutableString *computedUrl = [URL.absoluteString mutableCopy];
if (context.mobileRoot) {
[computedUrl replaceOccurrencesOfString:@"https://mobile.stackexchange.com" withString:context.mobileRoot options:NSCaseInsensitiveSearch range:NSMakeRange(0, [computedUrl length])];
@import UIKit;
@import JRSwizzle;
@interface UINavigationController (Logging)
@end
@implementation UINavigationController (Logging)
+ (void)load
{
@bnickel
bnickel / aspect-grow.swift
Created August 7, 2015 15:28
Walgreens photo uploaded would only let me crop, but I had a bunch of square photos, so...
#!/usr/bin/swift
// Because sometimes you don't want to crop.
// USAGE: aspect-grow RATIO FILES...
// aspect-grow 4:6 *.jpg
//
// Places all files in an output directory relative to their location, so
// aspect-grow 4:6 *.jpg creates a folder "output" in this directory but
// aspect-grow 4:6 a/b.jpg and b/c.jpg creates folders a/output and b/output.
@bnickel
bnickel / Keychain.playground
Created February 23, 2015 22:43
Application passwords in Swift using function overloading
import Security
import Foundation
// MARK: - Redefining boilerplate code
func SecKeychainOpen(path:String) -> SecKeychainRef? {
var keychain:Unmanaged<SecKeychainRef>? = nil
if SecKeychainOpen((path as NSString).UTF8String, &keychain) == errSecSuccess {
return keychain?.takeRetainedValue()
@bnickel
bnickel / RestorationDefender.swift
Last active January 12, 2023 09:47
A few friendly methods to help you detect state restoration problems in your non-storyboard apps.
// USAGE:
// Call RestorationDefender.printViewControllerClassesThatAreProbablyNotRestorable() to print a list of view controllers that will probably not return from state restoration.
// Call RestorationDefender.crashWhenViewControllersDoNotImplementStateRestoration() to crash your app when a view controller appears without setting restorationIdentifier and restorationClass.
// Call RestorationDefender.shoutWhenViewControllersDoNotImplementStateRestoration() to print a big message when a view controller appears without setting restorationIdentifier and restorationClass.
import Foundation
private func objc_getClassList() -> [AnyClass] {
let expectedClassCount = objc_getClassList(nil, 0)
var allClasses = UnsafeMutablePointer<AnyClass?>.alloc(Int(expectedClassCount))
@bnickel
bnickel / command-line.swift
Created January 21, 2015 17:59
Provides operators ans wrappers for terse command execution in Swift scripts.
#!/usr/bin/swift
// TL;DR: Skip to line 183.
// To run as a script, run with /usr/bin/swift or chmod +x.
// To run as a playground, comment out the first line and `mkdir -p "~/Documents/Shared Playground Data"`.
import Foundation
let stdin = NSFileHandle.fileHandleWithStandardInput()
let stdout = NSFileHandle.fileHandleWithStandardOutput()
@bnickel
bnickel / page.html
Created January 12, 2015 21:21
Test page to see how far away from a link you can touch while still tapping it.
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<style>
div {
position: absolute;
background-color:green;
left: 100px;
}
@bnickel
bnickel / bad-error-messages
Created December 26, 2014 19:39
Demonstrates NSURLSession's lack of localized error descriptions.
#!/usr/bin/swift
import Foundation
let url = NSURL(string: "http://not-a-real-host")!
let request = NSURLRequest(URL: url)
var response:NSURLResponse? = nil
var error:NSError? = nil
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)