This is sample app of React-Native, with Swift bridge.
Counter.swift
import Foundation
@objc(Counter)
class Counter: NSObject {
static func requiresMainQueueSetup() -> Bool {
return false
}
@objc
func increment(_ origin: Int, withCallback callback: RCTResponseSenderBlock) -> Void {
callback([origin + 1])
}
@objc
func decrement(_ origin: Int, withResolve resolve: RCTPromiseResolveBlock, withReject reject: RCTPromiseRejectBlock) -> Void{
if(origin <= 0){
reject("E_COUNT", "You can't decrement any more!!", nil)
return
}
resolve(origin - 1)
}
}
Counter.m
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"
@interface RCT_EXTERN_MODULE(Counter, NSObject)
RCT_EXTERN_METHOD(increment: (NSInteger)origin withCallback:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(decrement: (NSInteger)origin withResolve:(RCTPromiseResolveBlock)resolve withReject:(RCTPromiseRejectBlock)reject)
@end