React-native-orientation: Can't read getInitialOrientation of undefined

Created on 12 Mar 2017  Â·  16Comments  Â·  Source: yamill/react-native-orientation

I am using react-native 0.39.2 and "react-native-orientation": "^1.17.0"
My code looks like below. I will get Can't read getInitialOrientation of undefined error when executing this code. I have run link on this project.

import Orientation from 'react-native-orientation';
componentWillMount(){

    console.log('xxxx:', Orientation.getInitialOrientation());
  }

to-be-reviewed

Most helpful comment

For anyone who still is having this problem in iOS, you need to manually link the dependencies even if you ran react-native-link.
To link frameworks and libraries:

Your project / the target / General -> Link Frameworks and Libraries

and copy Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a into the link framework and libraries,

also you need to add this file your appdelagate.m file

#import "Orientation.h" // <--- import

@implementation AppDelegate

  // ...

  - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [Orientation getOrientation];
  }

The Build Phase/Link Binary and Libraries was already linked if you ran react-native-link
If not put the libRCTOrientaton.a file into the build phases as well and make sure its set to required.

Once this was done the problem was fixed for me. Hope this helps.

All 16 comments

After running link you will need to rebuild the application, hot/live reloading will not pull in the newly linked native code.

I rebuild the whole thing but still got undefined error. I printed the Orientation and got below output:

screen shot 2017-03-15 at 5 08 53 pm

but when I call Orientation.getInitialOrientation(), I got below error:

Possible Unhandled Promise Rejection (id: 0):
Cannot read property 'initialOrientation' of undefined
TypeError: Cannot read property 'initialOrientation' of undefined
    at Object.getInitialOrientation

This error was thrown from getInitialOrientation() method.

save problem

I'm having the same problem, but only on ios. My android version works fine.

@joeyzhao0113 @mayaa6 did you guys manage to fix this?

not yet.

On Thu, May 18, 2017 at 9:09 PM, bharath-kamath notifications@github.com
wrote:

@joeyzhao0113 https://github.com/joeyzhao0113 @mayaa6
https://github.com/mayaa6 did you guys manage to fix this?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/yamill/react-native-orientation/issues/160#issuecomment-302398559,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADEmotiWI1K4cX32bTNVHmoGLHe6i9ONks5r7EMDgaJpZM4MaeJw
.

I don’t have plan on that.

On 18 May 2017, at 11:09 pm, bharath-kamath notifications@github.com wrote:

@joeyzhao0113 https://github.com/joeyzhao0113 @mayaa6 https://github.com/mayaa6 did you guys manage to fix this?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/yamill/react-native-orientation/issues/160#issuecomment-302398559, or mute the thread https://github.com/notifications/unsubscribe-auth/AWdMip4H_am-gNTiABJ1N0K8GU00V-rFks5r7EMDgaJpZM4MaeJw.

For anyone who still is having this problem in iOS, you need to manually link the dependencies even if you ran react-native-link.
To link frameworks and libraries:

Your project / the target / General -> Link Frameworks and Libraries

and copy Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a into the link framework and libraries,

also you need to add this file your appdelagate.m file

#import "Orientation.h" // <--- import

@implementation AppDelegate

  // ...

  - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [Orientation getOrientation];
  }

The Build Phase/Link Binary and Libraries was already linked if you ran react-native-link
If not put the libRCTOrientaton.a file into the build phases as well and make sure its set to required.

Once this was done the problem was fixed for me. Hope this helps.

and copy Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a into the link framework and libraries,

I am not familiar with iOS project, and how can I found the file Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a?

If you go into xcode, right beneath the x in upper left hand corner there are 8 icons.
Click on the folder icon then click on your project. In your project follow the path
Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a

2017-07-24 09 46 33No libRCTOrientation. A file? I don't have the link.

@1551715165 the file in red color
image

@mayaa6 I know. I don't know this file in the whole project all find it again. Don't have this. A file. This file is created by yourself?

import "../../node_modules/react-native-orientation/iOS/RCTOrientation/Orientation.h"

or
$(SRCROOT)/../node_modules/react-native-orientation/iOS/RCTOrientation
(make sure it is recursive)

Tried what @jpcarts23 and @visylvius mentioned and it works on android and ios. But for ios it doesn't work when trying to run the simulator through react-native-cli in the command line. Works perfectly fine in xCode. Does anyone know why? Getting the error Cannot read property '...' of undefined

Same error. I can understand ios link steps but react-native link works well with ios (not with Android that I linked myself). If I run with xcode work in ios too...

Was this page helpful?
0 / 5 - 0 ratings