react-native-unified-contacts: Cannot read property `getContacts` of undefined
I feel like I must be doing something wrong here.
To import react-native-unified-contacts
, at the top of the file, I have:
var Contacts = require('react-native-unified-contacts');
(which is copy/pasted straight out of the readme)
In the code, I run:
Contacts.getContacts( (err, contacts) => {
if (err) {
return reject(err);
}
resolve(contacts);
});
Running Contacts.getContacts
throws the error at the bottom. It’s as though Contacts
is undefined and so the module isn’t exported?
I’ve followed the other instructions in the README
- adding the folder into the project in XCode, ensuring it’s a top-level folder, npm install
, etc. It’s possible I did that wrong, but I checked twice.
Stacktrace:
Possible Unhandled Promise Rejection (id: 0):
Cannot read property 'getContacts' of undefined
TypeError: Cannot read property 'getContacts' of undefined
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:84737:9
at tryCallTwo (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25983:1)
at doResolve (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26138:9)
at new Promise (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26004:1)
at InviteFriends.getContacts (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:84735:8)
at new InviteFriends (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:84772:7)
at ReactCompositeComponentWrapper._constructComponentWithoutOwner (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:18187:19)
at ReactCompositeComponentWrapper._constructComponent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:18169:13)
at ReactCompositeComponentWrapper.mountComponent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:18081:15)
at Object.mountComponent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:16531:29)
This is running in the simulator on iOS 10 in XCode 8.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 42 (27 by maintainers)
@jignamru Okay, I was able to get it working without needing to update the library. Here’s what I did:
Create a new app called ExampleApp:
Go into that the new ExampleApp directory:
Install React Native Unified Contacts and save it to
package.json
:Open up the ExampleApp in XCode:
Takes a minute to index your new project.
Click on the top level Project:
Click on the
+
button in the bottom left and click “Add files…”Navigate to
ExampleApp/node_modules/react-native-unified-contacts/
:Click on
RNUnifiedContacts
directory and clickAdd
:Ensure
RNUnifiedContacts
is at the first level in my Project Structure:Click on root level
ExampleApp
in the project structure:Click on
Build Settings
tab:Set the
Use Legacy Swift Language Version
toNo
.Build the project:
This should be “Successful”.
Add
Note: After entering “NSContactsUsageDescription” and tabbing from the key field, it will be replaced with XCode’s friendlier version.
NSContactsUsageDescription
key toInfo.plist
:Open up
index.ios.js
:Add the following code that Includes the
react-native-unified-contacts
library and gets all the Contacts:Run the project:
This will take a little while to boot up the simulator if you haven’t already got it running.
You’ll see iOS ask you for access to Contacts. Click OK:
Use the Shake Gesture for the Simulator:
Click “Debug JS Remotely”:
Go to the browser window where the remote debugger is attached and view the console, you’ll see that all of the simulator’s Contacts are there:
That should do it!
So try and replicate these steps and see if you can get it working. I’ll be around tonight so post a reply and let me know how it goes.
Well, I’ve got it working in my project, so there IS a way. I’ll just try and reverse engineer it. I won’t be able to test against 8.1 beta but I’ll see what’s going on.
I wish I just wrote this in Obj-C from the get-go but I wanted to make use of the Swift niceties. I might still rewrite in Obj-C just so it plays nicely with React Native and rnpm, though.