Skip to content

Instantly share code, notes, and snippets.

@koenpunt
Last active May 2, 2016 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenpunt/cf8130bdfc432d31136b to your computer and use it in GitHub Desktop.
Save koenpunt/cf8130bdfc432d31136b to your computer and use it in GitHub Desktop.
UIRefreshControl override so that app correctly idles while UI testing

UIRefreshControl+testing.swift

You'll need to a -DTESTING compile flag to the configuration you use for testing to enable this extension only for test builds.

Imgur

//
// UIRefreshControl+testing.swift
// Kakhiel
//
// Created by Koen Punt on 08-02-16.
// Copyright © 2016 Fetch!. All rights reserved.
//
import Foundation
import UIKit
/** testing Extends UIRefreshControl
*/
#if TESTING
extension UIRefreshControl {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
// make sure this isn't a subclass
if self !== UIRefreshControl.self {
return
}
dispatch_once(&Static.token) {
let originalSelector = Selector("_setRefreshControlState:notify:")
let swizzledSelector = Selector("kp__setRefreshControlState:notify:")
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
// MARK: - Method Swizzling
// Overrides so that app idles correctly when running UITests
func kp__setRefreshControlState(state: Int, notify: Bool) {
print("state: \(state) notify: \(notify)")
}
}
#endif
@WestonHanners
Copy link

do you have any idea how to do this with the Swift 2.2 selector syntax?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment