在委托方法上添加完成处理程序
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 [=10=].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
return
• 在新方法
中捕获 requestProductInfo()
之后的代码
• 在委托方法中,dispatch_async()
到主队列并调用新方法
• 在新方法中,弹出微调器视图控制器,然后进行高级购买舞蹈
我很难理解完成处理程序。我正在尝试让一个函数 (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 [=10=].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
return
• 在新方法
中捕获requestProductInfo()
之后的代码
• 在委托方法中,dispatch_async()
到主队列并调用新方法
• 在新方法中,弹出微调器视图控制器,然后进行高级购买舞蹈