gpt4 book ai didi

ios - SKProductsRequest 不会在 Swift 2 中调用我的回调

转载 作者:行者123 更新时间:2023-11-28 06:36:34 27 4
gpt4 key购买 nike

我按照不同的教程在 Swift 2 的 iOS 应用程序中检索产品信息。但我无法让它工作:-/

我使下面的代码尽可能简单,我只调用了 requestProductsSimple 方法,我希望调用 productRequest()。运行前我有 0 个错误和 0 个警告。

但输出只显示:

请求开始

结束

几秒钟后我得到了 EXEC_BAD_ACCESS。这是代码。我希望有人能提供帮助。

谢谢:)

import UIKit
import StoreKit

class ViewController: UIViewController {

@IBOutlet var mainView : UIView?

override func loadView() {
super.loadView()
}

override func viewDidLoad() {
super.viewDidLoad()

let shop = IAPHelper()
shop.requestProductsSimple()

print("ended")

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}


class IAPHelper: NSObject, SKProductsRequestDelegate {

override init() {

}

func requestProductsSimple() {

let productIdentifiers: Set<String> = ["my_product_id"]
let productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest.delegate = self
productsRequest.start()

print("request started")

}

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {

print("received")

}

func request(request: SKRequest, didFailWithError error: NSError) {

print("request failed")

}

}

最佳答案

因为你的 shop 是一个局部变量,它会在 viewDidLoad 返回后被释放。这是在异步操作完成之前,当它尝试调用现在发布的完成方法时,您会遇到段错误。

将您的 shop 移动到属性中,以便在 viewDidLoad 返回后保留强引用

关于ios - SKProductsRequest 不会在 Swift 2 中调用我的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946365/

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