gpt4 book ai didi

swift - 关于SKStoreReviewController.requestReview()的回调

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:00 28 4
gpt4 key购买 nike

如果显示从 View Controller 启动的审核弹出窗口,由于缺少 SKStoreReviewController.requestReview( )

我想在审查弹出窗口关闭时调用 becomeFirstResponder()。有什么想法吗?

有没有办法扩展 SKStoreReviewController 并以某种方式添加回调?

最佳答案

警告这可能会在某个时候中断。

第 1 步:将此代码添加到您的 didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let windowClass: AnyClass = UIWindow.self

let originalSelector: Selector = #selector(setter: UIWindow.windowLevel)
let swizzledSelector: Selector = #selector(UIWindow.setWindowLevel_startMonitor(_:))

let originalMethod = class_getInstanceMethod(windowClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(windowClass, swizzledSelector)

let didAddMethod = class_addMethod(windowClass, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))

if didAddMethod {
class_replaceMethod(windowClass, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}

return true
}

第二步:添加这个类

class MonitorObject: NSObject {
weak var owner: UIWindow?

init(_ owner: UIWindow?) {
super.init()
self.owner = owner
NotificationCenter.default.post(name: UIWindow.didBecomeVisibleNotification, object: self)
}

deinit {
NotificationCenter.default.post(name: UIWindow.didBecomeHiddenNotification, object: self)
}
}

第 3 步:添加此 UIWindow 扩展

private var monitorObjectKey = "monitorKey"
private var partialDescForStoreReviewWindow = "SKStore"

extension UIWindow {
// MARK: - Method Swizzling
@objc func setWindowLevel_startMonitor(_ level: Int) {
setWindowLevel_startMonitor(level)

if description.contains(partialDescForStoreReviewWindow) {
let monObj = MonitorObject(self)
objc_setAssociatedObject(self, &monitorObjectKey, monObj, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}

第 4 步:将其添加到 Controller 的 ViewDidLoad 中您想要的地方

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeHiddenNotification(_:)), name: UIWindow.didBecomeHiddenNotification, object: nil)
}

第五步:为通知添加回调并检查关联对象是否匹配

@objc func windowDidBecomeHiddenNotification(_ notification: Notification?) {
if notification?.object is MonitorObject {
print("hello")
}
}

现在,当评论对话框关闭时,将触发通知并调用 'print("hello")。

关于swift - 关于SKStoreReviewController.requestReview()的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008845/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com