Skip to content

Instantly share code, notes, and snippets.

View insidegui's full-sized avatar

Guilherme Rambo insidegui

View GitHub Profile
@insidegui
insidegui / devicectl.sh
Created October 19, 2023 21:58
Helper functions for using devicectl to kill processes on connected iOS devices
# Add to your zsh profile
function devicepid() {
if [ -z "$1" ]; then
echo "Usage: devicepid <device-name> <search>"
echo "Example: devicepid 'iPhone 15 Pro Max' SpringBoard"
return 1
fi
if [ -z "$2" ]; then
@insidegui
insidegui / PlatformViewRepresentable.swift
Created June 27, 2023 13:05
A protocol that abstracts UIViewRepresentable/NSViewRepresentable allowing for a single implementation for UIKit and AppKit platforms
import SwiftUI
#if !os(watchOS)
#if canImport(UIKit)
public typealias PlatformViewRepresentableType = UIViewRepresentable
#else
public typealias PlatformViewRepresentableType = NSViewRepresentable
#endif // canImport(UIKit)
@insidegui
insidegui / fix-homekit-entitlement.sh
Created August 12, 2022 21:20
Fixes missing HomeKit entitlement when building for Mac Catalyst on Xcode 14
if [ "$EFFECTIVE_PLATFORM_NAME" = "-maccatalyst" ]; then
echo "Adding com.apple.developer.homekit entitlement"
/usr/libexec/PlistBuddy -c "Add :com.apple.developer.homekit bool true" "$TARGET_TEMP_DIR/$FULL_PRODUCT_NAME.xcent"
fi
@insidegui
insidegui / PlatformViewRepresentable.swift
Created August 10, 2022 18:13
Protocol that automatically adds UIViewRepresentable or NSViewRepresentable conformance to implementers according to the current platform
import SwiftUI
#if os(iOS) || os(tvOS)
public typealias PlatformView = UIView
public typealias PlatformViewRepresentable = UIViewRepresentable
#elseif os(macOS)
public typealias PlatformView = NSView
public typealias PlatformViewRepresentable = NSViewRepresentable
#endif
@insidegui
insidegui / atoi.swift
Last active August 10, 2022 12:30
Implementation of the "atoi" function in Swift (just an exercise, not for use in production)
/**
Implementation of the "atoi" function in Swift.
This implementation is an exercise and should not be used in production,
Swift has built-in types and functions that can do this sort of conversion.
*/
/// Parses the input string as a 32-bit integer.
/// Returns `nil` if the input contains non-ASCII characters, or is not a valid number.
func myAtoi(_ input: String) -> Int32? {
/// The base ASCII code, where the numbers begin.
@insidegui
insidegui / Animation+Slow.swift
Created July 6, 2022 20:00
Handy debugging extension on SwiftUI's Animation
import SwiftUI
/// On macOS, modifying an Animation with .currentSpeed(), or using the .current static property
/// allows for easy animation debugging by holding down the Shift key when triggering the animation.
/// When the animation is triggered while the Shift key is pressed, it will be played in slow motion.
/// Using this extension has no effect when targeting other OSes or when building for release.
extension Animation {
static var currentSpeed: Double {
#if DEBUG
@insidegui
insidegui / FixSwiftUIMaterialInPreviews.m
Created May 13, 2022 21:27
Fixes SwiftUI's Material not being rendered correctly in Xcode previews
#if DEBUG
/*
This fixes SwiftUI previews not rendering translucent materials correctly by
swizzling a couple of properties on NSWindow.
Just drop into your project and add to the target being previewed (or something it links against).
Notice the #if DEBUG, so this code won't end up in release builds. It also checks for the
XCODE_RUNNING_FOR_PREVIEWS environment variable so that it won't affect regular debug builds of the app.
@insidegui
insidegui / SemanticVersion.swift
Created January 30, 2022 13:56
Simple Swift type for representing Major.Minor.Patch versions, useful for things such as comparing app versions against values from a backend
/*
Example:
let v1 = SemanticVersion(string: "1.0.0")
let v2 = SemanticVersion(string: "2.0.0")
print(v1 > v2) // false
print(v2 > v1) // true
@insidegui
insidegui / reloadplugins.sh
Created January 29, 2022 23:00
Make your Mac app's extensions immediately available on macOS with a run script build phase
# Add this to a "Run Script" build phase in your app's main target, as the last step.
# It will use the pluginkit command-line tool to force the plugin system on macOS to add your extensions to its database, making them available.
# I made this specifically for widgets, but it should work for pretty much any extension type (appex bundle).
find $CODESIGNING_FOLDER_PATH -name '*.appex' -exec pluginkit -a {} \;
@insidegui
insidegui / CodableReference.swift
Created January 4, 2022 18:03
Property wrapper that allows for properties to be encoded as references by ID and resolved while decoding.
protocol ReferenceEncodable: Identifiable {
static var referenceStorageKey: CodingUserInfoKey { get }
}
extension ReferenceEncodable {
static var referenceStorageKey: CodingUserInfoKey {
CodingUserInfoKey(rawValue: String(describing: Self.self) + "ReferenceStorage")!
}
}