React Native NativeModules Empty Object - react-native

Trying to create my first npm module in order to determine if a user is on the phone. It's for a React Native app. No matter what I try, the module returns undefined. NativeModules always appears to be an empty object.
Please help! Below is a link to the code. export default RNOnPhoneCall; in index.js will only return undefined. How do I link the functions in ios folder, and export them in index.js? Heads up, android is not up to date yet, only ios.
Link to Github

After snooping around a little, I found out that the Obj-C file isn't added to the Compile Sources in Xcode. Just follow me:
Let's say the file names are: NotchNative.h and NotchNative.m. If the NotchNative.m file is not listed, the files will not be compiled by Xcode hence it needs to be added to the Compile Sources.
Follow the steps in the image and rebuild/run the iOS app again.
It's surely a fault on the Xcode side, as it doesn't happen in case of Swift projects.

Try rebuilding your project. I faced the same issue recently and simply rebuilding resolved it

I was not exporting the method correctly in my iOS file. Here's the solution, along with a link to the final project.
react-native-check-phone-call-status on github
#import "RNCheckPhoneCallStatus.h"
#import "React/RCTLog.h"
#import <AVFoundation/AVAudioSession.h>
#import<CoreTelephony/CTCallCenter.h>
#import<CoreTelephony/CTCall.h>
#implementation RNCheckPhoneCallStatus
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(get:(RCTResponseSenderBlock)callback)
{
NSString *phoneStatus = #"PHONE_OFF";
CTCallCenter *ctCallCenter = [[CTCallCenter alloc] init];
if (ctCallCenter.currentCalls != nil)
{
NSArray* currentCalls = [ctCallCenter.currentCalls allObjects];
for (CTCall *call in currentCalls)
{
if(call.callState == CTCallStateConnected)
{
phoneStatus = #"PHONE_ON";
}
}
}
callback(#[[NSNull null], phoneStatus]);
}
#end

Related

React Native build errors

When trying to build a native module from Xcode and importing RCTUIManager (#import "React/RCTUIManager.h") I am getting a lot of errors related to RCTBridgeModule. The errors are:
"Redefinition of RCTMethodInfo"
"Cannot find protocol declaration for "RCTBridgeModule" and other errors
If I don't import RCTUIManager everything works fine, but I need this for using:
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[[NSNumber numberWithInt:tagId]];
if (![view isKindOfClass:[UIView class]]) {
RCTLog(#"expecting UIView, got: %#", view);
}
else {
}
}];
How can I get rid of this errors? I am using the latest version of React Native (0.57.8) and Xcode (10.2).
Try with #import <React/RCTUIManager.h>?
Also it would be a good idea to clean the project before building. You can do that selecting Product -> Clean Build Folder or by pressing CMD SHIFT and K.

Vector font not working

I already had run react-native start and then react-native run-ios. So my app is running.
I placed ios-glyphs.ttf into a folder called fonts in my root directory. So ./fonts. I then added to package.json this:
"rnpm": {
"assets": [
"fonts"
]
}
I then ran react-native link.
After that in my app I added in a rende:
<Text style={{fontFamily:'ios-glyphs'}}></Text>
On load of my app it is fine, but when i load the page that renders this custom font I get "Unrecognized font family 'ios-glyphs'", here is a screenshot: http://i.imgur.com/G6hwlor.png
The page does load after dismissing that error but the icon looks like a question mark in a box but my real icon is a star:
I then did some funky stuff. I thought I linked it wrong so I changed the folder path, and moved font to new folder and linked again. Then I went into xcode and saw two of them in "Resources" so I deleted the first one as that folder was no longer there.
Does anyone know how to fix this error? Should I throw discard this commit? Will it make it like I never linked anything? I'm worried I might have screwed up my project with xcode.
Thanks very much to #sfratini - React-Native can't resolve module for fonts - the font name was actually totally different then file name, as hinted by the tutorial.
How I found the real font name was by following #sfratini's instructions.
I opened up xcode, and opened the ios folder in my project folder with xcode, like this.
I then added to AppDelegate.m this code:
NSArray *families = [[UIFont familyNames] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSMutableString *fonts = [NSMutableString string];
for (int i = 0; i < [families count]; i++) {
[fonts appendString:[NSString stringWithFormat:#"\n%#:\n", families[i]]];
NSArray *names = [UIFont fontNamesForFamilyName:families[i]];
for (int j = 0; j < [names count]; j++) {
[fonts appendString:[NSString stringWithFormat:#"\t%#\n", names[j]]];
}
}
NSLog(#"%#", fonts);
Then found the new icon name in the console log.
My project folder:
Open with xcode:
Click on the folders, then add to AppDelegate.m the code then save and either run react-native run-ios or press "Play" button as in screenshot below (You need to recompile, I doubt hot reloading will work but I didn't test):
Then a full list of font names get spit out in the console log:
I did the fonts before linking, then after linking, and did a diff to find my font names.

How to integrate IBM worklight with react-native app?

Our team was using Ionic/Cordova with IBM worklight(7.0). Recently we got an opportunity to try out react-native and we need the worklight integrated with the application.
How do we integrate IBM worklight with react-native application?
In general you need you to create a React Native project like any other, and add to it the MobileFirst Native iOS SDK as you also would add it to any other native iOS project (by following these instructions: https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/7.0/hello-world/configuring-a-native-ios-with-the-mfp-sdk/)
You can find detailed instrustions and a video in the following blog post: https://mobilefirstplatform.ibmcloud.com/blog/2015/06/03/react-native/
The blog post does mention an important aspect of the integration to take note of:
Below I think one of the more important chunks of code to integrate React Native for iOS with the MobileFirst Platform Foundation. The code chunk is the MobileFirst Platform Foundation Adapter invocation call from the file ResourceRequest.m being exported into the React Native javascript code. In this section of code there are two important React Native methods. One being RCT_EXPORT_MODULE(); which will allow you to export a native class into the React Native javascript. The other is RCT_EXPORT_METHOD(...) this class explicitly tells React to expose this class's method for use in the React Native javascript. Exporting the particular method below is going to pass in a path named "path" and also a callback named "results." The adapter call is getting a list of movies stored as JSON data to a particular path (this path ended up not being needed). Then that data is passed as the callback into the React Native code.
#import <Foundation/Foundation.h>
#import "WLResourceRequest.h"
#import "ResourceRequest.h"
#implementation ResourceRequest
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(getJavaAdapter:(NSString *)path results:(RCTResponseSenderBlock)callback)
{
NSLog(#"Invoking GET Procedure...");
NSURL* url = [NSURL URLWithString:#"http://localhost:10080/HelloMobileFirst/adapters/MoviesAdapter/getStories"];
WLResourceRequest* resourceRequest = [WLResourceRequest requestWithURL:url method:WLHttpMethodGet];
[resourceRequest sendWithCompletionHandler:^(WLResponse *response, NSError *error) {
NSString* resultText;
if(error != nil){
resultText = #"Invocation failure.";
resultText = [resultText stringByAppendingString: error.description];
}
else{
resultText = response.responseText;
callback(#[[NSNull null], resultText]);
}
}];
}
#end

Run a scene created with Cocos Builder in cocos2d 2.0, assertion failed

I am following this tutorial to try to run a scene created in Cocos Builder (the tutorial does it with a detailed "cat jump" project, but it's not important for me to do it the same way). I am doing it with cocos2d 2.0 instead of 2.1 as done in the tutorial because with the version 2.1 I get some syntax errors in the CCBReader class, which instead compiles fine with cocos2d 2.0 .
I've followed the guide until the "The Main Event" paragraph, and the app should work. For who doesn't want to follow the guide, this is what I've done so far:
Correctly dragged CCBReader and CCControlExtension folder to the xcode project;
Dragged all the resources from the Cocos Builder project folder to xcode project's resources;
Created a group named "scenes" in which I dragged my MainMenuScene.ccbi file;
Replaced the line of code that pushes the scene with this one:
-
[director_ runWithScene: [CCBReader sceneWithNodeGraphFromFile: #"MainMenuScene.ccbi"]];
I even tried this:
[director_pushScene: [CCBReader sceneWithNodeGraphFromFile: #"MainMenuScene.ccbi"]];
With no luck. When I run the app an assertion fails:
I've found the main reason of the problem, but I don't know how to fix it. The problem is that in a CCBReader method:
+ (CCScene*) sceneWithNodeGraphFromFile:(NSString *)file owner:(id)owner
{
return [CCBReader sceneWithNodeGraphFromFile:file owner:owner parentSize:[[CCDirector sharedDirector] winSize]];
}
This method returns nil, then this other method gets called:
+ (CCScene*) sceneWithNodeGraphFromFile:(NSString *)file owner:(id)owner parentSize:(CGSize)parentSize
{
CCNode* node = [CCBReader nodeGraphFromFile:file owner:owner parentSize:parentSize];
CCScene* scene = [CCScene node];
[scene addChild:node];
return scene;
}
Where node is nil, and this causes the assertion fail, but I don't know why sceneWithNodeGraphFromFile:owner: returns nil, I have a file named "MainMenuScene.ccbi" in my project, and it's also copied into the app the bundle, so I don't see a reason for this method to return nil. I would really appreciate if someone could show me how to fix this problem, or to show me some alternative way to import a Cocos Builder scene into a cocos2d 2.0 project.
put a breakpoint on this line:
CCNode* node = [CCBReader nodeGraphFromFile:file owner:owner parentSize:parentSize];
then look at all of the variables in the variable view then step into that method, you will see why that is returning nil, probably wrong file path, or wrong type, URL for path etc
Ramy, seens like the file isn`t compiled in your project. check if the file "MainMenuScene.ccbi" is on your Bundle Resources.
You can check it in your project > Build Phases > Copy Bundle Resources...
Or you can select the file in your project tree and check "Target Membership" on right panel (Utilities)
It turned out that I forgot to add the directory "ccbResources" from the Cocos Builder project to xcode. Now with this folder added the entire project works.

Converting 'ZXingObjC' (QR Code Library) non-ARC to ARC, have some problems

I've never worked with non-ARC before, and I tried ZXingObjC sample project (QR Code Scanner), it's non-ARC project as well as the library.
I started with deleting the autorealese, dealloc etc.
But I've encounter 'retain' and I don't know yet how to handle it.
I'll glad for a hand as it will benefit a lot of us.
Example of 'retain' statements:
if (zxd) {
input =
[ZXCaptureDeviceInput deviceInputWithDevice:zxd
ZXAV(error:nil)];
[input retain];
}
Another One:
- (void)setCaptureDevice:(ZXCaptureDevice *)device {
if (device == capture_device) {
return;
}
if(capture_device) {
ZXQT({
if ([capture_device isOpen]) {
[capture_device close];
}});
[capture_device release];
}
capture_device = [device retain];
}
EDIT:
OK, I've been trying to make "EDIT --> Refactor --> Convert to Objective-C ARC", I get:
Another way to use "ZXingOjbC" without convert it to arc projcet:
I use the library by turning off arc when compile it's source code, it works fine when combining both arc and non-arc code in the same project.
use following steps:
put "ZXingObjC" source folder into your project
go to project->Build Phases
select all ZXingObjc files(you can select all at once by pressing shift) then press Enter.
key in "-fno-objc-arc" tell compiler to compile these source code without arc.
OK, what I did is:
Clean all non-ARC old code.
"EDIT --> Refactor --> Convert to Objective-C ARC".
Added missing AudioToolbox.Framework
Project now fully work with ARC.
I'll upload later to github.