I'm trying to get this to work, but I must have made a mistake somewhere. This is the Delegate class (snipped down) as per the C headers:
#protocol PaymentEngineMiddlewareDelegate <NSObject>
#required
/*!
#brief Delegate method for startTransaction.
#param transResponse - Contains all the transaction response info from the gateway
*/
-(void)transactionComplete :(PaymentEngineTransactionResponse *)transResponse;
#optional
/*!
#brief Delegate method for device connected. The method gets called when the device is connected
*/
-(void)deviceConnected;
/*!
#brief Delegate method for device disconnected. The method gets called when the device is disconnected
*/
-(void)deviceDisconnected;
#end
For this, I have:
// #interface PaymentEngineMiddlewareDelegate : NSObject
[BaseType(typeof(NSObject))]
[Model]
[Protocol]
interface PaymentEngineMiddlewareDelegate
{
[Abstract]
[Export("transactionComplete:")]
void TransactionComplete(PaymentEngineTransactionResponse transResponse);
[Export("deviceConnected")]
void DeviceConnected();
[Export("deviceDisconnected")]
void DeviceDisconnected();
}
Then for the PaymentEngineMiddleware class, the header has this:
#interface PaymentEngineMiddleware : NSObject
+ (PaymentEngineMiddleware *) getInstance;
#property(nonatomic, strong) id<PaymentEngineMiddlewareDelegate> delegate;
//------------------------------Device Methods------------------------------
/*!
#brief This method must be called before starting any transaction. Use it to connect to a device.
#param deviceName The name of the device such as icmp or castle
#param setDelegate Sets to self
*/
-(void)setDevice :(NSString *)deviceName :(id)setDelegate;
#end
And for that, I have:
// #interface PaymentEngineMiddleware : NSObject
[BaseType(typeof(NSObject))]
[Protocol]
interface PaymentEngineMiddleware
{
// +(id)getInstance;
[Static]
[Export("getInstance")]
PaymentEngineMiddleware GetInstance { get; }
[Export("delegate", ArgumentSemantic.Retain)]
PaymentEngineMiddlewareDelegate Delegate { get; set; }
[Export("setDevice:setDelegate:")]
void SetDevice(string deviceName, PaymentEngineMiddlewareDelegate setDelegate);
}
In code, when I try to call the "middleware.SetDevice('name', this);" where this is a class that inherits from the delegate, I get the exception.
Is there anything obvious that I missed or got wrong?
Your selector for setDevice method is wrong.
The proper selector for -(void)setDevice :(NSString *)deviceName :(id)setDelegate method is setDevice::. This is because the second parameter in this method doesn't have an external name.
The final entry for your binding should look like this
[Export("setDevice::")]
void SetDevice(string deviceName, PaymentEngineMiddlewareDelegate setDelegate);
You can read more about how the methods signatures in Objective-C are constructed in the Apple docs.
Also look in the Storyboard file for the component action in properties which is not implemented in the code.
in my case I have given touch insideup action for button in storyboard file but not implemented that in a code. after implementing this issue is no more.
Related
This is my Swift class :
class MyClass : NSObject {
public var inAppMessagesController: MPInAppMessagesController!
fun myFunction() {
self.inAppMessagesController.inAppInteractionDelegate = self // Error in this line - Cannot assign value of type 'MyClass' to type 'MPInAppMessageControllerDelegate?'
}
}
extension MyClass : MPInAppMessageControllerDelegate {
// Functions
}
As stated in comments, this is the error -
Cannot assign value of type 'MyClass' to type
'MPInAppMessageControllerDelegate?'
inAppInteractionDelegate in Objective-C class MPInAppMessagesController :
#interface MPInAppMessagesController : NSObject
#property (nonatomic, weak, nullable) id <MPInAppMessageControllerDelegate> inAppInteractionDelegate;
#end
MPInAppMessageControllerDelegate declared in MPInAppMessagesController.h :
#protocol MPInAppMessageControllerDelegate<NSObject>
// Functions
#end
The only missing part is you need to include this class inside the bridging file
#import "MPInAppMessagesController.h"
Look here to a SwiftObjc
Hello I want to use this method inside my swift class, already create the header, but I only manage to access the method (setApiKey). this is the code in objective c
#import <Foundation/Foundation.h>
#class CLQResponseHeaders, CLQError;
#class CLQToken;
NS_ASSUME_NONNULL_BEGIN
#interface Culqi : NSObject
/**
* gets singleton object.
* #return singleton
*/
+ (Culqi *_Nonnull)sharedInstance;
+ (void)setApiKey:(NSString *_Nonnull)apiKey;
#pragma mark - Tokens
- (void)createTokenWithCardNumber:(NSString *_Nonnull)cardNumber
cvv:(NSString *_Nonnull)cvv
expirationMonth:(NSString *_Nonnull)expirationMonth
expirationYear:(NSString *_Nonnull)expirationYear
email:(NSString *_Nonnull)email
metadata:(NSDictionary * _Nullable)metadata
success:(void (^_Nullable)(CLQResponseHeaders *_Nonnull responseHeaders, CLQToken * _Nonnull token))success
failure:(void (^_Nullable)(CLQResponseHeaders *_Nonnull responseHeaders, CLQError * _Nonnull businessError, NSError * _Nonnull error))failure;
#end
NS_ASSUME_NONNULL_END
My swift code is
import Foundation
import UIKit
class RegisterController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//THIS METHOD CALL SUCCESS
Culqi.setApiKey("")
}
}
Update: the method setApiKey is accessible, what happens is that I can not call the method "createTokenWithCardNumber"
Declare your function/method setApiKey in header file of your Objective-C class.
// Move following line/code to header file (.h) of your objective-c class
+ (void)setApiKey:(NSString *_Nonnull)apiKey;
Your method createTokenWithCardNumber is an instance method (not a class method), you need to create an instance of your class to access it. Try this,
let c = Culqi()
c.createTokenWithCardNumber(#<your parameter arguments>#)
// or use shared instance
Culqi.sharedInstance().createTokenWithCardNumber(#<your parameter arguments>#)
When I assign value to age, I get this error:
#interface Person : NSObject
{
int age;
}
-(void)setAge;
#end
I tried to use self.age, yet it did not work
Here is my .m file:
#implementation Person
-(void)setAge(int)value
{
age = value;
}
#end
I tried several differnet things. ..I get this error when I type this: age = value; do you know why this is?
You should add:
-(void)setAge:(int)value;
In the header file because the current method specification you have there doesn't have a parameter.
Your method in the implementation should also have the same spec as it's missing a colon currently.
You have actually declared one method (-setAge) and implemented another (-setAge:, note the colon). You should really declare age as a property and avoid explicit ivars as much as possible. Also, I hope you have properly formatted the class in your real code.
#interface Person : NSObject
#property (nonatomic) int age;
#end
#implementation Person
-(void)setAge:(int)value
{
_age = value;
}
#end
Note that it is no longer necessary to explicitly #synthesize properties, and they automatically synthesize with an underscored ivar.
I am a newbie on Monotouch. Recently, I am working on a Monotouch binding project that binds a custom iOS framework that developed myself into a .NET framework library. I follow the instructions on Xamarin but currently I am having an issue that cannot be resolved. This is my code.
**HEADER FILE IN OBJECTIVE C**
*GRG.h*
#interface GRG: NSObject {}
// Shared instance
+ (GRG*) sharedG;
// Preference class
#property (nonatomic, readonly) GRGPreferences *preferences;
// Driver version
#property (readonly,copy) NSString* driverVersion;
// More parameters...
#end
*GRGPreferences.h*
#interface GRGPreferences : NSObject <GRGPreferencesProtocol>{}
// Enable DEBUG
#property BOOL debugEnabled;
// More parameters...
#end
*GRGPreferencesProtocol.h*
#protocol GRGPreferencesProtocol <NSObject>
// More parameters...
#end
I convert my header file into this
**API DEFINITION**
[BaseType (typeof (NSObject))]
interface GRG
{
[Static][Export("sharedG")]
GRG SharedG{ get; }
[Export("preferences")]
GRGPreferences Preferences{ get;}
[Export("driverVersion", ArgumentSemantic.Copy)]
string DriverVersion {get;}
}
[BaseType (typeof (GRGPreferencesProtocol))]
public interface GRGPreferences
{
[Export("debugEnabled")]
bool DebugEnabled{ get; set;}
}
[BaseType(typeof (NSObject))]
[Model]
public interface GRGPreferencesProtocol
{}
After that, I created a test app on mono to test the newly created library and get access to the values I created. However, I got an error.
Console.WriteLine(GRG.sharedG.DriverVersion);
- This works fine. It returns the proper value.
GRGPreferences pref = GRG.SharedG.Preferences;
- Error : "Cannot cast from source type to destination type."
Console.WriteLine(GRG.sharedG.Preferences.DebugEnabled);
- Error : "Cannot cast from source type to destination type."
Can anyone please help me?
From a quick look I think this is what you want:
[BaseType (typeof (NSObject))]
public interface GRGPreferences : GRGPreferencesProtocol {
Your GRGPreferences type inherits from NSObject while implementing the protocol you want.
Building a wrapper for an iOS library. So far so good. Except in a method that returns an object. Method always returns NSObject instead. The same story is in delegate that has an object parameter .
For example,TCConnection is defined in Monotouch binding project as
//#interface TCConnection : NSObject
[BaseType (typeof(NSObject))]
public interface TCConnection
{
[ExportAttribute("state")]
int state {get;}
//...
}
class TCDevice is defined as follows
//#interface TCDevice : NSObject
[BaseType (typeof(NSObject))]
public interface TCDevice
{
[ExportAttribute("initWithCapabilityToken:delegate:")]
IntPtr Constructor(String token, TCDeviceDelegate d);
[Export("connect:delegate:")]
TCConnection Connect([NullAllowed] NSDictionary param, [NullAllowed] TCConnectionDelegate del);
}
Everything is compiled nicely to a dll.
When I use the dll in other project, I call
MyTCDeviceDelegate d=new MyTCDeviceDelegate();
String token="XXXXX";
TCDevice dev=new TCDevice(token, d);
TCConnection conn=dev.Connect(null,null);
The last line always throws Invalid Cast exception.It looks like the method returns NSObject.
What I am missing here?