How can I pass parameters to a local page in PhoneGap app?

后端 未结 3 1394
野的像风
野的像风 2021-01-15 15:08

In a PhoneGap app, we can setup the starting page using the following code.

self.viewController.wwwFolderName = @\"www\";
self.viewController.startPage = @\"         


        
相关标签:
3条回答
  • 2021-01-15 15:39

    If I was you, I would just pass all of the information to javascript via a plugin method that you will create(i.e. plugin.GetQueryString() ) You can create a general plugin method that will return all of the query parameters as a string, and then you can process it in javascript with a following method:

    function getQueryParams(qs) {
        var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;
    
        while (tokens = re.exec(qs)) {
            params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
        }
    
        return params;
    }
    
    window.$_GET = getQueryParams(plugin.GetQueryString());
    

    As a result you will get a nice data structure with all of your GET parameters that you can access by name.

    0 讨论(0)
  • 2021-01-15 15:49

    In phonegap apps, you cannot call local pages by passing parameters to them because the system searches for files with exactly the same name as you pass. It doesn't treat the parameters as query string.

    Ex: when you say "index.html?param=abc", instead of calling page index.html with a
    parameter called param having value abc. It calls the page "index.html?param=abc"
    which doesn't exist in the system. Hence, the error.
    

    The best method to solve the issue is to save your parameters as local storages in the calling page, consume them in your next page and destroy them.

    Ex: In your calling JavaScript file, use:
    window.localStorage.setItem('param', 'abc');
    
    Now, in your called file, access the local storage and delete it
    param abc = window.localStorage.getItem('param');
    
    window.localStorage.removeItem('param');
    

    Hope that helps.

    0 讨论(0)
  • 2021-01-15 15:52

    Edit: for Cordova & Phonegap apps this plugin is useful http://goo.gl/TWN8wq so you do not have to do all by yourself manually modifying the Android manifest or the iOS plist files. There is also the advantage of being able to use the same handleOpenURL() function on both platform (you cannot without the plugin, handleOpenURL() would be a iOS thing only)

    Anyway, manually:

    To achieve what you want, you have to :

    1 - Register a custom scheme (like HelloWorld) and custom url (like customurl) in xCode

    2 - Place a link on the page like<a href=”HelloWorld://customurl/username=James Bond”>HelloWorld</a>

    3 - Get the value of the parameter username using the function handleOpenURL for Phonegap < 3.0 or OpenURL for Phonegap 3+, like:

    function handleOpenURL(url) {
    
                        //Here we can parse the url .
    
                        var link = url.split(‘=’);
    
                        var username = link[1];
    
                        alert(username);
    
    }
    

    There is a full tutorial on how to do it here (iOS) which is where I took everything from.

    Credits to Sourabha Kumar Sahoo

    0 讨论(0)
提交回复
热议问题