swift原生模块
https://reactnative.cn/docs/native-modules-intro
// xyLive.swift
import Foundation
@objc(XyLive)
class XyLive: NSObject {
@objc(addEvent:location:date:)
func addEvent(name: String, location: String, date: NSNumber) -> Void {
// Date is ready to use!
print("hello")
}
}
// xyLive-bridge.m
#import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(XyLive, NSObject)
RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(nonnull NSNumber *)date)
@end
// index.tsx
<Button
onPress={() => {
const XyLive = NativeModules.XyLive;
XyLive.addEvent(
'Birthday Party',
'4 Privet Drive, Surrey',
Date.now(),
);
}}>
test ios module
</Button>
oc ui控件
//
// RNTMapManager.m
// mapp
//
// Created by 武昭 on 2021/12/29.
//
#import <MapKit/MapKit.h>
#import "RCTConvert+Mapkit.h"
#import <React/RCTViewManager.h>
@interface RCTXyRtcTextureViewManager : RCTViewManager
@end
@implementation RCTXyRtcTextureViewManager
RCT_EXPORT_MODULE(RCTXyRtcTextureView)
RCT_EXPORT_VIEW_PROPERTY(zoomEnabled, BOOL)
RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, MKMapView)
{
[view setRegion:json ? [RCTConvert MKCoordinateRegion:json] : defaultView.region animated:YES];
}
- (UIView *)view
{
return [[MKMapView alloc] init];
}
@end
// ==========================================================
//
// RCTConvert+Mapkit.h
// mapp
//
// Created by 武昭 on 2021/12/30.
//
#ifndef RCTConvert_Mapkit_h
#define RCTConvert_Mapkit_h
#import <MapKit/MapKit.h>
#import <React/RCTConvert.h>
#import <CoreLocation/CoreLocation.h>
#import <React/RCTConvert+CoreLocation.h>
@interface RCTConvert (Mapkit)
+ (MKCoordinateSpan)MKCoordinateSpan:(id)json;
+ (MKCoordinateRegion)MKCoordinateRegion:(id)json;
@end
@implementation RCTConvert(MapKit)
+ (MKCoordinateSpan)MKCoordinateSpan:(id)json
{
json = [self NSDictionary:json];
return (MKCoordinateSpan){
[self CLLocationDegrees:json[@"latitudeDelta"]],
[self CLLocationDegrees:json[@"longitudeDelta"]]
};
}
+ (MKCoordinateRegion)MKCoordinateRegion:(id)json
{
return (MKCoordinateRegion){
[self CLLocationCoordinate2D:json],
[self MKCoordinateSpan:json]
};
}
@end
#endif /* RCTConvert_Mapkit_h */