我使用Swift创建了一个本地模块,用于从React Native中调用iAP。
在Swift模块内,我试图存储从JS传入的回调以购买产品,
以便我可以在SKPaymentTransactionObserver委托中调用该回调。
然而,Objective C不支持可选项,这段代码会在大多数情况下崩溃。
我会收到"fatal error: unexpectedly found nil while unwrapping an Optional value"和"(@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.NSArray>)"错误信息。
以下是Swift类的代码:
以下是Swift类的代码:
@objc(StoreManager)
class StoreManager: NSObject {
// callbackholder initialized to an optional as .None with RCTResponseSenderBlock type
var purchaseCallbackHolder: RCTResponseSenderBlock? = .None
override init() {
super.init()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
@objc func purchaseProduct(productIdentifier: String, callback successCallback: RCTResponseSenderBlock) {
//omitted purchase code
// set purchaseCallbackHolder to callback passed in from JS
purchaseCallbackHolder = successCallback
}
}
extension StoreManager: SKPaymentTransactionObserver {
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break;
// omitted extra cases
default:
break;
}
}
// invoke callback waiting to be called
purchaseCallbackHolder?([])
purchaseCallbackHolder = .None
}
}
}
Objective C block description
/** RCTResponseSenderBlock
* The type of a block that is capable of sending a response to a bridged
* operation. Use this for returning callback methods to JS.
*/
typedef void (^RCTResponseSenderBlock)(NSArray *response);
有没有更安全的方法来存储Obj C块以便以后调用?
我真的被卡住了,非常感谢任何帮助!谢谢
更新 以下是堆栈跟踪:
AppName[1788:581487] *** -[StoreManager respondsToSelector:]: message sent to deallocated instance 0x14d43f80
(lldb) bt
* thread #4: tid = 0x8df6f, 0x25750ffe CoreFoundation`___forwarding___ + 530, queue = 'com.facebook.react.StoreManagerQueue', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)
* frame #0: 0x25750ffe CoreFoundation`___forwarding___ + 530
frame #1: 0x2567b298 CoreFoundation`_CF_forwarding_prep_0 + 24
frame #2: 0x2f103f1e StoreKit`__NotifyObserverAboutChanges + 66
frame #3: 0x2565bc08 CoreFoundation`CFArrayApplyFunction + 36
frame #4: 0x2f103ecc StoreKit`-[SKPaymentQueue _notifyObserversAboutChanges:sendUpdatedDownloads:] + 128
frame #5: 0x2f1028e8 StoreKit`-[SKPaymentQueue addPayment:] + 320
frame #6: 0x00106438 AppName`StoreManager.purchaseProduct(productIdentifier="iosmonthlysubscription799", successCallback=0x00106694 AppName`partial apply forwarder for reabstraction thunk helper from @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.NSArray>) -> (@unowned ()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>) -> (@unowned ()) with unmangled suffix ".40" at inAppPayments.swift, self=0x14d0a990) -> ()) -> () + 1168 at inAppPayments.swift:54
frame #7: 0x00106634 AppName`@objc StoreManager.purchaseProduct(String, callback : ([AnyObject]!) -> ()) -> () + 240 at inAppPayments.swift:0
frame #8: 0x25752664 CoreFoundation`__invoking___ + 68
frame #9: 0x256778bc CoreFoundation`-[NSInvocation invoke] + 292
frame #10: 0x2567b356 CoreFoundation`-[NSInvocation invokeWithTarget:] + 50
frame #11: 0x001672d6 AppName`-[RCTModuleMethod invokeWithBridge:module:arguments:](self=0x14fdba90, _cmd="invokeWithBridge:module:arguments:", bridge=0x14faa1a0, module=0x14d0a990, arguments=@"2 elements") + 1636 at RCTModuleMethod.m:489
frame #12: 0x001abaea AppName`-[RCTBatchedBridge _handleRequestNumber:moduleID:methodID:params:](self=0x14faa1a0, _cmd="_handleRequestNumber:moduleID:methodID:params:", i=6, moduleID=77, methodID=3, params=@"2 elements") + 926 at RCTBatchedBridge.m:987
frame #13: 0x001aae44 AppName`__33-[RCTBatchedBridge handleBuffer:]_block_invoke.452(.block_descriptor=<unavailable>) + 1028 at RCTBatchedBridge.m:915
frame #14: 0x00b9bba6 libdispatch.dylib`_dispatch_call_block_and_release + 10
frame #15: 0x00ba64aa libdispatch.dylib`_dispatch_queue_drain + 2014
frame #16: 0x00b9ede2 libdispatch.dylib`_dispatch_queue_invoke + 282
frame #17: 0x00ba799e libdispatch.dylib`_dispatch_root_queue_drain + 426
frame #18: 0x00ba77f0 libdispatch.dylib`_dispatch_worker_thread3 + 100
frame #19: 0x2547ee0c libsystem_pthread.dylib`_pthread_wqthread + 1024
frame #20: 0x2547e9fc libsystem_pthread.dylib`start_wqthread + 8