Skip to content

Invoke any native code directly from Javascript in React Native (without wrapping it first with a native manager)

Notifications You must be signed in to change notification settings

peacechen/react-native-invoke

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Native Invoke

Invoke any native code directly from Javascript in React Native (without wrapping it first with a native manager). Gives you full access from JS to all native API of iOS (and Android soon).

Install

iOS

  • In your project root, npm install react-native-invoke --save

  • In Xcode, in Project Navigator (left pane), right-click on the Libraries > Add files to [project name]
    Add ./node_modules/react-native-invoke/ios/RNInvoke.xcodeproj

  • In Xcode, in Project Navigator (left pane), click on your project (top) and select the Build Phases tab (right pane)
    In the Link Binary With Libraries section add libRNInvoke.a

  • In Xcode, in Project Navigator (left pane), click on your project (top) and select the Build Settings tab (right pane)
    In the Header Search Paths section add $(SRCROOT)/../node_modules/react-native-invoke/ios
    Make sure on the right to mark this new path recursive

Android

Coming soon

Executing calls to native

Notice that this is regular Javascript code. It has full access to all native API in iOS and there's no RN native manager involved wrapping each individual API call.

import Invoke from 'react-native-invoke';

// execute a single call
const _getContentOffset = Invoke.call(_scrollView, 'contentOffset');
const {x, y} = await Invoke.execute(_getContentOffset);

Invoke.execute returns a promise. The native code doesn't actually execute until Invoke.execute runs.

// execute multiple calls
const _getScrollView = Invoke.call(_scrollParent, 'scrollView');
const _getContentOffset = Invoke.call(_getScrollView, 'contentOffset');
const {x, y} = await Invoke.execute(_getContentOffset);

Only simple serializable objects can pass between native and JS. Since many methods take a complex object as argument, we support making multiple calls in one execution so the result of one call can be passed to the next one without going through JS.

Example invocations

1. from Objective-C
CGPoint offset = [componentView.scrollView contentOffset];
    to Javascript
const _getScrollView = Invoke.call(_componentView, 'scrollView');
const _getOffset = Invoke.call(_getScrollView, 'contentOffset');
const {x, y} = await Invoke.execute(_getOffset);

###### 2. from Objective-C
CGRect frame = componentView.frame;
    to Javascript
const _getFrame = Invoke.call(_componentView, 'frame');
let {x, y, width, height} = await Invoke.execute(_getFrame);

###### 3. from Objective-C
[componentView setFrame:frame];
    to Javascript
const _setFrame = Invoke.call(_componentView, 'setFrame:', Invoke.IOS.CGRect({x, y, width, height}));
await Invoke.execute(_setFrame);

###### 4. from Objective-C
id textView = [componentView valueForKey:@'_textView'];
CGRect pos = [textView caretRectForPosition:textView.selectedTextRange.start];
    to Javascript
const _getTextView = Invoke.call(_componentView, 'valueForKey:', '_textView');
const _getSelectedTextRange = Invoke.call(_getTextView, 'selectedTextRange');
const _getStartPosition = Invoke.call(_getSelectedTextRange, 'start');
const _getCaretRect = Invoke.call(_getTextView, 'caretRectForPosition:', _getStartPosition);
const {x, y, width, height} = await Invoke.execute(_getCaretRect);

## Full example project

Available here, open the /example folder, run npm install and then open /example/ios/example.xcodeproj in Xcode.

API

> Invoke.execute(invocation)

Send the entire invocation to native and execute it. Code runs in native only when we reach this command. Returns a promise with the final return value (make sure it's serializable).

> Invoke.call(target, methodSignature, arg1, arg2, ...)

Prepare a call for later execution.

> Invoke.React.view(componentRef)

Returns (in later execution) the native view backing the React component ref.
Example:

<ScrollView refreshControl={<RefreshControl refreshing={true} ref='myRefreshControl'/>} />
const _componentView = Invoke.React.view(this.refs['myRefreshControl']);

##### > `Invoke.IOS.CGPoint({x, y})`

Returns (in later execution) an iOS point.

> Invoke.IOS.CGRect({x, y, width, height})

Returns (in later execution) an iOS rect.

Notes

  • The final return value from native arrives as the promise result of Invoke.execute. It has to be serializable! If you have return values that aren't serializable (like complex objects), you probably need to have several Invoke.calls and pass them between eachother.

  • All native code is executed on the main thread.


## License

MIT

About

Invoke any native code directly from Javascript in React Native (without wrapping it first with a native manager)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 54.5%
  • JavaScript 35.4%
  • Python 6.2%
  • Java 3.9%