Skip to content

Instantly share code, notes, and snippets.

View insidegui's full-sized avatar

Guilherme Rambo insidegui

View GitHub Profile
@insidegui
insidegui / ScrollViewOffsetModifier.swift
Created July 20, 2021 20:28
A SwiftUI ViewModifier that can be used to read a ScrollView's offset and store it into a @State property of the view
struct ScrollViewOffsetPreferenceKey: PreferenceKey {
static var defaultValue: CGPoint = .zero
static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) {
value = nextValue()
print("value = \(value)")
}
typealias Value = CGPoint
@insidegui
insidegui / WebCacheCleaner.swift
Created September 14, 2016 23:12
Clear WKWebView's cookies and website data storage, very useful during development.
import Foundation
import WebKit
final class WebCacheCleaner {
class func clean() {
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("[WebCacheCleaner] All cookies deleted")
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
@insidegui
insidegui / gist:a18124c0c573a4eb656f5c485ea7dae4
Last active March 29, 2024 07:46
Unofficial documentation for the iCloud Apple device image URL format
https://statici.icloud.com/fmipmobile/deviceImages-9.0/iPhone/iPhone9,4-2-3-0/online-infobox__3x.png
A B C D E F G
A: deviceImages version seems to determine the format of the image specifier (C, D, E, F)
B: device marketing name
C: device model identifier
D: color cover glass (front color)
1 - Black
2 - White
E: device enclosure color (back color)
@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 / 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")!
}
}
@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 / lsremovearchives.sh
Created December 21, 2021 18:28
Remove Xcode app archives from macOS LaunchServices database
#!/bin/bash
# Recursivelly removes all apps from your Xcode archives from the LaunchServices database, preventing them from being used for widgets, launch at login, etc.
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -R -f -u $HOME/Library/Developer/Xcode/Archives
@insidegui
insidegui / EncodableValue.swift
Created January 15, 2017 02:40
A better way to "store value types" in NSUserDefaults or NSUbiquitousKeyValueStore
/**
A better way to "store value types" in NSUserDefaults or NSUbiquitousKeyValueStore
*/
import Foundation
/// ValueCoder is a class that can encode values of a specific type via NSCoding
protocol ValueCoder: NSObjectProtocol, NSCoding {
/// The type this coder can encode/decode
associatedtype ValueType
@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 / Data+Compression.swift
Created July 21, 2020 15:01
A wrapper for compression/decompression of Swift Data using Apple's Compression framework.
/// Created by Gui Rambo
/// This wraps Apple's Compression framework to compress/decompress Data objects.
/// It will use Compression's modern API for iOS 13+ and its old API for older versions.
/// For more information, check out Apple's documentation: https://developer.apple.com/documentation/compression
/*
Copyright 2020 Guilherme Rambo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: