Skip to content

Instantly share code, notes, and snippets.

@davibe
Created March 16, 2017 10:19
Show Gist options
  • Save davibe/580fc918310f3b481d541d79894b1d82 to your computer and use it in GitHub Desktop.
Save davibe/580fc918310f3b481d541d79894b1d82 to your computer and use it in GitHub Desktop.
react-native swift module with no macros
//
// RCTViewController.swift
// Grocerest
//
// Created by Davide Bertola on 15/03/2017.
//
import Foundation
import UIKit
import React
class MyMethod: NSObject, RCTBridgeMethod {
@objc(JSMethodName) var jsMethodName: String! = "MyMethod"
public var functionType: RCTFunctionType = RCTFunctionType.normal
public func invoke(
with bridge: RCTBridge!,
module: Any!,
arguments: [Any]!) -> Any!
{
print("I have been invoked", bridge, module, arguments.description)
return "value"
}
}
class MyModule: NSObject, RCTBridgeModule {
public static func moduleName() -> String! {
return "MyModule"
}
public func methodsToExport() -> [RCTBridgeMethod]! {
return [MyMethod()]
}
}
class RCTViewController: UIViewController {
var moduleName:String = "ProductAvailabilityModal"
let data:NSDictionary = [
"scores": [
[
"name": "Alex",
"value": "42"
],
[
"name": "Joel",
"value": "10"
]
]
]
override func loadView() {
super.loadView()
let providerSettings = RCTBundleURLProvider.sharedSettings()!
let jsCodeLocation = providerSettings.jsBundleURL(
forBundleRoot: "index.ios",
fallbackResource: nil
)
// a bridge holds one jsContext and may be shared between multiple RCTViews
let rctBridge = RCTBridge.init(
bundleURL: jsCodeLocation,
moduleProvider: { () -> [RCTBridgeModule]? in
return [MyModule()]
},
launchOptions: nil
)
let rctRootView = RCTRootView.init(
bridge: rctBridge,
moduleName: moduleName,
initialProperties: data as [NSObject : AnyObject]
)
view = rctRootView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment