GithubHelp home page GithubHelp logo

johnkmzhou / cordova-create-react-app Goto Github PK

View Code? Open in Web Editor NEW
171.0 8.0 31.0 96 KB

A tutorial on how to set up a Cordova project using Create React App.

JavaScript 94.82% HTML 4.41% CSS 0.77%
cordova create-react-app react webpack tutorial starter

cordova-create-react-app's Introduction

Cordova Create React App Tutorial

I was dissatisfied with tutorials that didn't meet my needs, boilerplate projects with incompatible dependencies, and yeoman generators with errors so I decided to make a tutorial to help others set up their initial project. Also if you're migrating an existing web app to Cordova, this will provide insight into what you need to get that to work.

Note that I'm running this tutorial with Cordova 8 but it should work with other versions.

Installation

Install the Create React App CLI.

npm install -g create-react-app

Install the Cordova CLI.

npm install -g cordova

Create the project.

create-react-app tutorial

Because we will be editing the Webpack configuration, go to your Create React App project directory and run:

yarn run eject

Go to your config/paths.js file and change

appBuild: resolveApp('build') to appBuild: resolveApp('www')

Because your files will be served from file:// add this line to your package.json (facebook/create-react-app#1094):

"homepage": "./"

This is specific to create-react-app. In other projects you would need to ensure your paths are not prepended with a /.

Now we will need some files from a Cordova project.

cordova create tutorial com.example.tutorial Tutorial

Next copy the config.xml from your Cordova project to your Create React App project. The other files and directories in the Cordova project are not currently used in this tutorial but take note of them because you may make use of them as your project develops. For example, the res directory would be where you would place icons and splash screens.

Next modify your index.js so it looks like:

const startApp = () => {
  ReactDOM.render(<App />, document.getElementById('root'));
  registerServiceWorker();
};

if(window.cordova) {
  document.addEventListener('deviceready', startApp, false);
} else {
  startApp();
}

And add <script type="text/javascript" src="cordova.js"></script> to index.html. You may also want to add

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">

Now we can build our output to the www directory.

yarn run build

The rest of these instructions have files and changes that are not in the current repository due to the nature of the dependencies that have to be brought down. Also I didn't want to tie the tutorial down with specific versions of Android and iOS.

To target platforms:

cordova platform add ios

cordova platform add android

You need to install SDKs for each platform that you wish to target. Read this to check what requirements need to be satisfied: https://cordova.apache.org/docs/en/latest/guide/cli/index.html#install-pre-requisites-for-building Generally you will probably have to install Android Studio, XCode, SDKs, emulators, build systems, etc.

To test the app run:

cordova emulate android

cordova emulate ios

To test on a connected devie run:

cordova run android

cordova run ios

Recommended Plugins

Often what you will find is that a plugin is no longer being maintained. In that scenario your options are either to find a fork that resolves the issues you are encountering or, as a last ditch effort, attempt to fix it yourself.

Firebase OAuth Authentication

To authenticate using OAuth Providers follow these instructions: https://firebase.google.com/docs/auth/web/cordova

Because the cordova-universal-links-plugin is outdated you should instead install this fork:

cordova plugin add https://github.com/walteram/cordova-universal-links-plugin.git --save

Firebase Analytics and Push Notifications

Due to various issues that haven't beeen resolved in the latest npm package of cordova-plugin-firebase I recommend getting the version of the plugin directly from git

cordova plugin add https://github.com/arnesson/cordova-plugin-firebase.git --save

iOS Deployment

First, sign up for a paid Apple Developer Account. In Xcode, open your project from /platforms/ios and go to your Preferences > Accounts. Add the Apple ID that was used for the developer account. Select your Agent Role and click on Manage Certificates.. and click on the + icon to generate a signing certificate.

Next, go to https://developer.apple.com/account/ios/profile and add a new provisioning profile. Select your development or distribution option and on the next page fill your App ID Description (for example, My Cordova App) and your bundle ID that can be found in config.xml.

Next, in Xcode go to your project navigator and select your project. In a drop down list make sure your project is selected as a target. Then, under Signing, select your Team and generate your signing certificate.

To prepare for distribution on the Apple App Store, you need to create an archive. In Xcode make sure the app is properly provisioned and signed for distribution. Next run yarn build followed by cordova prepare to copy any assets and plugins. Then in Xcode, ensure the version and build numbers are properly set and choose a generic device target from the Scheme toolbar menu. Then choose Product > Archive. There is an issue uploading to the App Store from the Archives window. Instead export the archive and upload it via Application Loader. After uploading the build to iTunes Connect, if the build is still in the "Processing" phase after an hour, attempt to upload another build because more than likely there's probably an issue with Apple's servers.

Android Deployment

First, sign up for a Google Play Developer account. Next, run yarn build followed by cordova build android --release which will result in an apk that needs to be signed. Then run:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore android.keystore app-release-unsigned.apk alias-name

where android.keystore is the location of the keystore and alias-name is the alias for your key. Make sure you have Android Studio installed because it will also come with the zipalign tool. For Mac OS you can find it in ~/Library/Android/sdk/build-tools/{version}/zipalign. Then run:

~/Library/Android/sdk/build-tools/{version}/zipalign -v 4 app-release-unsigned.apk release.apk

followed by uploading the build to the Google Play Console.

cordova-create-react-app's People

Contributors

johnkmzhou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cordova-create-react-app's Issues

Cordova.js loads too late

I was following this tutorial and I faced an issue where cordova.js wasn't loaded when index.js was executed.

In particular, window.cordova was undefined at this part. It would load later.

if (window.cordova) {
  document.addEventListener('deviceready', startApp, false);
} else {
  startApp();
}

I found the problem was that the <script type="text/javascript" src="cordova.js"></script> was placed below the </body>. This would lead to the script loading after the app. More information here.

To fix this, cordova.js should be placed somewhere such as before </head> (which is where the css is linked). This ensures we have cordova when the app bootstraps. I tried this fix and it works for me.

Why build to `www` instead of the usual `build`?

I am trying to use your instructions in the README. I am still struggling with a sequel of error messages originating from the yarn run eject step: missing babel-loader, etc.

Question is: all cordova apps have their source code inside www/, so I don't understand why you use that directory as build target. I fear that the build run will wipe it away every time, forcing me to move my source code elsewhere. But then why not leave the build target where create-react-app puts it?

On the other hand, I do not see how to integrate the various cordova build <platform> commands in the collection of scripts of the React app without ejecting it.

Can you please shed some more light on this subject?

Is eject really necessary?

Hello, thanks for this amazing tutorial. It was helpful to me.

The only question is about ejection. When I ejected, the config folder was readonly, so I coudn't edit it. But it was not necessary, as the only reason to do that is to change the build path. I could manually move the build to www folder. Alternatively, you can do something as said in this issue, but with caution about support.

Empty screen

i followed everything you mentioned, but at the end when i run cordova emulator androind the emulator started but i only got empty white screen.
cordova version: 8.1.0
React version: 16.5.2

Use Cordova plugins

Hi!
Have any ideas how to use cordova plugins inside react?
For example simplest cordova-plugin-device

Trying call like:

  • console.log(device.cordova);
  • --/-- window.cordova.plugins.device.cordova
  • --/-- window.plugins.device.cordova

But nothing works.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.