How to create instance of Storefront.PaymentSettings in swift NSObject subclass

Highlighted
New Member
4 0 0

Hi there,  I'm building a react-native mobile iOS app and I know nothing about Swift. I'm following a tutorial to integrate swift into react-native project and trying to expose a Swift method to JavaScript. 

 

 

I succeeded in fetching some shop data following the mobile-buy-sdk-ios GitHub page:

https://github.com/Shopify/mobile-buy-sdk-ios

 

Here is how I fetched data in .swift file: (this works well)

The `@objc` and `NSObject` are said to be required when exposing Swift method to JS.

 

import Foundation
import MobileBuySDK

@objc(GraphQuery)
class GraphQuery: NSObject {
  
  let client = Graph.Client(
    shopDomain:"aaa.myshopify.com",
    apiKey:"f45b11c03d5b894709ebuhuis57d5e59"
  )

@objc
  func queryShop() {
    let query = Storefront.buildQuery { $0
      .shop { $0
        .collections(first:10){ $0
          .edges{ $0
            .node { $0
              .id()
              .title()
            }
          }
        }
      }
    }
    
    let task = client.queryGraphWith(query){ response, error in
      if let res = response {
        let name = res.shop.collections.edges
        print("name is: \(name)")
      } else {
        print("Query failed: \(error)")
      }
    }
    task.resume()
  }
}

 

 

 

However, I'm stuck on the card vaulting where it throws error:

 

import Foundation
import MobileBuySDK

@objc(GraphQuery)
class GraphQuery: NSObject {

  let cardClient : Card.Client // once added, throws 'Class 'GraphQuery' has no initializers'
  let settings : Storefront.PaymentSettings

 @objc
  func cardVault() {
    let creditCard = Card.CreditCard(firstName: "An", lastName: "Y", number: "15467862389", expiryMonth: "08", expiryYear: "19",verificationCode:"565")
    let task = cardClient.vault(creditCard, to: settings.cardVaultUrl) { token, error in
      if let token = token {
        print("card vault is: \(token)")
      }else {
        print("card vault failed: \(error)")
      }
    }
    task.resume()
  }
}

 

 

I tried to fix this 'no initializers' error by providing an 

 

override init(settings: Storefront.PaymentSettings, cardClient: Card.Client) {
    self.settings = settings
    self.cardClient = cardClient
    super.init()
  }

But it then throws another error: Initializer does not override a designated initializer from its superclass

 

What is the correct way to create these two instances in the .swift file so that the method can be exposed to JavaScript?

let cardClient : Card.Client
let settings : Storefront.PaymentSettings
0 Likes