omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Help with objc_util delegates

    Pythonista
    2
    3
    2350
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • peterdougstuart
      peterdougstuart last edited by ccc

      Hi

      I'm trying to get some objc_util code working that involves a delegate, however I just can't seem to find the right syntax for expressing the delegate method.

      Can anyone help? Code is below and the delegate I'm targeting is defined here:
      https://developer.apple.com/documentation/storekit/skproductsrequestdelegate/1506070-productsrequest

      I'm marshalling access to the objc_util code through a singleton which is initiated within the app using InApp. initialize()

      BTW the code is running in the AppTemplate in Xcode.

      Thanks!

      from objc_util import *
      
      def PurchaseController_fetchAvailableProducts(_self, _cmd):
      
          obj = ObjCInstance(_self)
          
          sk_class = ObjCClass("SKProductsRequest")
          products_request = sk_class.alloc().init(productIdentifiers=[InApp.PRODUCT_ID])
          
          products_request.delegate = obj
          products_request.start()
      
      def PurchaseController_canMakePurchases(_self, _cmd):
          sk_class = ObjCClass("SKProductsRequest")
          return sk_class.canMakePayments()
      
      def PurchaseController_purchase(_self, _cmd):
          pass
      
      def PurchaseController_purchaseMyProduct(_self, _cmd):
          pass
      
      def productsRequest_request_didReceiveResponse(_self, _cmd, request, response):
          #defined here: https://developer.apple.com/documentation/storekit/skproductsrequestdelegate/1506070-productsrequest
          InApp.receive_products(response)
      
      class InApp:
      
          PRODUCT_ID = "Purchase001"
          Instance = None
          
          @classmethod
          def initialize(cls):
              cls.Instance = InApp()
      
          @classmethod
          def receive_products(cls, response):
              cls.Instance.products_received = True
              self.products = ['Dummy1', 'Dummy2']
          
          def __init__(self):
              
              self.products = []
              self.products_received = False
              
              ObjCClass('NSBundle').bundleWithPath_('/System/Library/Frameworks/StoreKit.framework').load()
              
              superclass = ObjCClass("NSObject")
              
              methods = [PurchaseController_fetchAvailableProducts,
                         PurchaseController_canMakePurchases,
                         productsRequest_request_didReceiveResponse]
              
              protocols = ['SKProductsRequestDelegate', 'SKPaymentTransactionObserver']
              
              purchase_controller_class = create_objc_class('PurchaseController', superclass, methods=methods, protocols=protocols)
      
              purchase_controller = purchase_controller_class.alloc().init()
      
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        The method name is
        productsRequest:didReceiveResponse:
        which turns into

        productsRequest_didReceiveResponse_

        you seem to have an extra request in the name...

        1 Reply Last reply Reply Quote 1
        • peterdougstuart
          peterdougstuart last edited by

          Many thanks that's done it. I was a bit thrown by the syntax of how an objective-c delegate maps to the python method, but I think I get it now. I know precious little objective-c which isn't helping!

          Thanks again.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors