NativeScript Does Not Recognize "android.Manifest.permission.READ_CONTACTS" - android

Hello I am new to mobile development and Angular 2. I am trying to request permission for reading contacts, however NativeScript is not recognizing "android".
For exmple I am not able to run the following code:
permissions.requestPermission(android.Manifest.permission.INTERNET, "I need these permissions because I'm cool");
Error: Cannot find name 'android'.
Any help will be greatly appreciated.

At the top of your TS file just add:
declare var android: any;
TypeScript does not know what "android" is; so you have to tell it what it is
The other way to fix this is to download the platform typings
https://www.npmjs.com/package/tns-platform-declarations and then install them. Please note the Typings do typically slow down compilation as they add a lot of data that has to be parsed...
Disclaimer: I'm the author of the NativeScript-Permissions plugin you are using. ;-)

Related

How to force "invalid" Typescript to compile straight into JS?

I am writing a plugin for Nativescript, in TS. I am following this guide to reference an android java SDK that I want to use. Essentially, to target a library's platform specific API's, you have to call them relative to their gradle import values - like so.
In my TS, I have the following line:
var mClient = new com.Microsoft.azure.MobileServiceClient();
which I believe should be fine, despite the errors, but unfortunately, it is causing tsc to fail -
nsAzure.android.ts(7,27): error TS2304: Cannot find name 'com'
How can I get this to transpile literally into js and continue with the build and ideally not spit errors or warnings into my log?
If you really just want to silence the error:
declare var com: any;
var mClient = new com.Microsoft.azure.MobileServiceClient();
Ideally you would get a .d.ts file that described com though.

Attempting to change navigator.userAgent using Cordova for Android

I am new to all of this, so I am probably missing something pretty obvious, but I am trying to change the userAgent string in an application I am building with Cordova. Based upon the documentation at the Cordova website, it would appear that I should only need to add the following to my config.xml file:
preference name="OverrideUserAgent" value="My string value here"
So I included this line in my config.xml and then added this code to my javascript in my cordova app:
var ua = navigator.userAgent;
alert(ua);
I then build the project and install it on my android phone. However, when I run the app I get the same userAgent reported back by my alert regardless of whether I included preference name="OverrideUserAgent" value="My string value here" in the config.xml or not. I am using version 5 of Cordova (5.3.1 I believe) and this config.xml fix was listed in the documentation for version 5.1.1 on the Cordova website.
I have also seen another possible fix on stackoverflow in the following post:
Change PhoneGap / Cordova User-Agent for AJAX
Specifically I am looking at the response that recommends:
put this in your onCreate method: (after super.init();)
super.appView.getSettings().setUserAgentString("Your own agent string");
I am sorry to ask such a basic question, but how to I carry this out? Is it possible to do this while still using the Cordova CLI or do I need to import the project into Android studio first? Where exactly do I find the onCreate method that I need to modify.
Thank you for any help you may be able to offer.

"Error: Unexpected close tag" Cordova/PhoneGap

When running the command cordova platform add android, I get a bunch of errors popping up in the terminal.
Caelans-MBP:HiltonExtraordinaryFB Caelan$ cordova platform add android
Creating android project...
Creating Cordova project for the Android platform:
Path: platforms/android
Package: com.phonegap.hiltonsimplyfb
Name: Hilton F&B
Android target: android-19
Copying template files...
Project successfully created.
Error: Unexpected close tag
Line: 8
Column: 12
Char: >
at error (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/node_modules/sax/lib/sax.js:347:8)
at strictFail (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/node_modules/sax/lib/sax.js:364:22)
at closeTag (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/node_modules/sax/lib/sax.js:523:7)
at Object.write (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/node_modules/sax/lib/sax.js:948:29)
at XMLParser.feed (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/lib/parsers/sax.js:48:15)
at ElementTree.parse (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/lib/elementtree.js:263:10)
at Object.exports.XML (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/node_modules/elementtree/lib/elementtree.js:593:13)
at Object.module.exports.parseElementtreeSync (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/util/xml-helpers.js:126:38)
at android_parser.update_from_config (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/metadata/android_parser.js:221:23)
at android_parser.update_project (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/metadata/android_parser.js:349:14)
Caelans-MBP:HiltonExtraordinaryFB Caelan$
Is there any way I can resolve these errors so I can continue.
Without seeing your config.xml file it's hard to troubleshoot, but I'll bet it's the name of your app:
Name: Hilton F&B
Since config.xml must be valid XML, make sure you're using the & ampersand entity and not a raw ampersand, which has meaning in XML syntax.
I also had the same issue and wanted to share my experience. The problem was Unexpected close tag, in your config.xml file you forgot to close one or two tags, if you look at the line which is shown as error, there is not error even, but somewhre in your config.xml file there is tag not closed. Please, make sure all tags have closed correctly.
In my case, I was missing one closing tag in the string that I was parsing to XML. It had a starting tag not closing tag.
Make sure to check all the Tags have starting and closing tags.
If you have XML on runtime and cannot be checked manually, You can use an amazing npm package which provide validate method to check if XML is valid.
npm link: https://www.npmjs.com/package/fast-xml-parser
example:
const { XMLValidator } = require('fast-xml-parser');
const result = XMLValidator.validate(
"<root>Run the , ...' >/etc/hosts.allow where each <net>/<mask> combination (for example, '192.168.1.0/255.255.255.0') represents one network block in use by your organization that requires access to this system</root>",
{
allowBooleanAttributes: true,
}
);
Just sharing, might be helpful for someone.

Cordova error when adding android platform

I'm trying to add the android platform using this command "platform add android error" but I get this error: http://i.imgur.com/C3ShDqn.png
My user folder is "C:\Users\Cretu Eusebiu" but in that error it's says only "C:\Users\Cretu" so I guess is something wrong with the space ?
I've set all paths for npm, android, ant and java.
Thank you.
It was not because of my username with spaces, it was from the spaces of the path where I created the app "C:\Google Drive\Android Apps".

Android: Phonegap 3.1.x fail to load geolocation plugin

We are using Phonegap 3.1.0-0.15.0 and the application chocked when trying to access location information.
Works fine in iOS however the App chock on Android
We are using:
navigator.geolocation.getCurrentPosition(app.location.onSuccess, app.location.onError);
and we also tried:
var geo = cordova.require('cordova/plugin/geolocation');
geo.getCurrentPosition(app.location.onSuccess, app.location.onError);
The logcat mentioned the following:
Could not find cordova.js script tag. Plugin loading may fail. at file:///android_asset/www/phonegap.js:1511
Uncaught module cordova/plugin/geolocation not found at file:///android_asset/www/phonegap.js:56
The plugin was added using:
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git
Are we doing something wrong ?
Thank you in advance
/Edwardo.
Just to answer my own questions. I removed all the plugins manually and then add them (including the problematic geolocation plugin) using the following command:
phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git
That solved my issue.
Edward.

Categories

Resources