NathanaelA / nativescript-permissions

Wraps up the entire Android 6 permissions system in a easy to use plugin.
MIT License
46 stars 22 forks source link

Where is this 'android' object coming from? #11

Closed chief10 closed 8 years ago

chief10 commented 8 years ago

I have been trying to figure out how to get permissions into my nativescript app and came across your plugin.

When looking through the source code of it, I see numerous references to an 'android' object. Where is this coming from since 'application' is the only thing that is being imported?

Below is an example of what I mean: var hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED == android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, perm);

NathanaelA commented 8 years ago

You might look at the readme or demo for the project; it shows how to use the plugin to get any permissions you might need. android is a global on the android platform...

chief10 commented 8 years ago

Thanks for responding to this.

I have tried looking at the demo in the repo for this project, but it doesnt quite illustrate what I would need it to illustrate since I am using both Angular 2 and Typescript.

When I try to use the module as you have it in my project, typescript complains that it doesnt know what "android" is because it isnt declared anywhere in the file. To try and get around this, I created a seperate JS file with the following code

var permissions = require('nativescript-permissions');
var getLocation = function(msg) {
    return permissions.requestPermission(android.Manifest.permission.READ_CONTACTS, msg)
}
exports.getLocation = getLocation 

And try to use it in my project like this:

        var location = require ('./shared/permissions.js');
        location.getLocation("I need them!")
          .then((data) => {
            console.log(data, "Successssssss");
          }, (err) => {
            console.log("Errrrrror","Errrrrror")
            console.log(err, "Errrrrror")
          })

In my AndroidManifest.xml ( among other things ) I have:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

But then I get this error:

JS: Error: Uncaught (in promise): TypeError: format.replace is not a function
JS:     at resolvePromise (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:511:32)
JS:     at /data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:547:18
JS:     at ZoneDelegate.invokeTask (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:323:38)
JS:     at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (/data/data/org.nativescript.mymarket/files/app/tns_modules/@angular/core/src/zone/ng_zone_impl.js:44:41)
JS:     at ZoneDelegate.invokeTask (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:322:43)
JS:     at Zone.runTask (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:223:48)
JS:     at drainMicroTaskQueue (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:449:36)
JS: Unhandled Promise rejection: format.replace is not a function ; Zone: angular ; Task: Promise.then ; Value: TypeError: format.replace is not a function TypeError: format.replace is not a function
JS:     at Console.sprintf (/data/data/org.nativescript.mymarket/files/app/tns_modules/console/console.js:159:23)
JS:     at Console.formatParams (/data/data/org.nativescript.mymarket/files/app/tns_modules/console/console.js:165:32)
JS:     at Console.log (/data/data/org.nativescript.mymarket/files/app/tns_modules/console/console.js:246:49)
JS:     at /data/data/org.nativescript.mymarket/files/app/login.component.js:31:25
JS:     at ZoneDelegate.invoke (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:290:29)
JS:     at Object.NgZoneImpl.inner.inner.fork.onInvoke (/data/data/org.nativescript.mymarket/files/app/tns_modules/@angular/core/src/zone/ng_zone_impl.js:53:41)
JS:     at ZoneDelegate.invoke (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:289:35)
JS:     at Zone.run (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:183:44)
JS:     at /data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:544:58
JS:     at ZoneDelegate.invokeTask (/data/data/org.nativescript.mymarket/files/app/tns_modules/zone.js/dist/zone-node.js:323:38)
JS: Error: Uncaught (in promise): TypeError: format.replace is not a function

EDIT: I am using Nativescript 2.2

NathanaelA commented 8 years ago

Ah, two things to make your life easier. Read my Blog post on TypeScript and Android. ;-)
http://fluentreports.com/blog/?p=308 -- this will help you with the android issue.

The second is; The reason you are getting an error is because console.log wants a string as the first parameter; when you pass it and object it can crash with errors like that; so change you code to be:

var location = require ('./shared/permissions.js');
        location.getLocation("I need them!")
          .then((data) => {
            console.log("Successssssss" , data);
          }, (err) => {
            console.log("Errrrrror","Errrrrror");
            console.log ("Errrrrror", err);
          })

And you should be good, ALWAYS pass a string as the first parameter to a console.log statement even if it is a empty string console.log("", dataItem);