{"id":1501,"date":"2022-08-30T15:17:03","date_gmt":"2022-08-30T15:17:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/24\/phonegap-3-0-custom-plugin-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:03","modified_gmt":"2022-08-30T15:17:03","slug":"phonegap-3-0-custom-plugin-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/phonegap-3-0-custom-plugin-collection-of-common-programming-errors\/","title":{"rendered":"Phonegap 3.0 Custom Plugin-Collection of common programming errors"},"content":{"rendered":"<p>I put together a plugin for an app some months ago with phonegap 2.7 and it worked perfectly. The plugin basically opens up the users phonebook and returns to my app the details of the contact the user selects.<\/p>\n<p>I have recently upgraded to Phonegap 3.0 and I am trying to convert my plugin to 3.0; However I can&#8217;t get the plugin to work now that it&#8217;s all 3.0&#8230;.here is what I have<\/p>\n<p><strong>ContactView.java<\/strong><\/p>\n<p>src\\com\\huronasolutions\\plugins\\ContactView.java<\/p>\n<pre><code>package com.huronasolutions.plugins;\n\nimport org.json.JSONArray;\nimport org.json.JSONException;\nimport org.json.JSONObject;\n\nimport android.app.Activity;\nimport android.content.Intent;\nimport android.database.Cursor;\nimport android.net.Uri;\nimport android.provider.ContactsContract;\n\nimport org.apache.cordova.CallbackContext;\nimport org.apache.cordova.PluginResult;\nimport org.apache.cordova.CordovaPlugin;\n\npublic class ContactView extends CordovaPlugin {\n    private static final int PICK_CONTACT = 1;\n    private CallbackContext callback;\n\n    @Override\n    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {\n        if (action.equals(\"getContact\")) {\n\n            this.callback = callbackContext;\n            cordova.getActivity().runOnUiThread(new Runnable() {\n                        public void run() {\n                    startContactActivity();\n                    PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);\n                    mPlugin.setKeepCallback(true);\n                    callbackContext.sendPluginResult(mPlugin);\n                    }\n            });\n            return true;\n        }\n        return false;\n    }\n\n    public void startContactActivity() {\n        Intent intent = new Intent(Intent.ACTION_PICK);\n        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);\n        this.cordova.startActivityForResult((CordovaPlugin) this, intent,\n                PICK_CONTACT);\n\n    }\n\n    @Override\n    public void onActivityResult(int reqCode, int resultCode, Intent data) {\n        String name = null;\n        String number = null;\n        String email = null;\n        switch (reqCode) {\n        case (PICK_CONTACT):\n            if (resultCode == Activity.RESULT_OK) {\n                Uri contactData = data.getData();\n                Cursor c = this.cordova.getActivity().getContentResolver()\n                        .query(contactData, null, null, null, null);\n                if (c.moveToFirst()) {\n                    String ContactID = c.getString(c\n                            .getColumnIndex(ContactsContract.Contacts._ID));\n                    String hasPhone = c\n                            .getString(c\n                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));\n\n                    if (Integer.parseInt(hasPhone) == 1) {\n                        Cursor phoneCursor = this.cordova\n                                .getActivity()\n                                .getContentResolver()\n                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,\n                                        null,\n                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID\n                                                + \"='\" + ContactID + \"'\", null,\n                                        null);\n                        while (phoneCursor.moveToNext()) {\n                            number = phoneCursor\n                                    .getString(phoneCursor\n                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));\n                        }\n                    }\n                    \/\/ get email address\n                    Cursor emailCur = this.cordova\n                            .getActivity()\n                            .getContentResolver()\n                            .query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,\n                                    null,\n                                    ContactsContract.CommonDataKinds.Email.CONTACT_ID\n                                            + \"='\" + ContactID + \"'\", null,\n                                    null);\n                    while (emailCur.moveToNext()) {\n                        \/\/ This would allow you get several email addresses\n                        \/\/ if the email addresses were stored in an array\n                        email = emailCur\n                                .getString(emailCur\n                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));\n                        \/\/ String emailType = emailCur.getString(\n                        \/\/ emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));\n                    }\n                    emailCur.close();\n\n                    name = c.getString(c\n                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));\n                    JSONObject contactObject = new JSONObject();\n                    try {\n                        contactObject.put(\"name\", name);\n                        contactObject.put(\"phone\", number);\n                        contactObject.put(\"email\", email);\n                    } catch (JSONException e) {\n                        e.printStackTrace();\n                    }\n\n                    callback.success(contactObject);\n                }\n            }\n            break;\n        }\n    }\n}\n<\/code><\/pre>\n<p><strong>ContactView.js<\/strong><\/p>\n<p>\\assets\\www\\js\\android\\ContactView.js<\/p>\n<pre><code>cordova.define(\"com.huronasolutions.plugins.ContactView\", function (require, exports, module) {\n    var exec = require(\"cordova\/exec\");\n\n    var contactView = {\n        show: function (successCallback, failCallback) {\n\n            function success(args) {\n                if (typeof successCallback === 'function')\n                    successCallback(args);\n            }\n\n            function fail(args) {\n                if (typeof failCallback === 'function')\n                    failCallback(args);\n            }\n\n            return exec(\n                function (args) { success(args); },\n                function (args) { fail(args); },\n                'ContactView',\n                'getContact',\n                []);\n        }\n    }\n    module.exports = contactView;\n\n});\n<\/code><\/pre>\n<p>I have the following in the head of my index.html file<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>When I call it in my code like this<\/p>\n<pre><code>window.contactView.show(\n               function (contact) {\n                   success({ \"contact\": contact, \"msg\": \"success\" });\n               },\n               function (fail) {\n                   fail({ \"msg\": \"We were unable to get the contact you selected.\" });\n               }\n           );\n<\/code><\/pre>\n<p>I get the following error in LogCat<\/p>\n<blockquote>\n<p>*09-17 22:09:24.285: E\/Web Console(1679): Uncaught TypeError: Cannot call method &#8216;show&#8217; of undefined at file:\/\/\/android_asset\/www\/js\/txt2.js:477*<\/p>\n<\/blockquote>\n<p>When I call it like this<\/p>\n<pre><code> contactView.show(\n                   function (contact) {\n                       success({ \"contact\": contact, \"msg\": \"success\" });\n                   },\n                   function (fail) {\n                       fail({ \"msg\": \"We were unable to get the contact you selected.\" });\n                   }\n               );\n<\/code><\/pre>\n<p>LogCat says contactView is undefined.<\/p>\n<p>Can someone help, I think I have followed every guide I can find online. Thanks<\/p>\n<ol>\n<li>\n<p>Call your plugin like this:<\/p>\n<pre><code>cordova.require(\"com.huronasolutions.plugins.ContactView\").show(\n function (contact) {\n   success({ \"contact\": contact, \"msg\": \"success\" });\n },\n function (fail) {\n   fail({ \"msg\": \"We were unable to get the contact you selected.\" });\n }\n);\n<\/code><\/pre>\n<p>Also make sure you have defined the plugin in your config.xml:<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-24 12:47:40. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I put together a plugin for an app some months ago with phonegap 2.7 and it worked perfectly. The plugin basically opens up the users phonebook and returns to my app the details of the contact the user selects. I have recently upgraded to Phonegap 3.0 and I am trying to convert my plugin to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1501","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1501"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1501\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}