joshuapinter / react-native-unified-contacts

Your best friend when working with the latest and greatest Contacts Framework in iOS 9+ in React Native.
MIT License
158 stars 56 forks source link

Cannot read property `getContacts` of undefined #12

Closed Jarred-Sumner closed 7 years ago

Jarred-Sumner commented 7 years ago

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.

joshuapinter commented 7 years ago

Hey @Jarred-Sumner. It might be an XCode 8 / Swift 2.3 3.0 issue.

I'll have a look tonight and ensure it's compatible.

Meanwhile, take a look at #7 and #13 to see if either of those are helpful. If they are, make sure to post your findings here.

Thanks.

Jarred-Sumner commented 7 years ago

I got it mostly working.

I ended up:

It felt pretty hacky though - would be great if it worked out of the box (and ideally supported RNPM)

On Mon, Sep 19, 2016 at 8:25 AM Joshua Pinter notifications@github.com wrote:

Hey @Jarred-Sumner https://github.com/Jarred-Sumner. It might be an XCode 8 / Swift 2.3 3.0 issue.

I'll have a look tonight and ensure it's compatible.

Meanwhile, take a look at #7 https://github.com/joshuapinter/react-native-unified-contacts/issues/7 and #13 https://github.com/joshuapinter/react-native-unified-contacts/pull/13 to see if either of those are helpful. If they are, make sure to post your findings here.

Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/joshuapinter/react-native-unified-contacts/issues/12#issuecomment-248025592, or mute the thread https://github.com/notifications/unsubscribe-auth/AArTS4EpVYPgii0zSIHJnK4lQ0KVPFqaks5qrqlTgaJpZM4KAJIv .

joshuapinter commented 7 years ago

You bet. I'll get this updated tonight so it "Just Works".

RNPM works great for Obj-C libraries but doesn't know what to do with Swift libraries. I'm sure I can get it working with some manual RNPM instructions, though.

Stay tuned.

homanp commented 7 years ago

Hey guys, I can confirm this issue. Just updated to iOS 10/Xcode 8.

joshuapinter commented 7 years ago

Hey Gents, spent the last couple days going through and updating to XCode 8 and Swift 3.

Even with their Swift Converter, Apple sure didn't make it easy.

I've pushed the latest to master and published version 1.0.0 to npm so I would recommend updating your package.json to use

"react-native-unified-contacts": "^1.0.0",

And then do a npm update react-native-unified-contacts.

Try out a build of your apps with this new version and let me know if you have any trouble. I'm going to close this issue for now.

jignamru commented 7 years ago

I'm at a loss here, @joshuapinter . Still seeing this error after updating to 1.0.0. Here are some of the things I've tried and re-tried:

  1. adding the folder into the project in XCode (top-level)
  2. npm install
  3. npm start -- --reset-cache

I'm on xcode 8.0, using swift 3.0

I'd really like to make this work but I feel like I'm stuck!

joshuapinter commented 7 years ago

@jignamru Strange. Okay, let's get this working.

I tested with our existing apps, but I will try it with a fresh app now and see if I run into problems.

Stay tuned!

Josh

joshuapinter commented 7 years ago

@jignamru Using a brand new sample app I ran into the same trouble. I think it's due to the newer version of React Native. I'll fix it and release a new update shortly.

joshuapinter commented 7 years ago

