繁体   English   中英

在委托方法上添加完成处理程序

[英]Adding a completion handler on a delegate method

我很难理解完成处理程序。 我试图让一个函数(purchaseRequest)等待直到另一个函数(didReceiveResponse)未被我调用,但作为委托完成。 有人可以给我指出如何实现这一目标吗?

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    print("response received")
    if response.products.count != 0 {
        productsArray = response.products[0]
        print(productsArray)
    } else { print("No Products Found") }
}

func purchaseRequest() {
    requestProductInfo()

    //NEED TO HOLD UNTIL didReceiveResponse (which, as a delegate method, is not called by me) IS DONE.
        print("Product1: \(self.productsArray)")
    let aSC = UIAlertController(title: "Premium App Required", message: "Premium App is Required for this feature. Would you like to purchase it for $0.99?", preferredStyle: UIAlertControllerStyle.ActionSheet)
    let buyAction = UIAlertAction(title: "Purchase", style: UIAlertActionStyle.Default) { (action) -> Void in
        let payment = SKPayment(product: self.productsArray)
        SKPaymentQueue.defaultQueue().addPayment(payment)

    }
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
    }
    aSC.addAction(buyAction)
    aSC.addAction(cancelAction)
    self.presentViewController(aSC, animated: true, completion:  nil)
}

•推动进度微调器视图控制器

•在requestProductInfo()之后立即使purchaseRequest返回

•使用新方法在requestProductInfo()之后捕获代码

•在委托方法中, dispatch_async()分配到主队列并调用新方法

•在新方法中,弹出微调器视图控制器,然后执行高级购买舞蹈

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM