Skip to content

Instantly share code, notes, and snippets.

View davidleee's full-sized avatar

David Lee davidleee

View GitHub Profile
@davidleee
davidleee / WKWebView-and-AVPlayer.md
Created September 30, 2022 01:43 — forked from 4np/WKWebView-and-AVPlayer.md
Detecting Video Playback inside a WKWebView

Detecting Video Playback inside a WKWebView

Observing video playback inside a black-boxed WKWebView is difficult as you don't have direct access to the video player.

Another complicating matter is that depending on the video, the video might be played using a HTML5 video player, while others might launch the native AVPlayerViewController for playback. While it might be possible to detect HTML5 based playback by injecting custom JavaScript using a WKUserContentController, this is not the approach we will follow in the document as these depend on what HTML5 Video Player is involved and is, as such, not a generic solution.

Making sure AVKit is used

@davidleee
davidleee / MeasureAppStartupTime.swift
Created September 25, 2022 00:48 — forked from DenTelezhkin/MeasureAppStartupTime.swift
Measure iOS app startup time, in seconds, from the time user tapped an icon on the home screen (using time, when app process was created). Swift 4.
// Returns number of seconds passed between time when process was created and function was called
func measureAppStartUpTime() -> Double {
var kinfo = kinfo_proc()
var size = MemoryLayout<kinfo_proc>.stride
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
sysctl(&mib, u_int(mib.count), &kinfo, &size, nil, 0)
let start_time = kinfo.kp_proc.p_starttime
var time : timeval = timeval(tv_sec: 0, tv_usec: 0)
gettimeofday(&time, nil)
let currentTimeMilliseconds = Double(Int64(time.tv_sec) * 1000) + Double(time.tv_usec) / 1000.0
@davidleee
davidleee / ExportTool.cs
Created July 19, 2022 06:17 — forked from larryhou/ExportTool.cs
Command-line building tool for Unity project
using UnityEditor;
using System.Collections.Generic;
// Put this memu command script into Assets/Editor/
class ExportTool
{
static void ExportXcodeProject ()
{
EditorUserBuildSettings.SwitchActiveBuildTarget (BuildTarget.iOS);
func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
if animated {
UIView.transitionWithView(window, duration: 0.5, options: .TransitionCrossDissolve, animations: {
let oldState: Bool = UIView.areAnimationsEnabled()
UIView.setAnimationsEnabled(false)
self.window!.rootViewController = rootViewController
UIView.setAnimationsEnabled(oldState)
}, completion: { (finished: Bool) -> () in
if completion {
completion!()
@davidleee
davidleee / Fonts.swift
Created November 25, 2020 02:56 — forked from feighter09/Fonts.swift
Set global font for iOS app in one place
// MARK: - Swizzling
extension UIFont {
class var defaultFontFamily: String { return "Georgia" }
override public class func initialize()
{
if self == UIFont.self {
swizzleSystemFont()
}
}
@davidleee
davidleee / realmMock.js
Created March 6, 2019 10:05 — forked from hyb175/realmMock.js
Realm Mock for jest
// https://github.com/realm/realm-js/issues/370#issuecomment-270849466
export default class Realm {
constructor(params) {
this.schema = {};
this.callbackList = [];
this.data = {};
this.schemaCallbackList = {};
params.schema.forEach((schema) => {
this.data[schema.name] = {};
});
@davidleee
davidleee / BOApplePencilReachability.swift
Created January 5, 2019 03:46
Apple Pencil Detecter by Daniel Bocksteger
/*
Copyright 2017 Daniel Bocksteger
BOApplePencilReachability.swift
from https://gitlab.com/DanielBocksteger/BOApplePencilReachability
Inspiration from Answer on the following question
https://stackoverflow.com/questions/32542250/detect-whether-apple-pencil-is-connected-to-an-ipad-pro/41264961#41264961
@davidleee
davidleee / firstDifferenceBetweenStrings.swift
Created August 16, 2018 03:02 — forked from kristopherjohnson/firstDifferenceBetweenStrings.swift
Swift code to find differences between strings and display them in a readable way, useful for displaying unit test results
import Foundation
/// Find first differing character between two strings
///
/// :param: s1 First String
/// :param: s2 Second String
///
/// :returns: .DifferenceAtIndex(i) or .NoDifference
public func firstDifferenceBetweenStrings(s1: NSString, s2: NSString) -> FirstDifferenceResult {
@davidleee
davidleee / String+ChineseDetect.swift
Created August 10, 2018 01:33
An extension for String to detect if it contains Chinese characters
extension String {
var containsChinese: Bool {
for substring in self {
if substring.isChinese {
return true
}
}
return false
}
}
import UIKit
extension String {
var glyphCount: Int {
let richText = NSAttributedString(string: self)
let line = CTLineCreateWithAttributedString(richText)
return CTLineGetGlyphCount(line)
}