@jignamru Okay, I was able to get it working without needing to update the library. Here's what I did:

  1. Create a new app called ExampleApp:

    $ react-native init ExampleApp
  2. Go into that the new ExampleApp directory:

    $ cd ExampleApp/
  3. Install React Native Unified Contacts and save it to package.json:

    $ npm install --save react-native-unified-contacts
  4. Open up the ExampleApp in XCode:

    $ open ios/ExampleApp.xcodeproj/

    Takes a minute to index your new project.

  5. Click on the top level Project: screenshot 2016-10-11 16 00 21
  6. Click on the + button in the bottom left and click "Add files..." screenshot 2016-10-11 16 00 30
  7. Navigate to ExampleApp/node_modules/react-native-unified-contacts/: screenshot 2016-10-11 16 05 02
  8. Click on RNUnifiedContacts directory and click Add: screenshot 2016-10-11 16 04 24
  9. Ensure RNUnifiedContacts is at the first level in my Project Structure: screenshot 2016-10-11 16 06 02
  10. Click on root level ExampleApp in the project structure: screenshot 2016-10-11 16 08 36
  11. Click on Build Settings tab: screenshot 2016-10-11 16 08 42
  12. Set the Use Legacy Swift Language Version to No. screenshot 2016-10-11 16 09 58
  13. Build the project: screenshot 2016-10-11 16 07 41 This should be "Successful".
  14. Add NSContactsUsageDescription key to Info.plist: screenshot 2016-10-11 16 14 13 screenshot 2016-10-11 16 14 40 Note: After entering "NSContactsUsageDescription" and tabbing from the key field, it will be replaced with XCode's friendlier version.
  15. Open up index.ios.js:

    $ open index.ios.js 
  16. Add the following code that Includes the react-native-unified-contacts library and gets all the Contacts:

    var Contacts  = require('react-native-unified-contacts');
    
    Contacts.getContacts( (error, contacts) =>  {
    if (error) {
      console.error(error);
    }
    else {
      console.log(contacts);
    }
    });
  17. Run the project: screenshot 2016-10-11 16 12 02 This will take a little while to boot up the simulator if you haven't already got it running.
  18. You'll see iOS ask you for access to Contacts. Click OK: screenshot 2016-10-11 16 20 49
  19. Use the Shake Gesture for the Simulator: screenshot 2016-10-11 16 21 57
  20. Click "Debug JS Remotely": screenshot 2016-10-11 16 22 30
  21. 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: screenshot 2016-10-11 16 23 04

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.

jignamru commented 7 years ago

Really appreciate your commitment to making this work, @joshuapinter. However, still a problem, and I noticed that I don't see what you see in step 12. This is what I see instead:

screen shot 2016-10-11 at 4 27 56 pm

You know why that would be?

$ swift -v Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38) Target: x86_64-apple-macosx10.9

$ react-native -v react-native-cli: 1.0.0 react-native: 0.35.0

joshuapinter commented 7 years ago

@jignamru Can you confirm that you've got the main target selected? screenshot 2016-10-11 17 34 38

jignamru commented 7 years ago

@joshuapinter - yes, I have the main target selected.

joshuapinter commented 7 years ago

And you're in XCode 8?

jignamru commented 7 years ago

Yes, Version 8.0 (8A218a)

I'm baffled by this. I remember having the problem in finding that setting in my older project. I'm going to try to reinstall xcode at this point since that's one thing I haven't done.

joshuapinter commented 7 years ago

Have you tried creating a new project and following every step from https://github.com/joshuapinter/react-native-unified-contacts/issues/12#issuecomment-253066009?

jignamru commented 7 years ago

@joshuapinter yes I did start from a new project.

joshuapinter commented 7 years ago

@jignamru That's super strange. Yeah, maybe a reinstall will help. If you just skip that step and keep going what happens?

jignamru commented 7 years ago

A reinstall didn't help my case. If I keep going, I get the same undefined error:

screen shot 2016-10-11 at 5 18 10 pm

Tried searching for answers on Google but not seeing anything relevant. Unsure what I can try next. This is so strange!

joshuapinter commented 7 years ago

There must be something we're missing with XCode. When you find out, please post what you find back here.

joshuapinter commented 7 years ago

@jignamru Any update?

jignamru commented 7 years ago

Still hunting for answers, @joshuapinter. I'm curious, do you have Swift 2.3 installed? I wonder if I'm not seeing the "swift legacy" option because I only have Swift 3 installed by updating to the latest xcode. I tried looking into getting 2.3 installed but couldn't find good documentations out there...

joshuapinter commented 7 years ago

@jignamru Not sure actually. I never intentionally installed it but maybe it was there with XCode 7.

jignamru commented 7 years ago

So are you currently running this example on xcode 7 or 8?

On Wed, Oct 12, 2016, 9:18 PM Joshua Pinter notifications@github.com wrote:

@jignamru https://github.com/jignamru Not sure actually. I never intentionally installed it but maybe it was there with XCode 7.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/joshuapinter/react-native-unified-contacts/issues/12#issuecomment-253410627, or mute the thread https://github.com/notifications/unsubscribe-auth/ALDQWteqh6_GXjMzW_kAZI03wGXBspvIks5qzbEigaJpZM4KAJIv .

