Skip to content

Instantly share code, notes, and snippets.

@tjluoma
tjluoma / macos-is-at-least.sh
Last active April 6, 2023 18:40
zsh script to compare macOS Versions - see https://rhymeswithdiploma.com/2020/07/10/macos-is-at-least/ for full explanation
#!/bin/zsh -f
# Purpose: Check to see if we are running on Big Sur
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2020-07-10
PATH="/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin"
# this will check to make sure `sw_vers` exists
@camyoh
camyoh / getIP.swift
Last active April 10, 2024 02:28
Get the IP on iphone, swift 5
let ipName = getIpAddress() ?? ""
func getIpAddress() -> String? {
var address : String?
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return nil }
guard let firstAddr = ifaddr else { return nil }
@SergLam
SergLam / UIDevice+Ext.swift
Created June 14, 2019 11:02
Detect iOS device model
public extension UIDevice {
enum DeviceModelName: String {
case undefined
case iPodTouch5
case iPodTouch6
case iPhone4
case iPhone4s
case iPhone5
@available(iOS 13.0, *)
func exampleCombineKVO () {
let article = Article(title: "Test", body: "Lorum ipsum")
// For a KeyPath 101, see https://www.swiftbysundell.com/posts/the-power-of-key-paths-in-swift
let keypath: ReferenceWritableKeyPath<Article,String> = \.title
// The new Combine stuff:
let sink = Subscribers.Assign(object: article, keyPath: keypath)
let source = Publishers.Future<String, Never> { subscriber in
@kemchenj
kemchenj / CodeTextField.swift
Last active December 11, 2023 07:19
CodeTextField
import UIKit
class CodeTextField: UITextField, UITextFieldDelegate {
let codeLength: Int
var characterSize: CGSize
var characterSpacing: CGFloat
let textPreprocess: (String) -> String
let validCharacterSet: CharacterSet
@DanielCardonaRojas
DanielCardonaRojas / UIScrollViewExtensions.swift
Created October 24, 2018 14:29
UIScrollView Extensions (scroll to subview and get relative offset of subview)
extension UIScrollView {
func scrollSubViewToTop(_ subview: UIView, offset: CGFloat, animated: Bool) {
let point = convert(subview.frame.origin, from: subview.superview ?? subview)
setContentOffset(CGPoint(x: 0, y: point.y - offset), animated: animated)
}
func viewPortOffset(of subview: UIView) -> CGFloat {
let point = convert(subview.frame.origin, from: subview.superview ?? subview)
return point.y - contentOffset.y
}
@novemberfiveco-gists
novemberfiveco-gists / WKCookieWebView.swift
Last active June 26, 2023 10:02
A WKWebView subclass that passes cookies after a 302 redirect response.
//
// WKCookieWebView.swift
//
// Created by Jens Reynders on 30/03/2018.
// Copyright (c) 2018 November Five
//
// 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
@enomoto
enomoto / swiftc_help.txt
Created June 18, 2018 20:37
$ swiftc -help
OVERVIEW: Swift compiler
USAGE: swiftc [options] <inputs>
MODES:
-dump-ast Parse and type-check input file(s) and dump AST(s)
-dump-parse Parse input file(s) and dump AST(s)
-dump-scope-maps <expanded-or-list-of-line:column>
Parse and type-check input file(s) and dump the scope map(s)
-dump-type-refinement-contexts
@implementation AppHostCookie
+ (NSMutableArray<NSString *> *)cookieJavaScriptArray
{
NSMutableArray<NSString *> *cookieStrings = [[NSMutableArray alloc] init];
//取出cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
@pkuecuekyan
pkuecuekyan / WKWebViewSizeToFit.swift
Last active May 19, 2024 21:53
Adjust height of WKWebView frame based on scrollHeight of the webView's content
// Since the WKWebView has no sizeToFit() method, increase the frame height of the webView to
// match the height of the content's scrollHeight
//
// The WKWebView's `navigationDelegate` property needs to be set for the delegate method to be called
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
if let height = result as? CGFloat {