Forum Archive

Help with objc_util delegates

peterdougstuart

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()
JonB

The method name is
productsRequest:didReceiveResponse:
which turns into

productsRequest_didReceiveResponse_

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

peterdougstuart

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.