Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to add a protocol to a Class signature in swift

I'm trying to create an in-app purchase in swift. In my class signature, I have the following:

class ViewController: UIViewController, UITextFieldDelegate, UIAlertViewDelegate,   
SKStoreProductViewControllerDelegate, SKPaymentTransactionObserver{

However, I get an error message: type "ViewController" does not conform to protocol: SKPaymentTransactionObserver

I've read this: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html and Conform to protocol in ViewController, in Swift

The SKSoreProductViewControllerDelegate works fine. What am I missing, please?

like image 407
krisacorn Avatar asked Dec 11 '22 04:12

krisacorn


2 Answers

Have you implemented the required methods in your class?

paymentQueue:updatedTransactions: and paymentQueue:updatedDownloads: are required methods and you will get a warning if they are not implemented.

like image 51
Dean Davids Avatar answered Mar 03 '23 20:03

Dean Davids


SKPaymentTransactionProtocol has these methods:

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) 
@optional func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
@optional func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!)
@optional func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!)
@optional func paymentQueue(queue: SKPaymentQueue!, updatedDownloads downloads: [AnyObject]!)

The first is a required method that your class has to implement in order to conform to the protocol. Add it to your ViewController and the error will disappear.

class ViewController: UIViewController, UITextFieldDelegate, UIAlertViewDelegate,   
SKStoreProductViewControllerDelegate, SKPaymentTransactionObserver{
    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!){/*...*/}
    /*...*/
}
like image 22
Connor Avatar answered Mar 03 '23 20:03

Connor