Foreign Code - function undefined in JavaScript

Hi,

I am fetching contacts from a device. Fetching contact from Android is working properly but in iOS I am getting below error.

Error: Fuse.Scripting.ScriptException: Name: TypeError

Error message: contacts.checkContactsPermissionIsGranted is not a function. (In 'contacts.checkContactsPermissionIsGranted()', 'contacts.checkContactsPermissionIsGranted' is undefined)

Some of code of uno file is:

using Uno;
using Uno.UX;
using Uno.Threading;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Bolav.ForeignHelpers;
using Uno.Permissions;
using Uno.Compiler.ExportTargetInterop;
using Fuse.Controls.Native.Android;

[UXGlobalModule]
public class Contacts : NativeModule {

	static readonly Contacts _instance;
	public Contacts()
	{
		if (_instance != null) return;
		_instance = this;
		Uno.UX.Resource.SetGlobalKey(_instance, "Contacts");
		AddMember(new NativeFunction("getAll", (NativeCallback)GetAll));
		AddMember(new NativeFunction("getPage", (NativeCallback)GetPage));
		AddMember(new NativeFunction("checkContactsPermissionIsGranted", (NativeCallback)CheckContactsPermissionIsGranted));
	}

	object GetAll (Context c, object[] args)
	{
		var a = new JSList(c);
		ContactsImpl.GetAllImpl(a);
		return a.GetScriptingArray();
	}

	object GetPage (Context c, object[] args)
	{
		var a = new JSList(c);
		ContactsImpl.GetPageImpl(a, Marshal.ToInt(args[0]), Marshal.ToInt(args[1]));
		return a.GetScriptingArray();
	}

	Future<string> Authorize (object[] args)
	{
		return ContactsImpl.AuthorizeImpl();
	}

	static Promise<string> _authorizePromise;

	public static Future<string> AuthorizeImpl()
	{
		if (_authorizePromise == null)
		{
			_authorizePromise = new Promise<string>();
			
		}
		return _authorizePromise;
	}

    object CheckContactsPermissionIsGranted(Context c, object[] args)
    {
        if defined(Android)
        {
            return CheckContactsPermissionGranted();
        }
        else if defined(iOS) {
        	var status = GetAuthorizationStatusiOS();
        	if (status == "AuthorizationAuthorized") {
				return true;
			}
			else  {
				return false;
			}
        }     
        else 
        {
            debug_log "Permission.uno::Permission required only on Android";
        }
        return null;
    }

    [Foreign(Language.Java)]
    extern(Android) bool CheckContactsPermissionGranted()
    @{
        return ActivityCompat.checkSelfPermission(Activity.getRootActivity(), android.Manifest.permission.READ_CONTACTS) ==
                PackageManager.PERMISSION_GRANTED ;
    @}

    [Foreign(Language.ObjC)]
	extern(iOS) string GetAuthorizationStatusiOS()
	@{
		ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

		if (status == kABAuthorizationStatusDenied) {
			return @"AuthorizationDenied";
		}
		else if (status == kABAuthorizationStatusRestricted) {
			return @"AuthorizationRestricted";
		}
		else if (status == kABAuthorizationStatusNotDetermined) {
			return @"AuthorizationNotDetermined";
		}
		else if (status == kABAuthorizationStatusAuthorized) {
			return @"AuthorizationAuthorized";
		}
		else {
			return @"Unknown";
		}
	@}
}

And in JS File:

var contacts = require('Contacts');
function getContacts() {
	setTimeout(function() {
	if (contacts.checkContactsPermissionIsGranted()) {
	       var gotContacts = contacts.getAll();
 }
}

Not really sure what I am doing wrong here. I would appreciate some pointers, thanks!

Hello, could you please post a complete example of this failing that we can run from here? I’l get top men working on it :slight_smile:

One question that popped up is, why didn’t you just use our Android Permissions api?