Code Monkey home page Code Monkey logo

swiftreactnative's Introduction

Info

this project shows the needed steps to migrate the inital react-native setup to pure Swift without the usage of expo.

problem

When creating a react-native project, it will create an Objective-C application delegate. After just replacing the Objective-C version with the Swift version, the run command will result in a error "No bundle URL present". This may have 2 reasons:

  1. the code for getting the URL changed through the years - solved by providing this repository with the swift file
  2. mismatch between running the cli command and the build setting. The react-native template doesn't set the DEBUG flag correctly. - solved by providing the setup steps here

requirements

xcode version: see XCODE_VERSION.md
react native verison: see package.json

Setup

  • npx @react-native-community/cli@latest init SwiftReactNative
  • open XCode
  • open ios/SwiftReactNative.xcworkspace
  • remove main.m, AppDelegate.h and AppDelegate.mm
  • select File / New / File...
  • select Swift File
  • select folder ios/SwiftReactNative and name AppDelegate
  • check "create bridge file" (or how it was spelled)
  • add to SwiftReactNative-Bridge-Header.h
     #import <React/RCTBridgeModule.h>
     #import <React/RCTBridge.h>
     #import <React/RCTEventDispatcher.h>
     #import <React/RCTRootView.h>
     #import <React/RCTUtils.h>
     #import <React/RCTConvert.h>
  • add to AppDelegate.swift
     import UIKit
     import React
     import Foundation
    
     @UIApplicationMain
     class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
           
           // MARK: ReactNative
           let bridge = RCTBridge(delegate: self, launchOptions: launchOptions)!
           let rootView = RCTRootView(bridge: bridge, moduleName: "SwiftReactNative", initialProperties: nil)
           
           self.window = UIWindow(frame: UIScreen.main.bounds)
           let rootViewController = UIViewController()
           
           rootViewController.view = rootView
           
           self.window!.rootViewController = rootViewController;
           self.window!.makeKeyAndVisible()
           
           return true
        }
     }
    
     // MARK: RCTBridgeDelegate
     extension AppDelegate: RCTBridgeDelegate {
        func sourceURL(for bridge: RCTBridge) -> URL? {
           // DEBUG requires SWIFT_ACTIVE_COMPILATION_CONDITIONS to contain DEBUG in a line in your target/build settings
           #if DEBUG
           NSLog("use metro server url - change schema build configuration to switch to release mode")
           return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
           #else
           return Bundle.main.url(forResource: "main", withExtension: "jsbundle")
           #endif
        }
     }
  • select project SwiftReactNative in left File navigator
  • select target SwiftReactNative
  • select tab Build Settings
  • search for SWIFT_ACTIVE_COMPILATION_CONDITIONS
  • extend the selection and add DEBUG on the right for Debug on the left

swiftreactnative's People

Contributors

mafo5 avatar renovate[bot] avatar

Watchers

 avatar

swiftreactnative's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • Update babel monorepo to v7.24.9 (@babel/core, @babel/preset-env, @babel/runtime)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

bundler
Gemfile
  • ruby >= 2.6.10
  • cocoapods '>= 1.13', '< 1.15'
  • activesupport '>= 6.1.7.5', '< 7.1.0'
cocoapods
ios/Podfile
gradle-wrapper
android/gradle/wrapper/gradle-wrapper.properties
  • gradle 8.6
npm
package.json
  • react 18.2.0
  • react-native 0.74.3
  • @babel/core ^7.20.0
  • @babel/preset-env ^7.20.0
  • @babel/runtime ^7.20.0
  • @react-native/babel-preset 0.74.85
  • @react-native/eslint-config 0.74.85
  • @react-native/metro-config 0.74.85
  • @react-native/typescript-config 0.74.85
  • @types/react ^18.2.6
  • @types/react-test-renderer ^18.0.0
  • babel-jest ^29.6.3
  • eslint ^8.19.0
  • jest ^29.6.3
  • prettier 2.8.8
  • react-test-renderer 18.2.0
  • typescript 5.0.4
  • node >=18

  • Check this box to trigger a request for Renovate to run again on this repository

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.