Skip to content

Instantly share code, notes, and snippets.

View mbernson's full-sized avatar
👨‍💻
Coding...

Mathijs Bernson mbernson

👨‍💻
Coding...
View GitHub Profile
@mbernson
mbernson / AppDelegate.swift
Last active April 17, 2025 14:52
How to adopt the UIScene lifecycle in an iOS app
//
// AppDelegate.swift
//
// Copyright © 2024 Q42. All rights reserved.
//
import UIKit
import FirebaseCore
import FirebaseCrashlytics
import Factory
@mbernson
mbernson / shabadge.yaml
Last active December 12, 2024 09:24 — forked from Nizzle/shabadge.yaml
ESPhome config for SHA2017 badge (https://wiki.sha2017.org/w/Projects:Badge)
esphome:
name: sha2017-badge
friendly_name: SHA2017 Badge
esp32:
board: esp32-pro
# Enable logging
logger:
@mbernson
mbernson / PageControl.swift
Last active December 10, 2024 11:14
UIPageControl for use with SwiftUI. It has a two-way binding to a generic selection value.
import SwiftUI
import UIKit
struct PageControl<SelectionValue: Hashable>: UIViewRepresentable {
let items: [SelectionValue]
@Binding var selection: SelectionValue?
func makeUIView(context: Context) -> UIPageControl {
let pageControl = UIPageControl()
pageControl.pageIndicatorTintColor = .gray
@mbernson
mbernson / create_simulator.sh
Created December 2, 2024 12:56
Create a custom iOS simulator on the command line
SIMULATOR_NAME="Custom Simulator"
DEVICE_ID="com.apple.CoreSimulator.SimDeviceType.iPhone-15"
RUNTIME_ID="com.apple.CoreSimulator.SimRuntime.iOS-17-2"
xcrun simctl create "$SIMULATOR_NAME" "$DEVICE_ID" "$RUNTIME_ID"
@mbernson
mbernson / NetworkConnectivityAsyncStream.swift
Created November 28, 2024 13:33
Monitor network connectivity in Swift using an async stream
import Foundation
import Network
enum NetworkConnectivity {
case online
case offline
case unknown
}
let networkConnectivity = AsyncStream(NetworkConnectivity.self) { continuation in
@mbernson
mbernson / action.yaml
Created September 4, 2024 12:03
Set up SSH keys in GitHub Actions workflow
# Put this in `.github/actions/auth-setup/action.yaml`
name: Setup correct ssh auth
description: Installs keys, configures .ssh/config
inputs:
spm-private-key:
required: true
description: The private key to pull private Swift packages
fastlane-match-private-key:
required: true
@mbernson
mbernson / gist:e830f34d1bcd88b8e7a97d21ca9dce24
Last active May 15, 2024 07:21
Compile libssh2 from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64)
#!/usr/bin/env zsh
set -e
# This script compiles libssh2 from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64).
# How to use it: download or clone libssh2. Put this script in the root of the repository and run it.
SDK=macosx
SDK_VERSION="14.0"
@mbernson
mbernson / MACAddress.swift
Last active December 2, 2024 13:47
MACAddress value type in Swift, for storing a MAC address. Its rawValue is of type Data, and it provides serialization and deserialization.
//
// MACAddress.swift
//
//
// Created by Mathijs on 23/01/2024.
//
import Foundation
/// Model for a standard MAC (Media Access Control) address, which consists of 6 bytes
@mbernson
mbernson / build_apple.sh
Last active May 15, 2024 07:21
Compile OpenSSL from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64)
#!/usr/bin/env zsh
set -e
# This script compiles OpenSSL from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64).
# How to use it: download or clone OpenSSL. Put this script in the root of the repository and run it.
CROSS_COMPILE=`xcode-select --print-path`/Toolchains/XcodeDefault.xctoolchain/usr/bin/
CROSS_TOP=`xcode-select --print-path`/Platforms/MacOSX.platform/Developer
CROSS_SDK=MacOSX.sdk
@mbernson
mbernson / Alert+Error.swift
Last active January 25, 2024 13:55
Handling of (non-fatal) errors in SwiftUI
import SwiftUI
extension View {
/// Presents an alert when an error is present.
func alert<E: Error>(_ titleKey: LocalizedStringKey, error: Binding<E?>, buttonTitleKey: LocalizedStringKey = "Oke") -> some View {
modifier(ErrorAlert(error: error, titleKey: titleKey, buttonTitleKey: buttonTitleKey))
}
}
private struct ErrorAlert<E: Error>: ViewModifier {