Skip to content

Instantly share code, notes, and snippets.

@algal
algal / HTTPBasicAuthenticationSessionTaskDelegate.swift
Last active October 6, 2021 07:33
HTTP Basic Authentication in iOS
/*
Everyone on Stack Overflow does HTTP Basic Authentication on iOS by manually
building the HTTP headers.
This amounts to re-implementing HTTP.
Why? The Cocoa Touch URL Loading System aleady knows HTTP, and you can
configure your URLSession to supply HTTP Basic Authentication credentials
like so.
import Foundation
/// Represents side effects needed to sync dst with src
enum Effect {
/// in dst, remove specified item in dst
case remove(dstIndex:Int)
/// in dst, add the specified item from src
case add(srcIndex:Int)
}
@algal
algal / SelfAssigningReferenceTypes.swift
Last active October 26, 2017 09:01
Brief exploration of Swift classes that assign to self via protocol extensions
/*
## background
When you write a function that mutates the var properties of a
mutable struct, this _creates a new instance_.[1] As a result,
a variable pointing to a value of this type before the mutation
will still be pointing to the old value after the mutation, as long
as you did not use that variable to do the mutation.
// known-good: Swift 3
import UIKit
/// for a UIView that can be populated via `configure`
protocol ConfigurableView : class {
associatedtype Value
func configure(_ value:Value)
}
/// for a UICollectionViewCell wrapping a UIView
/**
Generic CollectionViewCell, which can be specialized to wrap a UIView.
To use this, you can simply define a typealias like so:
```
class FooView : UIVIew { /* ... */ }
typealias FooCell = WrappedCollectionViewCell<FooView>
```
@algal
algal / ScaleAspectFitImageView.swift
Last active September 24, 2023 10:19
UIImageView subclass that works with Auto Layout to express its desired aspect ratio
import UIKit
// known-good: Xcode 8.2.1
/**
UIImageView subclass which works with Auto Layout to try
to maintain the same aspect ratio as the image it displays.
This is unlike the usual behavior of UIImageView, where the
@algal
algal / URL+Data.swift
Created February 23, 2017 19:45
Extract the data from certain "data:" URIs
// known-good Xcode 8.2.1
extension URL
{
/**
Returns the data from a URI with the "data:" scheme, which includes the optional "base64" annotation, and which has a mediatype that is encoded in ASCII. As described in https://tools.ietf.org/html/rfc2397.
*/
var asData:Data?
{
guard
let scheme = self.scheme, scheme == "data",
import Foundation
// machine, version, release, nodename, sysname
extension utsname : CustomStringConvertible {
static var `default`:utsname {
var x = utsname()
uname(&x)
return x
}
@algal
algal / CircleView.swift
Last active October 10, 2024 04:53
Custom UIView hosting a CAShapeLayer
// known-good: Xcode 8.2.1, Swift 3, 2017-02-08
/**
Draws a circle, centered in the view, with the specified radius
*/
class CircleView: UIView
{
// API
var circleRadius:CGFloat = 15 {
didSet { self.setNeedsLayout() }
@algal
algal / Extra Logging for My Great App.mobileconfig
Created February 1, 2017 04:32 — forked from zwaldowski/Extra Logging for My Great App.mobileconfig
Apple Configuration Profile for Logging in iOS 10 and macOS Sierra
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- iOS 10, macOS Sierra, and friends bring a new logging subsystem that's
supposed to scale from the kernel, up to frameworks, and up to apps. It defaults
to a more regimented, privacy-focused approach that large apps and complex
systems need.
It, along with Activity Tracing introduced in iOS 8 and macOS Yosemite and the
Console app in macOS Sierra, hope to help you graduate from caveman debugging to