React Native build errors - objective-c

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.

Related

React Native NativeModules Empty Object

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

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.

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.

Apple Mach-O Linker (Id) Error with Reachability

I keep getting this error when I try to build my project which uses reachability (the error only came up after I tried to implement reachability):
I read some other posts on the internet but nothing seemed to work. I added SystemConfiguration.framework (project, build phases, +), but that didn't work (I had already added it when the error came up). Here's how I implement the files:
#import <UIKit/UIKit.h>
#import "Reachability.h"
#interface catalogDetailView : UIViewController{
}
-(void)checkNetwork;
#end
Then in the .m:
#import "catalogDetailView.h"
-(void)checkNetwork{
if ([self reachable]) {
NSLog(#"Connected to Network");
}
else {
NSLog(#"Connection Failed");
}
}
-(BOOL)reachable {
Reachability *r = [Reachability reachabilityWithHostName:#"apple.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable) {
return NO;
}
return YES;
}
So if you could help that would be amazing!! Thanks ;)
Luke
You need to add Reachability.m to your target's "Compile Sources" build phase.
One way to do it:
Select Reachability.m in the project navigator, on the left side of the Xcode window. If you don't see it, choose the menu View > Navigators > Show Project Navigator.
Show the file inspector on the right side of the Xcode window. View > Utilities > Show File Inspector.
In the Target Membership section, check the checkbox next to your target.
I'll bet you that your 'compile sources list' does not contain reachability.m. Drag it in under the build phases tab to the list. I often find that when adding files to a project, Xcode often doesn't add the files your target's compile list (because the default is to not add them).

imagepicker controller shows error when build for iphone simulator 3.0

I am developing an application which uses both video recording and photo shoting.So i want to show buttons according to os for this i implement these methods.It's working fine when i build for OS 3.1 but when i build for OS 3.0 it shows errors
here are the methods
if ([self videoRecordingAvailable])
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsImageEditing = YES;
imagePickerController.allowsEditing = YES;
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
imagePickerController.videoMaximumDuration = 60.0f; // Length for video recording in seconds
imagePickerController.mediaTypes = [NSArray arrayWithObjects:#"public.movie", nil];
imagePickerController.showsCameraControls=YES;
[self.navigationController presentModalViewController:imagePickerController animated:YES];
}
- (BOOL) videoRecordingAvailable
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return NO;
return [[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera] containsObject:#"public.movie"];
}
the errors are
error: request for member 'allowsEditing' in something not a structure or union
error: request for member 'videoQuality' in something not a structure or union
error: 'UIImagePickerControllerQualityTypeHigh' undeclared (first use in this function)
(Each undeclared identifier is reported only once for each function it appears in.)
error: request for member 'videoMaximumDuration' in something not a structure or union
error: request for member 'showsCameraControls' in something not a structure or union
how do i solve this issue?
The problem is that the video capture has been added in 3.1, which means that the image picker from 3.0 does not support any of the video properties and methods (see the documentation and pay attention to the Availability sections).
As for the solution, I guess you could try using the message syntax instead of the dot syntax:
[picker setShowsCameraControls:YES];
This will give you warnings though (when compiled for 3.0 and older), and you have to be careful not to do it on older devices, because you’ll get an unknown selector exception. Or you can call the selector dynamically, which will get rid of the warnings and you can also check if the selector is supported first:
SEL msg = #selector(setShowsCameraControls:);
if ([picker respondsToSelector:msg])
[picker performSelector…];
There are already several questions about writing for different OS versions.
Responding to comments: I think the main problem is that you are blindly pasting the code without understading it. Don’t do that. Sit and think about what the code does, until you understand each and every line. Now to explain your problem more thoroughly:
The Image Picker in 3.0 has no video controls, since it can’t record video. Therefore when you try to compile a code such as picker.showsCameraControls, the compiler complains: There is no showsCameraControls property in the Image Picker class, that has only been added in 3.1.
But there is a way around that, you can use the message syntax ([foo setBar:…]) instead of the dot syntax (foo.bar=…). If the foo object has no setBar method, the compiler will warn you, but the code will compile. Now let’s use the message syntax to set the camera controls:
[picker setShowsCameraControls:YES];
When you compile this code for 3.1, it will compile without warning and run without error. When you compile for 3.0, you will get a warning from the compiler and if you run the code, it will fail (since there really is no showsCameraControls property). But that is not a problem, since you can only decide to run the fragile code if the OS supports it:
BOOL videoSupported = [picker respondsToSelector:#selector(setShowsCameraControls:)];
if (videoSupported) {
[picker setShowsCameraControls:YES];
// set all the other video properties
} else {
// do what makes sense without video support
}
This will work, but you’ll still get compiler warnings on 3.0. Now it depends on your default build target. If you build for 3.1, the warnings will disappear and the code should work on 3.0 just fine.