简体   繁体   English

我的 react-native 应用程序可以使用 xcode swift 库吗? 如何?

[英]Can my react-native application use xcode swift libraries? and How?

I'm currently quite new to react-native and xcode.我目前对 react-native 和 xcode 还很陌生。 So basically, I made a program on xcode using swift and importing a library, but now I need to use this code in my react native application.所以基本上,我使用 swift 在 xcode 上制作了一个程序并导入了一个库,但现在我需要在我的 react native 应用程序中使用这个代码。 I don't know if it is possible and how to go about doing it.我不知道这是否可能以及如何做到这一点。

Unfortunately it really depends on how you want to use your swift code, because React Native provides different methods for different circumstances, but I will try to give you an example of an basic "Event Bridge".不幸的是,这真的取决于你想如何使用你的 swift 代码,因为 React Native 为不同的情况提供了不同的方法,但我会尝试给你一个基本的“事件桥”的例子。 First of all every React Native Application automatically generates an iOS and Android project.首先,每个 React Native 应用程序都会自动生成一个 iOS 和 Android 项目。 Navigate to the iOS folder and open the Xcode workspace.导航到 iOS 文件夹并打开 Xcode 工作区。 Afterwards just drag your swift source file into the project (like in every other native application).然后只需将您的 swift 源文件拖到项目中(就像在其他所有本机应用程序中一样)。 As React Native uses an Objective-C Bridge we cannot directly use the Swift code so we need to bridge Swift -> Objc -> Javascript.由于 React Native 使用 Objective-C 桥接器,我们不能直接使用 Swift 代码,因此我们需要桥接 Swift -> Objc -> Z49E1703B6AAAF1D25。

Lets first dive into your Swift class:让我们首先深入了解您的 Swift class:

You need to be able to communicate with objc and export the methods to the react native runtime, here is an example of the swift file您需要能够与 objc 通信并将方法导出到 react native 运行时,这里是 swift 文件的示例

// CustomObject.swift
import Foundation

@objc(CustomObject)
class CustomObject: NSObject {

@objc static var isSelected = false // our state

  @objc
  func selectObject() {
    CustomObject.isSelected.toggle()
    print(CustomObject.isSelected ? "selected" : "not selected")
  }

  @objc
  func getState(_ callback: RCTResponseSenderBlock) {
    callback([NSNull(), CustomObject.isSelected]) // this will return into a  
    // javascript callback to retrieve the current state
  }

  // this is needed to make sure that this will run on the main thread 
  @objc
  static func requiresMainQueueSetup() -> Bool {
    return true
  }
}

Now we need to call these methods from Objc and expose it to JavaScript.现在我们需要从 Objc 调用这些方法并将其公开给 JavaScript。

// CustomObject.m

#import "React/RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(CustomObject, NSObject)
RCT_EXTERN_METHOD(selectObject)
RCT_EXTERN_METHOD(getState: (RCTResponseSenderBlock)callback)
@end

This is all you have to do.这就是你所要做的。

Now you can navigate back to your JavaScript Application and import NativeModules.现在您可以导航回您的 JavaScript 应用程序并导入 NativeModules。

import React, {useState} from 'react';
import {View, NativeModules, Button, Text} from 'react-native';

const TestComponent = () => {
const [selected, setSelected] = useState(false);


const triggerSelection = () => {
  NativeModules.CustomObject.selectObject()
  NativeModules.CustomObject.getStatus( (err, isSelected) => {
   setSelected(isSelected);
  });
}

return (
     <View>
       <Button title="Button" onPress={triggerSelection} />
       <Text>{selected}</Text>
     </View>
 );

}

To pass more complex structures from native to javascript, just take a look at this documentation, it is actually not that bad.要将更复杂的结构从 native 传递到 javascript,只需看看这个文档,它实际上并没有那么糟糕。

React Native - Native Modules React Native - 原生模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM