无法识别的选择器调用“_setApplicationIsOpaque:”
Unrecognised selector invoking "_setApplicationIsOpaque:"
我正在尝试访问 iOS 私有函数 _setApplicationIsOpaque:
(仅供个人使用和测试)。
我已经实现了这段代码:
@_silgen_name("_setApplicationIsOpaque:") func _setApplicationIsOpaque(_ arg1: Bool)
let invokeSetApplicationIsOpaque: (Bool) -> Void = {
// The Objective-C selector for the method.
let selector: Selector = Selector(("_setApplicationIsOpaque:"))
guard case let method = class_getInstanceMethod(UIApplication.self, selector), method != nil else {
fatalError("Failed to look up \(selector)")
}
// Recreation of the method's implementation function.
typealias Prototype = @convention(c) (AnyClass, Selector, Bool) -> Void
let opaqueIMP = method_getImplementation(method)
let function = unsafeBitCast(opaqueIMP, to: Prototype.self)
// Capture the implemenation data in a closure that can be invoked at any time.
return{ arg1 in function(UIApplication.self, selector, arg1)}
}()
extension UIApplication {
func setApplicationIsOpaque(_ isOpaque: Bool) {
return invokeSetApplicationIsOpaque(isOpaque)
}
}
我在以下 Whosebug 问题 中找到了这种访问私有 iOS API 的方式
this file GitHub.
问题是 运行 我收到错误的应用程序
[UIApplication _setBackgroundStyle:]: unrecognized selector sent to class 0x10437f348
我找到了 UIApplication in this GitHub repository 的 iOS 私有 API 的 header。
- (void)_setApplicationIsOpaque:(BOOL)arg1;
是一个 实例 方法(属于 UIApplication
),如
初始连字符 -
。实例方法被发送到一个实例
class,而不是 class 本身。
Objective-C 方法是带有两个隐藏参数的 C 函数,比较
Messaging
在“Objective-C 运行时编程指南”中:
It also passes the procedure two hidden arguments:
- The receiving object
- The selector for the method
对于实例方法,“接收对象”是实例
消息被发送,在你的例子中是 UIApplication
实例。
(对于 class 方法 它将是 class 对象)。
因此 Swift 扩展方法 setApplicationIsOpaque
必须将 self
传递给闭包,并且必须将其传递为
实现方法的第一个参数:
let invokeSetApplicationIsOpaque: (UIApplication, Bool) -> Void = {
// The Objective-C selector for the method.
let selector = Selector(("_setApplicationIsOpaque:"))
guard case let method = class_getInstanceMethod(UIApplication.self, selector), method != nil else {
fatalError("Failed to look up \(selector)")
}
// Recreation of the method's implementation function.
typealias Prototype = @convention(c) (UIApplication, Selector, Bool) -> Void
let opaqueIMP = method_getImplementation(method)
let function = unsafeBitCast(opaqueIMP, to: Prototype.self)
// Capture the implemenation data in a closure that can be invoked at any time.
return { (appl, arg1) in function(appl, selector, arg1)}
}()
extension UIApplication {
func setApplicationIsOpaque(_ isOpaque: Bool) {
return invokeSetApplicationIsOpaque(self, isOpaque)
}
}
注意这里不需要_silgen_name
声明。
@_silgen_name("_setApplicationIsOpaque:") func _setApplicationIsOpaque(_ arg1: Bool)
将Swift函数绑定到全局符号“_setApplicationIsOpaque”,
这是不存在的。如果您要添加对该函数的调用
_setApplicationIsOpaque(true)
然后构建应用程序将失败并出现链接器错误:
Undefined symbols for architecture x86_64: "__setApplicationIsOpaque:"
我正在尝试访问 iOS 私有函数 _setApplicationIsOpaque:
(仅供个人使用和测试)。
我已经实现了这段代码:
@_silgen_name("_setApplicationIsOpaque:") func _setApplicationIsOpaque(_ arg1: Bool)
let invokeSetApplicationIsOpaque: (Bool) -> Void = {
// The Objective-C selector for the method.
let selector: Selector = Selector(("_setApplicationIsOpaque:"))
guard case let method = class_getInstanceMethod(UIApplication.self, selector), method != nil else {
fatalError("Failed to look up \(selector)")
}
// Recreation of the method's implementation function.
typealias Prototype = @convention(c) (AnyClass, Selector, Bool) -> Void
let opaqueIMP = method_getImplementation(method)
let function = unsafeBitCast(opaqueIMP, to: Prototype.self)
// Capture the implemenation data in a closure that can be invoked at any time.
return{ arg1 in function(UIApplication.self, selector, arg1)}
}()
extension UIApplication {
func setApplicationIsOpaque(_ isOpaque: Bool) {
return invokeSetApplicationIsOpaque(isOpaque)
}
}
我在以下 Whosebug 问题
问题是 运行 我收到错误的应用程序
[UIApplication _setBackgroundStyle:]: unrecognized selector sent to class 0x10437f348
我找到了 UIApplication in this GitHub repository 的 iOS 私有 API 的 header。
- (void)_setApplicationIsOpaque:(BOOL)arg1;
是一个 实例 方法(属于 UIApplication
),如
初始连字符 -
。实例方法被发送到一个实例
class,而不是 class 本身。
Objective-C 方法是带有两个隐藏参数的 C 函数,比较 Messaging 在“Objective-C 运行时编程指南”中:
It also passes the procedure two hidden arguments:
- The receiving object
- The selector for the method
对于实例方法,“接收对象”是实例
消息被发送,在你的例子中是 UIApplication
实例。
(对于 class 方法 它将是 class 对象)。
因此 Swift 扩展方法 setApplicationIsOpaque
必须将 self
传递给闭包,并且必须将其传递为
实现方法的第一个参数:
let invokeSetApplicationIsOpaque: (UIApplication, Bool) -> Void = {
// The Objective-C selector for the method.
let selector = Selector(("_setApplicationIsOpaque:"))
guard case let method = class_getInstanceMethod(UIApplication.self, selector), method != nil else {
fatalError("Failed to look up \(selector)")
}
// Recreation of the method's implementation function.
typealias Prototype = @convention(c) (UIApplication, Selector, Bool) -> Void
let opaqueIMP = method_getImplementation(method)
let function = unsafeBitCast(opaqueIMP, to: Prototype.self)
// Capture the implemenation data in a closure that can be invoked at any time.
return { (appl, arg1) in function(appl, selector, arg1)}
}()
extension UIApplication {
func setApplicationIsOpaque(_ isOpaque: Bool) {
return invokeSetApplicationIsOpaque(self, isOpaque)
}
}
注意这里不需要_silgen_name
声明。
@_silgen_name("_setApplicationIsOpaque:") func _setApplicationIsOpaque(_ arg1: Bool)
将Swift函数绑定到全局符号“_setApplicationIsOpaque”, 这是不存在的。如果您要添加对该函数的调用
_setApplicationIsOpaque(true)
然后构建应用程序将失败并出现链接器错误:
Undefined symbols for architecture x86_64: "__setApplicationIsOpaque:"