Skip to content

Instantly share code, notes, and snippets.

View timonus's full-sized avatar

Tim Johnsen timonus

View GitHub Profile
@timonus
timonus / SwiftLeakPlayground.swift
Created March 24, 2023 21:57
Swift Leak Playground
//
// ViewController.swift
// Playground
//
// Created by Tim Johnsen on 3/24/23.
//
import UIKit
class MyObject {
@timonus
timonus / TJSwizzle.m
Created March 7, 2023 16:59
Swizzling copypasta
#import <objc/runtime.h>
void tj_swizzle(Class class, SEL originalSelector, SEL swizzledSelector)
{
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
@timonus
timonus / SFSafariViewController+ContextMenuBugWorkaround.m
Last active August 20, 2020 02:33
Workaround for SFSafariViewController preview + iOS 13 Context Menu negative interaction bug (FB7564018)
// Workaround for SFSafariViewController preview + iOS 13 context menu negative interaction bug (FB7564018)
// Examples: https://db.tt/hvBJOBdZwL, https://db.tt/egp20bSfS0
// With fix: https://db.tt/r5nCLNYbfs
@interface SFSafariViewController (ContextMenuBugWorkaround)
/// If this returns @c YES it's safe to present this view controller when "committing" from an iOS 13 context menu. If not, you should create a fresh @c SFSafariViewController and present that instead.
- (BOOL)canBePresentedOnContextMenuCompletion;
@end
@timonus
timonus / programmatic-dynamic-images.m
Last active January 1, 2024 12:08
Programmatically create iOS 13 dynamic images
- (UIImage *)dynamicImage
{
UITraitCollection *const baseTraitCollection = /* an existing trait collection */;
UITraitCollection *const lightTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]]];
UITraitCollection *const purelyDarkTraitCollection = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];
UITraitCollection *const darkTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, purelyDarkTraitCollection]];
__block UIImage *lightImage;
[lightTraitCollection performAsCurrentTraitCollection:^{
lightImage = /* draw image */;
@timonus
timonus / bookmarklet.js
Created June 21, 2017 18:30
Redirector Bookmarklet
// This redirects to a page that contains a link to the page currently being viewed, which can be useful for debugging things.
// Instructions: Create a bookmark then edit its address to be this.
// Video: https://db.tt/DaXP7t66KN
javascript:location.href='https://u121985.dl.dropboxusercontent.com/u/121985/redirect-link.html?url='+encodeURIComponent(location.href);
//
// UITraitCollection+DarknessCompatibility.h
// Tangent
//
// Created by Tim Johnsen on 6/25/16.
// Copyright © 2016 tijo. All rights reserved.
//
#import <UIKit/UIKit.h>
@timonus
timonus / UIWindow+AppSwitchScrollStopper.h
Last active January 6, 2017 19:01
AppSwitchScrollStopper
// UIWindow+AppSwitchScrollStopper.h
// Created by Tim Johnsen on 3/27/16.
#import <UIKit/UIKit.h>
@interface UIWindow (AppSwitchScrollStopper)
/// Call this early on in your app's lifecycle to avoid
/// scroll-related flashing when your app resumes from the background
- (void)installAppSwitchScrollStopper;
@timonus
timonus / TJLogoView.h
Created September 10, 2015 02:59
tijo Logo Objective-C Source
//
// TJLogoView.h
// Logo
//
// Created by Tim Johnsen on 9/7/15.
// Copyright (c) 2015 tijo. All rights reserved.
//
#import <UIKit/UIKit.h>
@timonus
timonus / kill.rb
Created August 20, 2015 20:34
Deletes all yer iOS simulators
#!/usr/bin/env ruby
device_types_output = `xcrun simctl list devicetypes`
device_types = device_types_output.scan /(.*) \((.*)\)/
runtimes_output = `xcrun simctl list runtimes`
runtimes = runtimes_output.scan /(.*) \(.*\) \((com.apple[^)]+)\)$/
devices_output = `xcrun simctl list devices`
devices = devices_output.scan /\s\s\s\s(.*) \(([^)]+)\) (.*)/
@timonus
timonus / class-hierarchy.py
Created January 16, 2015 00:43
Class Hierarchy Visualizer
import sys
import subprocess
import re
import os
import shutil
import json
# usage
# python class-hierarchy.py *directory to scan within*
# outputs nested json