Skip to content

Instantly share code, notes, and snippets.

@FabP93
FabP93 / SSL_Pinning.swift
Last active May 4, 2023 09:18
How to retrieve the SecTrust from the network call and compare it with your own certificate
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard
challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust
else {
completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
return
}
guard
@FabP93
FabP93 / Hash-Part5.swift
Created April 19, 2020 17:06
Hash example
extension Key: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(self.id)
}
func hash(into hasher: inout Hasher) {
hasher.combine(self.id)
}
}
@FabP93
FabP93 / Hash-Part4.swift
Created April 19, 2020 17:04
Hash example
let key1 = Key(id: 1, title: "Key1")
let key2 = Key(id: 1, title: "Key2")
dict[key1] = 1058
dict[key2] = 368
print(dict.count) // 2
print(key1 == key2) // false
print(key1.hashValue == key2.hashValue) // true
@FabP93
FabP93 / Hash-Part3.swift
Created April 19, 2020 17:02
Hashable example
struct Key {
let id: Int
let title: String
}
extension Key: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(self.id)
}
}
@FabP93
FabP93 / Hash-Part2.swift
Last active April 19, 2020 16:56
Hasher, how it works
// Using hasher. Actually, you will never use it directly.
var hasher = Hasher()
hasher.combine("Hello")
hasher.combine("World")
hasher.combine(2020)
let hashValue = hasher.finalize()
// This is the correct way to implement Hashable.
extension YourStruct: Hashable {
func hash(into hasher: inout Hasher) {
@FabP93
FabP93 / Hash-Part1.swift
Created April 19, 2020 16:52
Hashable in swift 1
/// A point in an x-y coordinate system.
struct GridPoint {
var x: Int
var y: Int
}
extension GridPoint: Hashable {
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
@FabP93
FabP93 / GenericsAndSubtypesPart3.swift
Last active April 16, 2020 19:05
Mixing generics and Subtypes Part 3
let pushups = Array<Pushup>()
// This doesn't raise any error because built-in
// generic types are covariant.
var exercises: Array<Exercise> = pushups
@FabP93
FabP93 / GenericsAndSubtypesPart2.swift
Last active April 16, 2020 19:03
Mixing generics and Subtypes Part 2
struct Activity<T> {}
var pushup = Activity<Pushup>()
// You can't do this, it raises the error:
// Cannot assign value of type 'Activity<Pushup>' to type 'Activity<Exercise>'
var activity: Activity<Exercise> = pushup
@FabP93
FabP93 / GenericsAndSubtypesPart1.swift
Last active April 16, 2020 19:02
Mixing generics and Subtypes Part 1
class Exercise {
func perform() {}
func stop() {}
}
class Pushup: Exercise {
override func perform() {}
override func stop() {}
}
@FabP93
FabP93 / ShareViewController.swift
Last active March 22, 2021 13:20
ShareExtension - ShareViewController part 2
private func handleSharedFile() {
// extracting the path to the URL that is being shared
let attachments = (self.extensionContext?.inputItems.first as? NSExtensionItem)?.attachments ?? []
let contentType = kUTTypeData as String
for provider in attachments {
// Check if the content type is the same as we expected
if provider.hasItemConformingToTypeIdentifier(contentType) {
provider.loadItem(forTypeIdentifier: contentType,
options: nil) { [unowned self] (data, error) in
// Handle the error here if you want