Skip to content

Instantly share code, notes, and snippets.

View AlexFlyce's full-sized avatar

Alessandro Francucci AlexFlyce

View GitHub Profile
struct VpnChecker {
private static let vpnProtocolsKeysIdentifiers = [
"tap", "tun", "ppp", "ipsec", "utun"
]
static func isVpnActive() -> Bool {
guard let cfDict = CFNetworkCopySystemProxySettings() else { return false }
let nsDict = cfDict.takeRetainedValue() as NSDictionary
guard let keys = nsDict["__SCOPED__"] as? NSDictionary,
@AlexFlyce
AlexFlyce / XCTUnwrap_1.swift
Created September 25, 2019 14:27
XCTUnwrap_1.swift
func testFirstNameNotEmpty() throws {
let forenames: [String] = customer.forenames
let firstName = try XCTUnwrap(forenames.first)
XCTAssertFalse(firstName.isEmpty)
}
@AlexFlyce
AlexFlyce / circularImage.swift
Created May 22, 2018 14:31
ART001-circularImage.swift
extension UIImage {
class func circularImage(from image: UIImage, size: CGSize) -> UIImage? {
let scale = UIScreen.main.scale
let circleRect = CGRect(x: 0, y: 0, width: size.width * scale, height: size.height * scale)
UIGraphicsBeginImageContextWithOptions(circleRect.size, false, scale)
let circlePath = UIBezierPath(roundedRect: circleRect, cornerRadius: circleRect.size.width/2.0)
circlePath.addClip()
@AlexFlyce
AlexFlyce / AsyncImageView.swift
Created May 22, 2018 12:47
ART001-AsyncImageView.swift
import UIKit
class AsyncImageView: UIView {
private var _image: UIImage?
var image: UIImage? {
get {
return _image
}
@AlexFlyce
AlexFlyce / decodedImage.swift
Last active May 22, 2018 14:35
ART001-decodedImage.swift
extension UIImage {
class func decodedImage(_ image: UIImage) -> UIImage? {
guard let newImage = image.cgImage else { return nil }
// To optimize this, you can some cache control.
let colorspace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(data: nil,
width: newImage.width,
height: newImage.height,
bitsPerComponent: 8,
bytesPerRow: newImage.width * 4,
@AlexFlyce
AlexFlyce / DroppingFramesHelper.swift
Last active May 22, 2018 09:24
ART001-DroppingFramesHelper.swift
class DroppingFramesHelper: NSObject {
private var firstTime: TimeInterval = 0.0
private var lastTime: TimeInterval = 0.0
func activate() {
let link = CADisplayLink(target: self, selector: #selector(update(link:)))
link.add(to: .main, forMode: .commonModes)
}