I need to implement flutter eventChannel in Objective-C class - swift

My project mixes Objective-C and Swift
I need to implement flutter eventChannel in Objective-C class
how should I do it?
I try to add a callback in Objective-C and return the result to the eventChannel in AppDelegate.swift. But it seems something is wrong... I am not familiar with Objectiv-C, who can help me
myController.h
typedef void(^NfcBlock)(bool isSuccess); // the nfc and ble service is from a static library.
typedef void(^BleBlock)(bool isSuccess); // i need return connect result to flutter view.
#interface MyController : NSObject<NfcDelegate, BleServiceDelegate,CBCentralManagerDelegate>
#property (nonatomic,strong) BlePassService *blePassService;
#property (nonatomic,strong) NfcPassService *nfcService;
#property (nonatomic,strong) MobilePass *mobilePass;
#property (nonatomic,strong) NfcBlock nfcBlock;
#property (nonatomic,strong) BleBlock bleBlock;
AppDelegate.swift
func nfcCallback(isSuccess:Bool){
// add event sink
}
case CallMethod.initState:
if passKit == nil { return }
passKit.initial()
passKit.setNfcBlock(nfcCallback)
result(String("success"))

Instead of methodChannel I should use FlutterBasicMessageChannel . It can communicate bidirectionally
#import <Flutter/Flutter.h>
#import <Foundation/Foundation.h>
API_AVAILABLE(ios(14.0))
#interface PassKit : NSObject<NfcDelegate, BleServiceDelegate,CBCentralManagerDelegate>
#property (nonatomic,strong) FlutterBasicMessageChannel *msgChannel;
.m File
- (void)sendData:(NSString *)data{
if(_msgChannel==nil) {
[_msgChannel sendMessage:[NSString stringWithFormat:#"error"]];
return;
};
[_msgChannel sendMessage:[NSString stringWithFormat:#"%#",data]];
}
AppDelegate.swift
let twoWayMethodChannel = FlutterBasicMessageChannel(name: ChannelName.twoWayName, binaryMessenger: controller.binaryMessenger);
passKit.setChannel(twoWayMethodChannel)

Related

Convert swift to objective-c on react native project

I want to convert bellow code to objective-c code:
import Foundation
#objcMembers
final class RNBridgeInstanceHolder : NSObject {
static let sharedInstance = RNBridgeInstanceHolder()
var bridge: RCTBridge?
var rctRootView: RCTRootView?
}
I tried like this header file:
#import <Foundation/Foundation.h>
#interface RNBridgeInstanceHolder: NSObject
#property (nonatomic, strong) RCTBridge *bridge;
#property (nonatomic, strong) RCTRootView *rootView;
#end
main file below
#import <React/RCTBridge.h>
#import "RNBridgeInstanceHolder.h"
#import <React/RCTRootView.h>
#implementation RNBridgeInstanceHolder : NSObject
RCTBridge *bridge;
RCTRootView *rootView;
#end
Anyone please help me to convert this code!

Class Extension - unrecognized setter

The following codes crashed:
#interface AppDelegate (PrivateMethods)
#property (nonatomic, strong) NSString * name;
#end
#implementation AppDelegate
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.name = #"foobar";
...
Error is:
'-[AppDelegate setName:]: unrecognized selector sent to instance 0x6d73df0'
When I change
#interface AppDelegate (PrivateMethods)
to
#interface AppDelegate ()
Then it is okay, what would be the reason?
Update: As answered below, since I must use class extension for this purpose, now this question become: Is the use of class extension to declare private methods acceptable?
e.g.
#interface AppDelegate ()
- (void) start;
#property (nonatomic, strong) NSString * name;
#end
Class extension is basically used to enhance the public property variable. Suppose you have exposed readonly object, or getter method of any variable, then you have make the same object as readwrite in extension.Whereas Category is only used to enhance the method/functionality of class.
check this
#interface MyClass : NSObject
// property here is used as readonly.
#property (retain, readonly) float value;
#end
// Private extension, typically hidden in the main implementation file.
#interface MyClass ()
#property (retain, readwrite) float value;
#end
or
#interface MyClass : NSObject
// here we have exposed getter method of private instance.
- (float) value;
#end
// Private extension, typically hidden in the main implementation file.
#interface MyClass ()
#property (retain, strong) float value;
#end
One is a category, the other is a class extension. If you want to add properties to an existing class, you need to use the latter.
This is the right approach:
#interface AppDelegate ()
#property (nonatomic, strong) NSString * name;
#end

Cocos2d ,need to include protocol in my implementation file

So basically I have a protocol inside my interface that I need to include in my implementation because I am getting an incomplete error and therefore can't continue.
. h file
#interface waveLayer1 : CCLayer <GameKitHelperProtocol>
{
...
}
.m file
#implementation waveLayer1
GameKitHelper.h file
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#protocol GameKitHelperProtocol
-(void) onLocalPlayerAuthenticationChanged;
-(void) onFriendListReceived: (NSArray*)friends;
-(void) onPlayerInfoReceived:(NSArray*)players;
#end
#interface GameKitHelper : NSObject {
id<GameKitHelperProtocol> delegate; bool isGameCenterAvailable; NSError* lastError;
}
#property (nonatomic, retain) id<GameKitHelperProtocol> delegate;
#property (nonatomic, readonly) bool isGameCenterAvailable; #property (nonatomic, readonly) NSError* lastError;
+(GameKitHelper*) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) getLocalPlayerFriends;
-(void) getPlayerInfo:(NSArray*)players;
#end
The error is "Method in protocol not implemented" I have more files I can show ,but to save room I decided to see if you can help me fix this with just these codes
#interface waveLayer1 : CCLayer <GameKitHelperProtocol>
This says that "wavelayer1" implements the protocol "GameKitHelperProtocol".
Method in protocol not implemented
says that a method declared in a protocol has not been implemented. Chances are that you forgot to implement one of the "GameKitHelperProtocol" methods, which makes your class NOT implement that protocol, which violates the declaration you made, which causes the compiler to output an error.
Implement these 3 methods in your waveLayer1 class..
-(void) onLocalPlayerAuthenticationChanged;
-(void) onFriendListReceived:(NSArray*)friends;
-(void) onPlayerInfoReceived:(NSArray*)players;
When you declare that a class adopts a protocol, you must write an implementation for all required methods that are defined in that protocol. So in this case, you need to add method implementations that are defined in GameKitHelperProtocol.

How to call nsobject class on a button in view controller

i am doing an apps which i need to call nsobject class in a view controller class, i try a couple method but its not working either i get a sigabrt error or the apps crash.
please help me.
I'm lost for days now.
any one have a sample code for calling nsobject from view controller?
thanks in advance for you kind help.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
-(IBAction)firstButton;
#end
#import <UIKit/UIKit.h>
#interface TutoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
UIButton *firstButton;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIButton *firstButton;
#end
Please post some code where we can provide the correct solution. Even I don't get problem exactly but if you want to call class object then do the following.
-(void)buttoevent:(id)sender
{
nsobjectclass *obj = [[nsobjectclass alloc]init];
//if you want to call any method of that class then do the following.
[obj methodname];
}
First of all check if you have imported the class before using it

What’s the point of repeated class interface declaration in the implementation file?

In some of the Apple Iphone examples, some of the properties are declared in the header file and some properties in the implementation file. For example in the Siesmic XML examples
ParseOperation.h
#interface ParseOperation : NSOperation {
NSData *earthquakeData;
#private
NSDateFormatter *dateFormatter;
// these variables are used during parsing
Earthquake *currentEarthquakeObject;
Contact *currentContactObject;
NSMutableArray *currentParseBatch;
NSMutableString *currentParsedCharacterData;
BOOL accumulatingParsedCharacterData;
BOOL didAbortParsing;
NSUInteger parsedEarthquakesCounter;
}
#property (copy, readonly) NSData *earthquakeData;
#end
ParseOperation.m
#interface ParseOperation () <NSXMLParserDelegate>
#property (nonatomic, retain) Earthquake *currentEarthquakeObject;
#property (nonatomic, retain) NSMutableArray *currentParseBatch;
#property (nonatomic, retain) NSMutableString *currentParsedCharacterData;
#property (nonatomic, retain) Contact *currentContactObject;
#end
What is the use of the additional interface declaration in the implementation file ?
That’s simply a difference between a public and a private class interface. The header describes the public interface, but some of the properties are only meant to be used by the class itself, not by its collaborators. These private properties are usually declared the way you described, as a category or a class extension inside the implementation file.
// Foo.h – the public interface
#interface Foo : NSObject {…}
// Collaborators can only read bar.
#property(readonly) int bar;
#property(readonly) int baz;
#end
// Foo.m
#import "Foo.h"
// Private interface
#interface Foo ()
// Inside class implementation we can also change bar.
#property(assign) int bar;
#property(assign) int other;
#end
#implementation Foo
#synthesize bar, baz, other;
…
#end