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
@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
@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 / 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 / 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 / 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 / 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
#if MOCK_URL_PROTOCOL
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [MockURLProtocol.self]
let urlSession = URLSession(configuration: configuration)
#else
let urlSession = URLSession.shared
#endif
let employeeInfo = """
{
"employeeID":156789,
"name": "Harris Smith"
}
"""
MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse.init(url: request.url!, statusCode: 200, httpVersion: "2.0", headerFields: nil)!
return (response,employeeInfo.data(using: .utf8)!)
}
class EmployeeInfoRequest {
#if MOCK_URL_PROTOCOL
class iMockURLProtocol: MockURLProtocol {
override func startLoading() {
iMockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse.init(url: request.url!, statusCode: 200, httpVersion: "2.0", headerFields: nil)!
return (response,mockEmployeeInfoData)
}
super.startLoading()
protocol APIRequest {
func mockURLProtocolClass() -> AnyClass?
}
extension APIRequest {
func mockURLProtocolClass() -> AnyClass? {
return nil
}
}