5

如果从视图控制器启动的评论弹出窗口出现,则由于缺少SKStoreReviewController.requestReview().

我想becomeFirstResponder()在审查弹出窗口被驳回时打个电话。任何的想法?

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

4

2 回答 2

2

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

第 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 步:将此添加到您想要的控制器的 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")。

于 2019-01-15T22:10:59.033 回答
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 访问。

于 2020-03-06T06:36:13.907 回答