1形耗、Android 如何接收URL參數(shù)
? ? ? ? 在文件android/app/src/main/java/.../MainActivity.java?重寫(xiě)方法?onNewIntent茂浮,代碼如下
public class MainActivity extends ReactActivity {? ??????
...
????@Override
????public void onNewIntent(Intent intent) {
? ??????super.onNewIntent(intent);
? ??????String action = intent.getAction();
? ??????Uri data = intent.getData();? // 獲取URL上的所有數(shù)據(jù)
? ??????if (data != null) {
? ??????????WritableMap params = Arguments.createMap();
? ??????????params.putString("url_query", data.getQuery());? // 獲取URL上的參數(shù)
? ??????????params.putString("url_scheme", data.getScheme());? // 獲取scheme
? ? ? ? ? ? params.putString("url_host", data.getHost());? // 獲取host? ? ? ? ? ????????????sendEvent(Objects.requireNonNull(getReactNativeHost().getReactInstanceManager().getCurrentReactContext()), "XXX", params);? // ‘xxx'這個(gè)可以自己隨便定義,react native監(jiān)聽(tīng)的名字渗常,建議跟ios保持一致
? ? ? ? }
? ? }
? ??private void sendEvent(ReactContext reactContext,?String eventName,?@Nullable WritableMap params) {
? ??????reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
? ? }
...
}
2、IOS 如何接收URL參數(shù)
? ? ? ? 2.1 在ios/文件夾下新建文件 XXX.m 汗盘,最好在xcode下新建皱碘, 此處的XXX建議與上面Android的'xxx'保持一致, 以下所有XXX均表示同一意思隐孽,建議使用同一個(gè)名稱癌椿。
@implementation XXX? ? ??
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
? ??return @[@"XXX"];
}
- (instancetype)init {
? ??????if (self = [super init]) {
? ??????????//初始化的時(shí)候注冊(cè)事件
? ? ? ? ? ??[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fireData:) name:@"fireData" object:nil];
? ? ? ? }
? ??????return self;
}
+ (BOOL)requiresMainQueueSetup{
? ??return YES;
}
+ (id)allocWithZone:(NSZone *)zone {
? ??static XXX *sharedInstance = nil;
? ??static dispatch_once_t onceToken;
? ??dispatch_once(&onceToken, ^{
? ??????sharedInstance = [super allocWithZone:zone];
? ??});
? ??return sharedInstance;
}
- (void)fireData:(NSMutableDictionary *)callbackMap{
? ? // 給React Native發(fā)送數(shù)據(jù),RN監(jiān)聽(tīng)XXX菱阵,建議與Android名稱保持一致
? ??[self sendEventWithName:@"XXX" body:callbackMap];
}
????2.2?在ios/文件夾下新建文件 XXX.h
#ifndef XXX_h
#define XXX_h
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface XXX : RCTEventEmitter <RCTBridgeModule>
- (void)fireData:(NSMutableDictionary *)notification;
@end
#endif
? ? 2.3 更新 ios/projectName/AppDelegate.m
...
#import "XXX.h"? // 引入2.2的文件
...
...
- (BOOL)application:(UIApplication *)application
????openURL:(NSURL *)url
? ??options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
? ??NSMutableDictionary * callbackMap = [NSMutableDictionary dictionary];
????[callbackMap setObject:url.query forKey:@"url_query"];
? ??[callbackMap setObject:url.scheme forKey:@"url_scheme"];
? ??[callbackMap setObject:url.host forKey:@"url_host"];
? ??XXX * xxx = [XXX allocWithZone:nil];? // xxx隨意起參數(shù)名
? ??[xxx fireData:(callbackMap)]; // xxx是上一行的參數(shù)名
? ??return [RCTLinkingManager application:application openURL:url options:options];
}
...
// 注意:一定要在這個(gè)@end之前寫(xiě)上面的代碼踢俄,可以緊挨著,但一定要在它的前面
@end
3晴及、React Native接收數(shù)據(jù) (我是在App.tsx里監(jiān)聽(tīng)的)
import { NativeEventEmitter, NativeModules } from 'react-native';
...
const XXX = new NativeEventEmitter(NativeModules.XXX);
XXX.addListener('XXX', async (callBackMap: any) => {
????// 處理接收到的數(shù)據(jù)
????...
}