关于SKStoreReviewController.requestReview()的回调

时间:2019-01-02 15:19:37

标签: swift becomefirstresponder skstorereviewcontroller

如果显示了从视图控制器启动的审阅弹出窗口,则由于缺少SKStoreReviewController.requestReview()的回调函数而无法使用该弹出窗口时,无法将窗口焦点切换回视图控制器。 / p>

在关闭评论弹出窗口时,我想打电话给becomeFirstResponder()。有想法吗?

是否可以扩展 SKStoreReviewController 并以某种方式添加回调?

2 个答案:

答案 0 :(得分:0)

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

步骤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
}

第2步:添加此类

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步:将此添加到您控制器的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)
}

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

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

现在,当关闭查看对话框时,将触发通知,并会调用'print(“ hello”)。

答案 1 :(得分:0)

有时,iOS应用程序失去了响应者链,就像上面显示StoreKit提示的示例一样。我们可以做的是检测UIApplication.sendAction中的此类事件,然后通过becomeFirstResponder重新激活第一响应者链。 UIKit将重新建立第一个响应者链,我们可以重新发送同一事件。

class MyApplication: UIApplication {

    func reactivateResponderChainWhenFirstResponderEventWasNotHandled() {
        becomeFirstResponder()
    }

    override func sendAction(_ action: Selector, to target: Any?, from sender: Any?, for event: UIEvent?) -> Bool {
        let wasHandled = super.sendAction(action, to: target, from: sender, for: event)
        if wasHandled == false, target == nil {
            reactivateResponderChainWhenFirstResponderEventWasNotHandled()
            return super.sendAction(action, to: target, from: sender, for: event)
        }
        return wasHandled
    }
}

这对我在iOS 13上有效,不需要任何私有API访问。