Skip to content

Instantly share code, notes, and snippets.

View steipete's full-sized avatar

Peter Steinberger steipete

View GitHub Profile
extension UIHostingController {
convenience public init(rootView: Content, ignoreSafeArea: Bool) {
self.init(rootView: rootView)
if ignoreSafeArea {
disableSafeArea()
}
}
func disableSafeArea() {
@steipete
steipete / FirebaseCoordinator.swift
Created April 13, 2020 13:38
If you're as confused as I am that there's an API for custom options, yet Google still requires a file named GoogleService-Info.plist in your app, here's some swizzling that fixes that for ya. All Swift :)
class FirebaseCoordinator {
static let shared = FirebaseCoordinator()
static let initialize: Void = {
/// We modify Google Firebase (and eventually Analytics) to load the mac-specific plist at runtime.
/// Google enforces that we have a file named "GoogleService-Info.plist" in the app resources.
/// This is unfortunate since we need two different files based on iOS and Mac version
/// One solution is a custom build step that copies in the correct file:
/// https://stackoverflow.com/questions/37615405/use-different-googleservice-info-plist-for-different-build-schemes
/// However, this is basically impossible since Catalyst doesn't set any custom build variables, so detection is extremely difficult.
/// We swizzle to modify the loading times.
@steipete
steipete / RandomColor.swift
Created April 6, 2021 17:20
Random Color for SwiftUI
extension Color {
/// Return a random color
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
@steipete
steipete / URLCacheTest.swift
Last active April 4, 2024 18:16
Using URLCache with download tasks (NSURLCache & NSURLSessionDownloadTask)
import Foundation
import os.log
class URLCacheTest {
let logger = Logger(subsystem: "URLCacheTest", category: "main")
// HTTP HEADERS:
// Date: Wed, 04 Nov 2020 11:13:24 GMT
// Server: Apache
// Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
@steipete
steipete / UIKitLegacyDetector.m
Last active March 12, 2024 13:57
A simple way to detect at runtime if we're running in UIKit legacy mode or the new "flat" variant. Written for our PDF iOS SDK (http://pspdfkit.com), where the precompiled binary needs to detect at runtime in what variant it's running. Want more stuff like that? Follow me on Twitter: http://twitter.com/steipete
// Taken from http://PSPDFKit.com. This snippet is under public domain.
#define UIKitVersionNumber_iOS_7_0 0xB57
BOOL PSPDFIsUIKitFlatMode(void) {
static BOOL isUIKitFlatMode = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// We get the modern UIKit if system is running >= iOS 7 and we were linked with >= SDK 7.
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0) {
isUIKitFlatMode = (NSVersionOfLinkTimeLibrary("UIKit") >> 16) >= UIKitVersionNumber_iOS_7_0;
}
@steipete
steipete / PSPDFUIKitMainThreadGuard.m
Last active March 10, 2024 19:23
This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…
// Taken from the commercial iOS PDF framework http://pspdfkit.com.
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
// Licensed under MIT (http://opensource.org/licenses/MIT)
//
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit.
#import <objc/runtime.h>
#import <objc/message.h>
@steipete
steipete / PSPDFEnvironment.m
Last active March 5, 2024 09:15
Example for DISPATCH_SOURCE_TYPE_MEMORYPRESSURE (Mac only, since 10.9) ... Since we share code between iOS and mac, I'm trying to be a good system citizen and reimplement the equivalent of UIApplicationDidReceiveMemoryWarningNotification on the Mac.
NSString *const PSPDFApplicationDidReceiveMemoryWarningNotification = @"PSPDFApplicationDidReceiveMemoryWarningNotification";
// Test with sudo memory_pressure -l critical. Don't forget to re-set afterwards!
__attribute__((constructor)) static void PSPDFInstallLowMemoryNotificationWarningMac(void) {
static dispatch_source_t source;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(source);
@steipete
steipete / View.swift
Created April 4, 2021 13:43
Accept dropping a file onto SwiftUI (both iOS and macOS)
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
if let loadableProvider = providers.first(where: { $0.canLoadObject(ofClass: URL.self) }) {
_ = loadableProvider.loadObject(ofClass: URL.self) { fileURL, _ in
if let fileURL = fileURL, fileURL.pathExtension.lowercased() == "zip" {
self.logger.info("Dropped \(fileURL.path)")
DispatchQueue.main.async {
importer.open(zipArchiveURL: fileURL)
}
}
}
@steipete
steipete / ReactNative.js
Created January 29, 2015 10:33
Facebook Group App's ReactNative
This file has been truncated, but you can view the full file.
__SSTOKENSTRING = "@generated SignedSource<<dbe928e1275c495c1922c1bf063ffb70>>";
! function(e) {
function t(e) {
function t() {
var t = Array.prototype.map.call(arguments, function(e) {
if (null == e) return null === e ? "null" : "undefined";
if ("string" == typeof e) return '"' + e + '"';
try {
return JSON.stringify(e)
} catch (t) {
@steipete
steipete / PSPDFThreadSafeMutableDictionary.m
Created December 2, 2013 09:00
Dictionary subclass whose primitive operations are thread safe. Most of the time you don't want locking at the collection level, but there are valid use cases for this, and it's interesting how to properly subclass the NSDictionary class cluster. We're using SpinLocks here, since it's highly unlikely that any of the operation locks for more than…
@interface PSPDFThreadSafeMutableDictionary : NSMutableDictionary
@end
#import "PSPDFThreadSafeMutableDictionary.h"
#import <libkern/OSAtomic.h>
@implementation PSPDFThreadSafeMutableDictionary {
OSSpinLock _lock;
NSMutableDictionary *_dictionary; // Class Cluster!