我尝试使用此集成指南将React Native集成到现有的Swift iOS应用程序中:https://facebook.github.io/react-native/docs/integration-with-existing-apps.html。 React Native的版本是0.53.0。
我已经成功安装了所有必需的pod,现在尝试构建项目,但总是遇到以下编译错误:
错误日志:
While building module 'yoga' imported from ...node_modules/react-native/React/Base/RCTConvert.h:19:
In file included from <module-includes>:1:
In file included from ...ios/Vandebron/Pods/Target Support Files/yoga/yoga-umbrella.h:15:
In file included from ...node_modules/react-native/ReactCommon/yoga/yoga/YGNode.h:13:
...node_modules/react-native/ReactCommon/yoga/yoga/Yoga-internal.h:11:10: fatal error: 'algorithm' file not found
#include <algorithm>
^~~~~~~~~~~
1 error generated.
In file included from ...node_modules/react-native/React/Views/RCTActivityIndicatorViewManager.m:10:
In file included from ...node_modules/react-native/React/Views/RCTActivityIndicatorViewManager.h:10:
In file included from ...node_modules/react-native/React/Views/RCTViewManager.h:13:
...node_modules/react-native/React/Base/RCTConvert.h:19:9: fatal error: could not build module 'yoga'
#import <yoga/Yoga.h
答案 0 :(得分:10)
这是一个cocoapods问题。尝试通过编辑yoga.podspec文件来公开所需的标题:node_modules / react-native / ReactCommon / yoga / yoga.podspec 在yoga.podspec结束时添加这一行:
spec.public_header_files ='yoga / Yoga.h','yoga / YGEnums.h',
看起来像这样:
source_files = 'yoga/**/*.{cpp,h}'
source_files = File.join('ReactCommon/yoga', source_files) if ENV['INSTALL_YOGA_WITHOUT_PATH_OPTION']
spec.source_files = source_files spec.source_files = source_files
+
+ # Only expose the needed headers
+ spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'
+
end
参考:https://github.com/facebook/react-native/issues/17893
实际拉取请求:https://github.com/facebook/react-native/pull/17764。请查看Plo4ox
的评论。
答案 1 :(得分:1)
您可以将以下代码片段放入Podfile中,以解决此问题以及React Native + Cocoapods的其他一些已知问题。我们在自己的项目中对其进行了测试,并且效果很好:
# When using RN in combination with Cocoapods, a lot of
# things are broken. These are the fixes we had to append
# to our Podfile when upgrading to ReactNative@0.55.4.
#
# WARNING: Check those line numbers when you're on a different version!
def change_lines_in_file(file_path, &change)
print "Fixing #{file_path}...\n"
contents = []
file = File.open(file_path, 'r')
file.each_line do | line |
contents << line
end
file.close
File.open(file_path, 'w') do |f|
f.puts(change.call(contents))
end
end
post_install do |installer|
# https://github.com/facebook/yoga/issues/711#issuecomment-381098373
change_lines_in_file('./Pods/Target Support Files/yoga/yoga-umbrella.h') do |lines|
lines.reject do | line |
[
'#import "Utils.h"',
'#import "YGLayout.h"',
'#import "YGNode.h"',
'#import "YGNodePrint.h"',
'#import "YGStyle.h"',
'#import "Yoga-internal.h"',
].include?(line.strip)
end
end
# https://github.com/facebook/yoga/issues/711#issuecomment-374605785
change_lines_in_file('../../node_modules/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceSizeMeasureMode.h') do |lines|
unless lines[27].include?("#ifdef __cplusplus")
lines.insert(27, "#ifdef __cplusplus")
lines.insert(34, "#endif")
end
lines
end
# https://github.com/facebook/react-native/issues/13198
change_lines_in_file('../../node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h') do |lines|
lines.map { |line| line.include?("#import <RCTAnimation/RCTValueAnimatedNode.h>") ? '#import "RCTValueAnimatedNode.h"' : line }
end
# https://github.com/facebook/react-native/issues/16039
change_lines_in_file('../../node_modules/react-native/Libraries/WebSocket/RCTReconnectingWebSocket.m') do |lines|
lines.map { |line| line.include?("#import <fishhook/fishhook.h>") ? '#import "fishhook.h"' : line }
end
end
源代码摘自以下要点:https://gist.github.com/Jpunt/3fe75effd54a702034b75ff697e47578
答案 2 :(得分:0)
对于反应59或更高版本,您需要执行此操作 它还解决了下载react 11的问题
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'root' do
# Uncomment the next line if you're using Swift or would like to use dynamic
frameworks
# use_frameworks!
# Pods for root
pod 'React', :path => '../node_modules/react-native'
pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "React"
target.remove_from_project
end
end
end