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

nativeException: "constructor": "constructor()function () { [native code] }" } #17

Closed ryc16 closed 5 years ago

ryc16 commented 8 years ago

@NathanaelA , nice simple plugin. I put it on the real situation. For example, create a app where the main page require to access the calllog. So i add some code your demo main-view-model.js as

  function HelloWorldModel() {
        _super.call(this);

        var cursor = appModule.android.context.getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI,
            null, null, null, null);
        if (cursor && cursor.getCount() > 0) {
            while (cursor.moveToNext()) { }
            cursor.close();
        }

        this.set("message", "Click to see if I have permissions!");
    }

Now, I want to settle to the permission before go to main page. So I think i should request on app.ts.

var application = require("application");
var permissions = require("nativescript-permissions");

console.log("Requested permissions:", [
    android.Manifest.permission.CAMERA,
    android.Manifest.permission.READ_CALL_LOG,
    android.Manifest.permission.ACCESS_FINE_LOCATION,
    android.Manifest.permission.READ_EXTERNAL_STORAGE,
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE]
);
permissions.requestPermission([
    android.Manifest.permission.CAMERA,
    android.Manifest.permission.READ_CALL_LOG,
    android.Manifest.permission.ACCESS_FINE_LOCATION,
    android.Manifest.permission.READ_EXTERNAL_STORAGE,
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE],
    "I really need all the permissions in the world!")
    .then(function (result) {
        console.log("WooHoo you granted me all the permissions!");

        application.start({ moduleName: "main-page" });
    })
    .catch(function (result) {
        var count = 0;
        console.dump(result);
        for (var res in result) {
            if (!result.hasOwnProperty(res)) { continue; }
            if (result[res] === true) { count++; }

        }
        console.log("Oops, I'm so sad, I was only granted " + count + " of 4 permissions!");
    });

However, I got below exception

JS: Requested permissions: android.permission.CAMERA,android.permission.READ_CALL_LOG,android.permission.ACCESS_FINE_LOCATION,android.permission.READ_EXTERNAL_STORAGE,android.permission.WRITE_EXTERNAL_STORAGE
JS: === dump(): dumping members ===
JS: {
JS:     "nativeException": {
JS:         "constructor": "constructor()function () { [native code] }"
JS:     }
JS: }
JS: === dump(): dumping function and properties names ===
JS: === dump(): finished ===

I got 2 questions.

1. In above situation, is it too early to invoke requestPermission? Where is the early places to invoke requestPermission before goes to main screen.

2. Let's say if I register application.uncaughtErrorEvent, once the permission throw exception, the uncaughtErrorEvent catch above permission deny but how we could invoke requestPermission again and navigate back to main page.

NathanaelA commented 8 years ago

You can probably do the permissions on application loaded event, but I don't believe you can do it before that point because several things aren't setup until loaded.

However, it might be better to navigate to a splash screen that does the permission check and then when the permission check is successful navigate to the main page...

ryc16 commented 8 years ago

@NathanaelA , about loaded event, do you mean application.launchEvent? Unfortunately put it on application.launchEvent got the same problem. If this is not you mean, please post the partial code here.

Agree, another option is to have splash to do the job but still want to know loaded event.

NathanaelA commented 8 years ago

Yep, the launch event. I would have thought is should have worked by that point. I'll try and run some tests later this week...

NathanaelA commented 5 years ago

Btw, I tested this in the current version of NativeScript in the App.js file;

application.on(application.launchEvent, function() {
    const permissions = require("nativescript-permissions");
    const perm = permissions.requestPermission(android.Manifest.permission.READ_CONTACTS, "I need Read Contact!");
    perm.then(() => {
        console.log("WooHoo, Perms");
    }).catch(() => {
        console.log("So sad, no perms...");
    });
});

And it worked fine.