Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

cordova - Workaround for missing whitelist in phonegap for windows phone

In my other question I found out that there is no whitelist for windows phones.

Now I am looking for a native code workaround but I have never written a line of native code for windows phones. So it's not easy for me. I think I can download a page like this:

void GetAirportData()
{
  var url = new Uri("http://server.example.com/data.php", UriKind.Absolute);
  var webClient = new WebClient();
  webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
  webClient.OpenReadAsync(url, url);
}

But how can a get this data to my javascript app?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a workaround. The following code is a Phonegap command that implements Cross Domain Call functionality.

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using WP7CordovaClassLib.Cordova;
using WP7CordovaClassLib.Cordova.Commands;
using WP7CordovaClassLib.Cordova.JSON;

namespace Cordova.Extension.Commands //namespace is predefined, don't change it!
{
    public class Cdc : BaseCommand //Cross domain call
    {
        [DataContract]
        public class CdcOptions
        {
            [DataMember(Name = "path")]
            public string Path { get; set; }
        }

        public void Call(string args)
        {
            CdcOptions options = JsonHelper.Deserialize<CdcOptions>(args);

            var url = new Uri(options.Path, UriKind.Absolute);

            var webClient = new WebClient();

            webClient.OpenReadCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error"));
                    return;
                }

                //Stream -> string
                var sr = new StreamReader(e.Result);
                var result = sr.ReadToEnd();

                DispatchCommandResult(
                    new PluginResult(PluginResult.Status.OK, result));
            };

            webClient.OpenReadAsync(url, url);

        }
    }
}

Test on the client side:


      <script type="text/javascript">

          function cdc(path, success, fail) {

              PhoneGap.exec(
                            success, //success
                            fail, //fail
                            "Cdc", //service
                            "Call", //action
                             path //args
                           );
          };

          function onDeviceReady(e) {

                cdc(
                    {
                        path: "http://stackoverflow.com/questions/9291809/workaround-for-missing-whitelist-in-phonegap-for-windows-phone"
                    },
                    function (arg) {
                        document.getElementById('test').innerHTML = arg;
                    }, function (arg) {
                        document.getElementById('test').innerHTML = arg;
                    });

            }

            document.addEventListener("deviceready", onDeviceReady, false);


      </script>
  </head>
    <body>
        <div id="test"></div>
    </body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...