felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

React Native 入门 --- 如何用 Xcode 编译调试 #83

Open felix-cao opened 5 years ago

felix-cao commented 5 years ago

在老的 React Native 工具中构建项目时,会自动生成一个ios 和一个 android 文件夹,以便用XcodeAndroid Studio打开 iosandroid 项目, 但是在新的 React Native 构建项目时,已经不再有这两个文件夹了,本文将教你如何用 Xcode 编译调试 React Native 项目呢?

一、yarn eject

利用 Expo 命令行工具构建 React Native 项目后,会在生成的 package.json 文件中有这么一段脚本

"start": "expo start",
"eject": "expo eject",
"android": "expo start --android",
"ios": "expo start --ios",

命令行帮助看看

$ yarn eject -h
yarn run v1.7.0
$ expo eject -h
Usage: eject [options] [project-dir]

Creates Xcode and Android Studio projects for your app. Use this if you need to add custom native functionality.

Options:

  --eject-method [type]  Eject method to use. If not specified, the command will ask which one to use. Required when using the --non-interactive option. expoKit, plain
  -f --force             Will attempt to generate an iOS project even when the system is not running macOS. Unsafe and may fail.
  --config [file]        Specify a path to app.json
  -h, --help             output usage information
✨  Done in 0.43s.

利用 yarn eject 生成一个 ios 和一个 android 文件夹,以便用XcodeAndroid Studio打开 iosandroid 项目

选择 ExpoKit, 会比较慢,可以考虑翻墙。

二、安装 CocoaPods

CocoaPods负责管理iOS项目中第三方框架。CocoaPods的项目源码在Github上管理。项目从2011年8月12日开始,CocoaPods的出现使得我们可以节省设置和更新第三方开源库的时间。

安装需要用到Ruby,虽然Mac自带了Ruby,不过版本有点老了,最好更新一下

$ sudo gem update --system

因为Ruby的软件源rubygems.org被屏蔽了,所以要更换源

$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

接下来查看下源路径是否更换了

$ gem sources -l

安装 cocoapods

$ sudo gem install cocoapods

不过有可能出错(我就是出错了),出错的话执行下面的命令

$ sudo gem install -n /usr/local/bin cocoapods

image

三、pod install

执行下面命令之前最好按照 Mac 命令行翻墙 #81 此文做下 proxy处理。

$ cd ios
$ pod install
$ pod setup

经过这一系列处理后,即可用 Xcode 打开 ios 文件夹进行编译调试了

四、导出ipa

参考 Xcode10 - 导出ipa

五、Error 处理

1)、No experience found at exp://exp.host/@czf2008700/weat_client 删除在 app.json 中配置的slug