Skip to content

Instantly share code, notes, and snippets.

View quangDecember's full-sized avatar
💭
my new year resolution is to not force unwrapped optionals

Quang quangDecember

💭
my new year resolution is to not force unwrapped optionals
View GitHub Profile
#if MOCK_URL_PROTOCOL
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [MockURLProtocol.self]
let urlSession = URLSession(configuration: configuration)
#else
let urlSession = URLSession.shared
#endif
@quangDecember
quangDecember / MockURLProtocol.swift
Created July 18, 2019 08:05
Mock URLProtocol that help mock server response
import Foundation
class MockURLProtocol: URLProtocol {
static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data) )?
override class func canInit(with request: URLRequest) -> Bool {
return true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
return request
@quangDecember
quangDecember / BaseTestCase+waitforexistence.swift
Created May 15, 2019 23:33
wait for existence in UI testing
import XCTest
class BaseTestCase: XCTestCase {
let app = XCUIApplication()
// ...
func waitforExistence(element: XCUIElement, file: String = #file, line: UInt = #line) {
let exists = NSPredicate(format: "exists == true")
@quangDecember
quangDecember / URLComponents+initURL.swift
Created May 15, 2019 19:28
Construct URL using URLComponents, ensure safe path, percent encoding
extension URLComponents {
init(scheme: String = "https", host: String, path: [String], queries: [URLQueryItem]?) {
self = URLComponents.init()
self.scheme = scheme
self.host = host
var p = path
p.insert("/", at: 0)
self.path = NSString.path(withComponents: p)
self.queryItems = queries
}
@quangDecember
quangDecember / build-binary-universal-framework.sh
Last active June 18, 2021 09:55
Build Binary framework script for iOS, included fix for Xcode 10.2 headers, originally by DJ110
# Xcode 10.2
# please use within Xcode environment (Build Phases -> Run Script or Scheme -> Post Actions)
xcodebuild -version
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
env > env.txt
# Step 1. Build Device and Simulator versions
xcodebuild -project "${PROJECT_NAME}.xcodeproj" -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
@quangDecember
quangDecember / iOS-notifications-action-minimal-code.swift
Created April 2, 2019 09:10
minimalist version of handle notification actions
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}
return true
}
@quangDecember
quangDecember / iOS-SDK-modularization-a-survey.md
Created March 21, 2019 12:01
iOS SDK Modularization: A survey

iOS SDK modularization: a survey

SDK in the survey:

  • Fabric Crashlytics
  • Firebase
  • Facebook SDK: Swift & Objective-C

Fabric Crashlytics

Crashlytics is a crash reporting library for mobile apps, a close-sourced binary framework.

@quangDecember
quangDecember / KeyboardTableView.swift
Created January 11, 2019 14:36 — forked from douglashill/KeyboardTableView.swift
A UITableView that allows navigation and selection using a hardware keyboard.
// Douglas Hill, December 2018
import UIKit
/// A table view that allows navigation and selection using a hardware keyboard.
/// Only supports a single section.
class KeyboardTableView: UITableView {
// These properties may be set or overridden to provide discoverability titles for key commands.
var selectAboveDiscoverabilityTitle: String?
var selectBelowDiscoverabilityTitle: String?
@quangDecember
quangDecember / GoogleTestViewController.swift
Created January 7, 2019 09:46
UI Testing with Lunch framework
class GoogleTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let webView = WKWebView.init(frame: self.view.frame)
webView.load(URLRequest.init(url: URL.init(string: "https://www.google.com")! ))
self.view.addSubview(webView)
}
@quangDecember
quangDecember / WebCacheCleaner.swift
Created December 27, 2018 10:00 — forked from insidegui/WebCacheCleaner.swift
Clear WKWebView's cookies and website data storage, very useful during development.
import Foundation
import WebKit
final class WebCacheCleaner {
class func clean() {
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("[WebCacheCleaner] All cookies deleted")
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in