Skip to content

Instantly share code, notes, and snippets.

View ethanhuang13's full-sized avatar

Ethan Huang ethanhuang13

View GitHub Profile
// iOS 17's JSONEncoder is rarely producing a different key sorting order(< 5%), which is ok for JSON itself, but will cause flaky tests.
// It wasn't happened pre-iOS 17.
// https://mastodon.social/@ethanhuang13/110904915670262949
import XCTest
final class JSONEncoderTests: XCTestCase {
func testEncode() {
struct Model: Codable {
var a: String = ""
@ethanhuang13
ethanhuang13 / ErrorAlerts.swift
Created June 4, 2022 07:07
SwiftUI .alert(item:) for multiple types of Error
//
// ContentView.swift
// ErrorAlert
//
// Created by Ethan Huang on 2022/6/4.
//
import SwiftUI
enum AError: Error {
@ViewBuilder
private func build<#name#>(_ viewStore: ViewStoreType) -> some View {
}
// A challenge from https://twitter.com/hanyu_chen_ios/status/1395533041989079045
// Screenshots of performance tests https://twitter.com/ethanhuang13/status/1395577548151541760?s=21
import XCTest
let maxValue = 100
let minValue = 0
final class ClampTests: XCTestCase {
func testMeasureNaiveCompare() throws {
@ethanhuang13
ethanhuang13 / VirtualKeyboard.swift
Last active February 7, 2024 05:58
MacBook Air M1 Zhuyin Keyboard written with SwiftUI ~=300 LOC, < 4hrs
//
// VirtualKeyboard.swift
// MacBook Air M1 Zhuyin Keyboard
// Written with SwiftUI ~=300 LOC, < 4hrs
// Created by Ethan Huang on 2021/1/13.
// Twitter: @ethanhuang13
import SwiftUI
struct VirtualKeyboard: View {
@ethanhuang13
ethanhuang13 / FacebookAuth.swift
Last active October 3, 2024 12:26
FacebookAuth is for iOS app developers who need to support Facebook login but don't want to use the official SDK
//
// FacebookAuth.swift
// GitHub: ethanhuang13
// Twitter: @ethanhuang13
import AuthenticationServices
import SafariServices
/*
Updated:

Keybase proof

I hereby claim:

  • I am ethanhuang13 on github.
  • I am ethanhuang13 (https://keybase.io/ethanhuang13) on keybase.
  • I have a public key ASDAEzUKl1dvIut56bNB61xAdNlHIdUIFYlJ2bJ3k8SlAAo

To claim this, I am signing this object:

@ethanhuang13
ethanhuang13 / dear$FirstName.md
Created August 8, 2019 09:18
Important Updates to CNCopyCurrentnetworkInfo API in iOS 13

Dear $firstName,

As we announced at WWDC19, we're making changes to further protect user privacy and prevent unauthorized location tracking. Starting with iOS 13, the CNCopyCurrentNetworkInfo API will no longer return valid Wi-Fi SSID and BSSID information. Instead, the information returned by default will be:

SSID: “Wi-Fi” or “WLAN” (“WLAN" will be returned for the China SKU) BSSID: "00:00:00:00:00:00"

If your app is using this API, we encourage you to adopt alternative approaches that don’t require Wi-Fi or network information. Valid SSID and BSSID information from CNCopyCurrentNetworkInfo will still be provided to VPN apps, apps that have used NEHotspotConfiguration to configure the current Wi-Fi network, and apps that have obtained permission to access user location through Location Services.

Test your app on the latest iOS 13 beta to make sure it works properly. If your app requires valid Wi-Fi SSID and BSSID information to function, you can do the following:

@ethanhuang13
ethanhuang13 / TrackerTests.swift
Created August 5, 2019 08:42
Tracker API Poll
let tracker = Tracker()
// Which one do you prefer? Consider labels, indendentation, readability...
// 1 func trackEvent(_ event: Event, _ parameters: EventParameter...)
tracker.trackEvent(.viewItem,
.itemId("12345"),
.itemName("An Item"),
.list("List name"))
// 2. func track(event: Event, parameters: EventParameter...)
tracker.track(event: .viewItem, parameters:
@ethanhuang13
ethanhuang13 / HexColor.swift
Created January 2, 2019 06:44
@dynamicMemberLookup example
// An (useless) example of Swift 4.2 @dynamicMemberLookup attributes.
// let color1 = HexColor().cbcbcb
// let color2 = HexColor().000000 // This can also be built. Will be treat as "000000"
import Foundation
@dynamicMemberLookup
struct HexColor {
subscript(dynamicMember member: String) -> UIColor? {
do {