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 / 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 February 5, 2024 16:20
MACAddress value type in Swift, for storing a MAC address
//
// 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 {
@mbernson
mbernson / ContentView.swift
Last active July 14, 2023 17:04
SwiftUI custom segmented control
import SwiftUI
struct ContentView: View {
@State var selected = "Foo"
var body: some View {
SegmentedControl(options: ["Foo", "Bar", "Baz"], selectedOption: $selected) { label in
Text(label)
}
}
@mbernson
mbernson / ContentView.swift
Last active January 31, 2023 08:17
Button that starts/stops a screen recording and present the user with the Apple-provided preview
// How to use it:
import SwiftUI
struct ContentView: View {
var body: some View {
RecordScreenButton()
}
}
@mbernson
mbernson / arrays.c
Created December 14, 2022 12:19
C array on the heap
#include <stdio.h>
#include <stdlib.h>
typedef struct Point {
int x;
int y;
} Point;
int main() {
int count = 2;
@mbernson
mbernson / MarkdownExample.swift
Created August 2, 2022 13:35
Example of custom styles using Markdown support in SwiftUI Text.
import SwiftUI
import PlaygroundSupport
import Foundation
struct MarkdownView: View {
var body: some View {
Text(try! AttributedString(markdown: "This is a **basic** _string_. Italic bits are red. All the **bold** bits are _coloured_ **green**! And [this is a link!](https://q42.nl/).", customBoldColor: .green, customItalicColor: .red))
.font(.title)
}
}
@mbernson
mbernson / ScrollViewWrapper.swift
Last active April 26, 2022 13:07
UIScrollView wrapped for SwiftUI, with support for pull to refresh using the refreshable modifier
//
// ScrollViewWrapper.swift
//
// Created by Mathijs Bernson on 10/03/2022.
// Copyright © 2022 Q42. All rights reserved.
//
import SwiftUI
import UIKit