Note: below solution just for not using InAppBrowser plugin,

When InAppBrowser plugin was add correctly, simply call open.window(url,"_system") will open url in new Browser.


First check your your config.xml , You can define your whiteList by change , for detail how to do it, please check http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html and will make any url not in white list be open in new safari window instand of InWebView.

If your url is in your white list (ex, you use * make all urls are allowed) but you want to open some url in safari, You have to add extra code to do that.

My solution is like below: first I define a function to open safary url:

LaunchNewWindow: function (url) {       
   if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {         
    window.open(url+"#_external", "_blank");}
   else{
    window.open(url, "_blank");
   }
}

then you have to change code in CordovaLic\Classses\CDViewController.m to handle your spacial url: I added it in line685 :

else {

// start stoneskin's change: force open external url in safari                              
               if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
NSString *myurlstr = [url absoluteString];
if ([myurlstr rangeOfString:@"#_external"].location != NSNotFound){
myurlstr = [myurlstr stringByReplacingOccurrencesOfString:@"#_external" withString:@""];
NSURL *myurl = [NSURL URLWithString:myurlstr];
[[UIApplication sharedApplication] openURL:myurl];
return NO;
}
}
//end start stoneskin's change if ([self.whitelist schemeIsAllowed:[url scheme]]) { return [self.whitelist URLIsAllowed:url]; } else { if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } else { // handle any custom schemes to plugins [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]]; } } return NO; }
Don't forget this code only working for Phonegap wrapped app.


 
Categories: JQuery | Mobile | XCode iOS

Yesterday I test cross domain post against MVC successfully. So how about post to WebAPI?

But first try I failed and got error message like "Optional parameter 'user' is not supported by 'FormatterParameterBinding'.

after some research and test, I notice webapi [fromBody] parameters binding only take Model class as parameter.
//var data = "user=a" not owrk...string didn't have properties of user;
public string Test([FromBody] string user)
        {
            return "user:" + user;
        }

I have to change code like below:
public string Test([FromBody] User user)
        {
            return "user:" + user;
        }

  public class User
    {
        public string Name { get; set; }

        public string Pwd { get; set; }
    }
And test to post by jQuery
$.post('http://localhost:6660/api/task/test/0/t', { Name: 'a' ,Pwd:'b'});

and everything works


 
Categories: Asp.net | JQuery | MVC

I don't want to use jsonP because it only can use "GET" method and all the data can be viewed on url.
After many search and failed test code. Finally I got below solution:

1. Enable CORS on server,
  a. asp.net site said you can enable cors for webapi: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api . unfortunately, it require preview version of visual studio 2013.
  b. http://enable-cors.org/ site offer the methods to enable cors on IIS7, But it not works in my test.
  c. so I have to add the Access-Control-Allow-Origin=* to the response header by code myself.
    Simplest way is  adding this function in global.asax.cs
   protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            
        }

my test Action:
 public ContentResult Test(FormCollection formValues,string user="", string pwd="" )
        {
            dynamic flexible = new ExpandoObject();
            flexible.UserName = user;
        

            var dictionary = (IDictionary<string, object>)flexible;
            dictionary.Add("IsSuccess", true);

            var re= JsonConvert.SerializeObject(dictionary); 
          
            return  Content(re);
        }

2. It's not finish yet. you can see the correct response was return to the client in Fiddle or firebug, but the JQuery ajax call success not be fired. we have to do some more code on client side:
var loginUrl = 'http://localhost:6660/home/test';
        var data = "user=a&pwd=b";
        $.ajax({
            type: 'POST',
            url: loginUrl,         
            data: data,
            sucess:function(re) {
                //never reached here
            },
            error: function (jqXHR, error, errorThrown) {
                //handle err
            },
            complete: function (re, status) {
                if (status == 'success') {
                    var reObj = JSON.parse(re.responseText);
                    //todo
                }               
            }
        });

Yub!  date was get from cross domain server. this even working open html file directly.


 
Categories: Asp.net | C# | JQuery | MVC