xcode 应用内购买测试期间的 EXC_BAD_ACCESS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7146545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EXC_BAD_ACCESS during in app purchase test
提问by Barry Nailor
Running a test for my in app purchase (first time doing in app purchases). I get EXC_BAD_ACCESS on the third line of this code:
为我的应用内购买运行测试(第一次进行应用内购买)。我在这段代码的第三行得到 EXC_BAD_ACCESS:
SKPayment *payment = [SKPayment paymentWithProduct:electronicProd];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
The is under an IBAction for a button. electronicPack is declared in the header as a SKProduct. Threw some NSLogs in the productsRequest didReceiveResponse, and when the product was requested (in the viewDidLoad) and they showed it was correctly fetching the product and storing it in electronicPack. Defined electronicPack as [[request.products] objectAtIndex:0] in the didReceiveResponse page. So yea. Thats where im at, dont know what to do. Any help is appreciated.
位于按钮的 IBAction 下。电子包在标头中声明为 SKProduct。在 productsRequest didReceiveResponse 中抛出了一些 NSLog,当产品被请求时(在 viewDidLoad 中),他们显示它正确地获取了产品并将其存储在电子包中。在 didReceiveResponse 页面中将电子包定义为 [[request.products] objectAtIndex:0]。所以是的。那就是我所在的地方,不知道该怎么办。任何帮助表示赞赏。
UPDATE: FIXED accidentally left in code that was adding an extra transaction observer lol
更新:修复意外留在添加额外事务观察器的代码中,哈哈
采纳答案by andreamazz
You need to retain the object that you are creating
您需要保留正在创建的对象
- (void)viewDidLoad {
//... stuff
SKProduct* electronicProduct = //...
[electronicProduct retain];
//... otherstuff
}
viewDidLoad is wrapped by the system in a autorelease pool, paymentWithProduct: returns an autorelease object. When viewDidLoad is done, all autorelease object are released, that's why you get a bad memory access when you try to access to it later.
viewDidLoad 被系统包裹在一个自动释放池中,paymentWithProduct: 返回一个自动释放对象。当 viewDidLoad 完成时,所有自动释放对象都被释放,这就是为什么当您稍后尝试访问它时会获得错误的内存访问。
回答by Mr. T
I've had the same issue, my solution was to call
我遇到了同样的问题,我的解决方案是打电话
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
on leaving the In-App Store in my App. Maybe it will help someone in the future.
在我的应用程序中离开应用程序内商店。也许它会在未来帮助某人。
回答by Zorayr
Seems like the issue is from trying to add a transaction observer before removing the previous one. Add the following to your controller to fix this issue:
似乎问题在于在删除前一个之前尝试添加一个事务观察者。将以下内容添加到您的控制器以解决此问题:
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
Hope this helps!
希望这可以帮助!
回答by imike
Mr.T answer is the best solution!
Mr.T回答是最好的解决方案!
In swift I call this removeTransactionObserver here:
在 swift 我称之为 removeTransactionObserver 在这里:
deinit {
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
}
回答by Shayno
I had the same error, quite simple to solve really. In my header file I had a SKProduct declared:
我有同样的错误,真的很容易解决。在我的头文件中,我声明了一个 SKProduct:
@property SKProduct *product;
I just changed it to:
我只是将其更改为:
@property (retain) SKProduct *product;
and it all works fine. Hope this helps someone.
一切正常。希望这可以帮助某人。
回答by Skoua
If you're creating a dedicated class for SKPaymentTransactionObserver
don't forget that it must be retained.
如果您正在创建一个专用类,SKPaymentTransactionObserver
请不要忘记必须保留它。
I got the error before realizing this, here's an example in AppDelegate
:
在意识到这一点之前我收到了错误,这是一个例子AppDelegate
:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var myTransactionObserver: MyTransactionObserver!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// don't
let myTransactionObserver = MyTransactionObserver()
// do
myTransactionObserver = MyTransactionObserver()
SKPaymentQueue.default().add(myTransactionObserver)
return true
}
// ...
}
回答by Usman Nisar
I Have the same issue, my solution was to call [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
我有同样的问题,我的解决方案是调用 [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
on store observer transaction finish/failed/completed callbacks
在商店观察者交易完成/失败/完成回调