ios - react native : Swift Module store objective C block for callback by delegate

标签 ios objective-c swift swift2 react-native

我在 Swift 中创建了一个本地模块,以从 React Native 调用 iAP。 在 Swift 模块中,我试图存储从 JS 传递给 purchaseProduct 的回调 这样我就可以在 SKPaymentTransactionObserver 委托(delegate)中调用该回调 但是 Objective C 不喜欢可选项,并且此代码崩溃了 10 次中的 9 次

我得到 fatal error: unexpectedly found nil while unwrapping an Optional value(@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.NSArray>)

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 block 以便稍后调用?

我真的很难过,感谢任何帮助!谢谢

更新 这是堆栈跟踪:

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

最佳答案

需要在 deinit 方法中移除事务观察器:

  override init() {
    super.init()
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
  }


  deinit {
    if SKPaymentQueue.canMakePayments() {
      SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
    }
  }

我在堆栈跟踪中查找了我遇到的错误,其中之一是:

StoreKit__NotifyObserverAboutChanges

向我指出这篇文章: A single bug in my StoreKit code that lost me 90% of IAP sales

关于ios - react native : Swift Module store objective C block for callback by delegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38288538/

相关文章:

ios - UIScrollView 滚动事件阻塞 UIView 动画

iOS 10,应用程序在前台时显示本地通知?

iOS - XML pretty-print

objective-c - iOS 编程与 Web 数据库接口(interface)

swift - 在工作表内的表单中使用选取器不起作用

objective-c - 我的第一个 block 实验给了我 EXC_BAD_ACCESS 我不知道为什么

iphone - iOS 5 隐藏 tabBarItem 文本标签

ios - 从 iOS 应用程序在 youtube 上上传视频?

ios - 确定设备的 3D 触摸可用性

ios - 在Swift中合并两个结构数组