-Jigna

joshuapinter commented 7 years ago

Xcode 8.

joshuapinter commented 7 years ago

@Jarred-Sumner Hey Jarred, any chance you can do a quick test and see if all the steps work for you in https://github.com/joshuapinter/react-native-unified-contacts/issues/12#issuecomment-253066009 and let us know?

jignamru commented 7 years ago

@joshuapinter: So another team member of mine gave this a shot and got the same results as me, with no option to select "use legacy swift" (both of us only have Swift 3.0 installed) and even after doing the rest of the steps, we are seeing the undefined error.

I tried finding a way to install an earlier version of swift, but without any success (Apple is not making it easy!) I suspect that if you remove any earlier installs of swift, you might see what we are seeing.

If you ever see and fix this, do let me know. You have a cool module but for now, I have to use another one that gets me results. Thanks again for all your help!

joshuapinter commented 7 years ago

Man, that's frustrating. Thanks for trying it out and letting me know. I'll reopen this issue and get it properly fixed up.

bourgois commented 7 years ago

Hi @joshuapinter ! Just tried your module and ran into the same issue as @jignamru using xcode 8.1beta on an ios10.1 phone :(

joshuapinter commented 7 years ago

@bourgois This is ridiculous. I'm getting this fixed today. Did you happen to walk through all the steps in https://github.com/joshuapinter/react-native-unified-contacts/issues/12#issuecomment-253066009?

bourgois commented 7 years ago

Yes I did :( but I am not starting from scratch. I am trying to use this in an existing RN project.

I also tried severals trick I found on SO about swift code inclusion within obj-c project with no luck...

joshuapinter commented 7 years ago

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.

alexHlebnikov commented 7 years ago

I have the same error on Xcode 8.1 (8B62).

joshuapinter commented 7 years ago

@alexHlebnikov Working on a solution here. Are you able to try the steps in https://github.com/joshuapinter/react-native-unified-contacts/issues/12#issuecomment-253066009 and let me know how it goes?

alexHlebnikov commented 7 years ago

@joshuapinter I tried these steps, there is no Use Legacy Swift Language Version in my project Build Settings.

joshuapinter commented 7 years ago

Hello All!

My apologies for the late reply to this but I was finally able to do some sleuthing this weekend and found a resolution to this issue. It helps that I was testing this out on a freshly formatted MacBook so I was actually running into the same problem all of you guys were having. And reproducibility is half the battle.

So, it all came down to a minor change in how the files are added to your Project in XCode. You can't add the directory RNUnifiedContacts, you have to add the three files inside of that directory instead.

See the updated Installation Instructions for what I mean.

It would be a terrific help to me if you guys could quickly test this out to see if it's working. I have a lot of updates I have planned for Unified Contacts so it would be great to solve this and not have to rewrite it in Obj-C.

Many thanks once again.

chrise86 commented 7 years ago

Hate to drag this bag up but I'm getting the same issues again after the latest update (Xcode 8.3) :(

joshuapinter commented 7 years ago

@chrise86 No worries. Did you follow the install instructions to the letter?

chrise86 commented 7 years ago

I did, but it grumbled about the following:

Check dependencies

“Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

** BUILD FAILED **

The following build commands failed:
    Check dependencies

Which I tried, but there are looooads of changes that it failed to do automatically.

joshuapinter commented 7 years ago

@chrise86 Damn. I wish Apple had better support for Swift but it's especially painful for third party libraries like this. Maybe the best bet is to convert this entirely to Obj-C. Not sure.

I'm headed out for the weekend but I'll be able to look at this after I'm back. Can you keep me posted on anything you try?

chrise86 commented 7 years ago

Sure thing!

artkulakow commented 6 years ago

I am having the same issue with xcode 9. I am getting Contacts.getContacts is undefined and I cannot find any of the Swift settings. Any ideas on how to fix this?

joshuapinter commented 6 years ago

@artkulakow Sorry for the trouble. Did you follow the instructions for including the library? I'll try with XCode9 this weekend and see if I can diagnose the issue.