Accessing a React Native module from other native code - ios

I use a native module that works great for getting device info in React Native, in the JS code. I'd like to also make use of it's functionality in other native (Objective-C) code.
Is it possible to access functionality of React Native custom modules from other native code?

You can either access the functionality directly (using -[RNDeviceInfo deviceName] method) or using the way React Native is accessing it, that is:
RNDeviceInfo *rn = [[RNDeviceInfo alloc] init];
NSLog(#"Device Name: %#", [rn constantsToExport][#"model"]);

I found one solution, that is admittedly a bit of a kludge. It does work, but I would imagine this is far from ideal.
At the top of the class that utilizes this code:
#interface RNDeviceInfo ()
- (NSString*) deviceName;
#end
Then I can make use of it like so:
RNDeviceInfo *rn = [[RNDeviceInfo alloc] init];
NSLog(#"Device Name: %#", [rn deviceName]);

Related

How to show native iOS views in a Flutter application?

I am currently working on a project in which I need to implement a graphical framework that has been written for Android and iOS; however, I am writing the application using Flutter/Dart, and so far I can not find any way for showing native iOS views in a Flutter application. I have found a module written for android that claims to do this (blog post), and I am curious if anyone knows of any modules or techniques that can achieve this for iOS.
I know that Flutter has the ChannelMethod feature, but that is not a solution-at least from my understanding, ChannelMethods can pass platform-specific messages, but this does not help me show a platform-specific plugin in a Flutter application.
It has also occurred to me to hard code an equivalent graphic interface in Flutter and then use ChannelMethods to pass the needed data, but this is not ideal because it appears that not all data I would need from the plugin is easily available. I'm really looking for a way to show the UIView in Flutter somehow.
Is there any way to show a native iOS view in a Flutter application?
Alternatively, is there any way to segue to a iOS viewController from a Flutter application?
You can use Flutter Platform Views to display platform native widgets in the Flutter app.
In the sample given in the docs, FLNativeViewFactory creates the platform view, and it provides a reference to the UIView. The iOS Views can be added using the provided UIView reference.
Using Swift
class FLNativeView: NSObject, FlutterPlatformView {
init(
frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?,
binaryMessenger messenger: FlutterBinaryMessenger?
){
_view = UIView()
super.init()
// iOS views can be added here
}
}
Using Objective-C
#implementation FLNativeView {
UIView *_view;
}
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger {
if (self = [super init]) {
_view = [[UIView alloc] init];
}
// iOS views can be added here
return self;
}
#end

Accessing the same persistent store from Swift and React Native

We're considering using React Native for one of our Screens - for fun, to check it and to get a better grasp of the possibilities, but even before w start I need some answers, so we're sure React Native and it's environments can meet our requirements.
In this question I'd like to specifically ask about persisting data and accessing it.
In our app we focus on offline experience, as it's one of the most crucial points for us. On iOS we have a couple of possibilities how to achieve this eg. Core Data, Realm... But the thing is if we decide to implement some part of our app as React Native we'd also need these parts to access stored data and even modify it and save to persistent store.
Can this be achieved? Having one persistent store (SQLite, Realm, something else?) and access it from both Swift code and React Native.
This is extremely easy with React Native in both iOS and Android with Native Modules.
You would build a simple native module in Swift/Obj-C and expose your persistent store:
//RCTMyDataStore.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#interface RCTMyDataStore : RCTEventEmitter <RCTBridgeModule>
#end
and
// RCTMyDataStore.m
#import "RCTMyDataStore.h"
#implementation RCTMyDataStore {
}
RCT_EXPORT_MODULE(MyDataStore);
RCT_REMAP_METHOD(getMyData,
getMyData_resolver:(RCTPromiseResolveBlock)resolve
getMyData_rejecter:(RCTPromiseRejectBlock)reject)
{
// get data from my persistent store
if (success) {
// convert to RN passable format (NSDictionary or NSArray)
resolve(myData);
} else {
reject(#"404", #"No data", nil);
}
}
#end
In your React Native code:
import {
NativeModules
} from 'react-native';
const NativeDataStore = NativeModules.MyDataStore; // must match the RCT_EXPORT_MODULE name
NativeDataStore.getMyData()
.then(data => {
// do some stuff
})
For more info on Native Modules and Swift support, you can check out the docs here: https://facebook.github.io/react-native/docs/native-modules-ios.html#exporting-swift

How to call methods in the custom view in iOS module, from titanium project?

I need to call methods in the custom view in iOS module from titanium project. I have followed the tutorials in Appcelarator documentation about creating iOS module. I could create the custom view in Titanium using below code.
var manage = require('com.test');
var manageView = manage.createView({
left:40,
right:40,
top:40,
height: 250,
backgroundColor:'blue' }); manageView.customMethodInView();
But i got error like "customMethodInView is not a function" when i run the app.
#import "TiViewProxy.h"
#import "CustomView.h"
#interface ComTestViewProxy : TiViewProxy {
CustomView *customView;
}
- (void) customMethodInView;
#end
This is the code in viewProxy class in iOS Module project.
Please help.
I know this is late, but for any wondering soul out there, it is my understanding that even if you don't intend to pass an argument for your method, the signature of the method in the native module should always take one:
your method
- (void) customMethodInView;
should look like this :
- (void)customMethodInView:(id)unused;

iOS - How does Cordova invoke objective c function?

How does cordova trigger objective-c native method. For example- When a user taps on submit button (html button), app needs to invoke native objective c function called 'dataSubmitted'.
Does Cordova monitor webview navigation and based on URL tags call method internally?
Is there any way JavaScript can interact with Obj-c native methods except monitoring webview navigations?
There are two ways to communicate with native language from JS.
1. Go to MainViewController.m -> find a function named webViewDidFinishLoad
and add the follwing code snippet..
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
NSString *pageUrl = [theWebView.request.URL absoluteString];
if ([pageUrl containsString:#"xyz"]) {
// xyz for xyz.html
}else if ([pageUrl containsString:#"abc"]) {
// abc for abc.html
}
self.webView = theWebView;
return [super webViewDidFinishLoad:theWebView];
}
Develop your own native plugin with cordova.exec ;
There are a few libraries available to communicate between Javascript and Objective-C it seems:
WebViewJavascriptBridge: https://github.com/marcuswestin/WebViewJavascriptBridge
GAJavaScript: https://github.com/newyankeecodeshop/GAJavaScript
I suggest you to check out this beautiful SO Post which explains Javascript & Objective-C interactions in detail.

How to use private frameworks in iOS

I am trying to use private frameworks in my app which is not going to app store. Can anyone guide me, how to use private headers generated using class-dump in my ios application.
Below link is listing some private frameworks generated using class-dump. I am trying to import SringBoardServices.h in my MyClass.m file, but i am unable to access it. I am creating an object to SringBoardServices.h like
SringBoardServices *ref = [[SringBoardServices alloc]init];
and trying to access it's methods like ref.method_name. Whether it is a right approach to use private frameworks?
LINK: private-frameworks
I tried using Class myclass = NSClassFromString(#"SpringBoardServices");
id myobj = [[myclass alloc] init]; , but unable to access SpringBoardServices methods.

Resources