Skip to content

Instantly share code, notes, and snippets.

View LeeKahSeng's full-sized avatar

Lee Kah Seng LeeKahSeng

View GitHub Profile
@LeeKahSeng
LeeKahSeng / swift-photospicker-full.swift
Created July 23, 2022 13:37
Full sample code for "How to Use the SwiftUI PhotosPicker" (https://swiftsenpai.com/development/swiftui-photos-picker/)
import SwiftUI
import PhotosUI
struct ContentView: View {
@State private var selectedItem: PhotosPickerItem? = nil
@State private var selectedImageData: Data? = nil
var body: some View {
@LeeKahSeng
LeeKahSeng / dynamic-dispatch-generic-full.swift
Last active August 2, 2022 15:00
Full sample code for article "How to Achieve Dynamic Dispatch Using Generic Protocols in Swift 5.7" (https://swiftsenpai.com/uncategorized/dynamic-dispatch-with-generic-protocols)
protocol Vehicle {
associatedtype FuelType: Fuel
var name: String { get }
func startEngin()
func fillGasTank(with fuel: FuelType)
}
@LeeKahSeng
LeeKahSeng / hijack-webview-nav-full.swift
Created February 16, 2022 14:06
Full sample code for article "How to Hijack WKWebView Navigation Actions" (https://swiftsenpai.com/development/hijack-wkwebview-navigation/)
import UIKit
import WebKit
// MARK: - Code related to "How to Hijack WKWebView Navigation Actions" starts here.
// https://swiftsenpai.com/development/hijack-wkwebview-navigation/
class ColorViewController: UIViewController {
var screenColor: UIColor?
@LeeKahSeng
LeeKahSeng / webview-inject-javascript-full.swift
Last active February 27, 2023 17:41
Sample code for "Injecting JavaScript Into Web View In iOS" (https://swiftsenpai.com/development/web-view-javascript-injection/)
import UIKit
import WebKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Configure the web view for JavaScript injection
configureWebView()
@LeeKahSeng
LeeKahSeng / KeychainHelper.swift
Last active June 19, 2024 19:59
Swift simple keychain helper class for iOS and macOs
// MIT License
//
// Copyright (c) 2023 Lee Kah Seng
//
// 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:
@resultBuilder
struct StringBuilder {
static func buildBlock(_ components: String...) -> String {
let filtered = components.filter { $0 != "" }
return filtered.joined(separator: "⭐️")
}
static func buildOptional(_ component: String?) -> String {
return component ?? ""
struct SFSymbolNameContentConfiguration: UIContentConfiguration, Hashable {
var name: String?
func makeContentView() -> UIView & UIContentView {
// Initialize an instance of SFSymbolNameContentView
return SFSymbolNameContentView(configuration: self)
}
func updated(for state: UIConfigurationState) -> Self {
@LeeKahSeng
LeeKahSeng / Google-Sign-In-Integration-11.swift
Created June 8, 2020 09:18
View controller implementation for Google sign-in integration
import UIKit
import GoogleSignIn
class ViewController: UIViewController {
var signInButton: UIButton!
var signOutButton: UIButton!
var greetingLabel: UILabel!
override func viewDidLoad() {
@LeeKahSeng
LeeKahSeng / Google-Sign-In-Integration-04.swift
Created June 8, 2020 09:11
Full Implementation of AppDelegate.swift for Google sign-in integration
import UIKit
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize Google sign-in
@LeeKahSeng
LeeKahSeng / Decode-Dynamic-Keys-JSON-Final.swift
Last active June 4, 2023 14:04
Decode and Flatten JSON with Dynamic Keys Using Swift Decodable (https://swiftsenpai.com/swift/decode-dynamic-keys-json/)
import Foundation
let jsonString = """
{
"S001": {
"firstName": "Tony",
"lastName": "Stark"
},
"S002": {
"firstName": "Peter",