Pages

Friday, September 23, 2011

[Code Snippet] Send GET and POST data to web services.

It has been two weeks with a lot of work and a seriously lack of time to update the blog, but we are back again.

This week code snippet is about sending data to web services, XML and JSON are most used technologies used today but maybe some of your has to send data to an older php web page that receives data from a form by GET, POST or both of them. How could we accomplish that?

Let's see a code snippet to do it synchronously, later we'll publish a new entry with the asynchronous method.

First thig to do is to create the NSURL object which we are going to make the request to. If it has any GET parameters we should put them in the URL String.

NSURL *url = [NSURL urlWithString:@"http://www.fiveflamesmobile.com/login.php?from=mobile"];

After that we have to create a NSMutableURLRequest in order to put the post parameter.

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHttpMethod:@"POST"];

And now we only have to put in the HTTPBody the form variables as URL encoding like "key1=value1&key2=value2..." as a NSData object.

NSString *formValues = [NSString stringWithFormat:@"name=%@&device=%@", @"FiveFlames", @"iPhone"];
NSData *formData = [formValues dataUsingEncoding:NSUTF8StringEncoding];
[urlRequest setHttpBody:formData];

And now, we only just to make the request and save the response.

NSURLResponse *response;
NSError *error;

NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest response:&response error:&error];

NSLog (@"Response: %@", responseData);

That's all, with this code snippet you could keep your older php or asp pages and reuse it to develop a server-client iOS app.

Enjoy it!

Thursday, September 8, 2011

[iOS2WP7] From iOS to Windows Phone. Introduction

From now we're going to alternate the blog's updates between Code Snippets and iOS to Windows Phone migration tips for whose that want to migrate this iOS applications and games to this new mobile OS.

iOS is a huge and very stable platform and iPhone is most sold device from all over the times, so there are thousands and thousand of applications and games in the AppStore. A lot of these games and applications are very useful or adictive (see Angry Birds for example) and Windows Phone users are waiting to have it in their phones so it's a good idea to migrate them, and now it could be a good moment to start.

Windows Phone is a very young OS for mobile devices, and being realistic there aren't so much available devices in the market so you could think "Why should I have to migrate my apps/games for that little potencial market?" Let's see a few good answers.

- Growing trend. Now there aren't a lot of devices running Windows Phone, but for Christmas campaign some manufacturers like Nokia (waiting for them...) HTC (Radar and Titan last ones) Samsung... are going to launch their last models including Windows Phone 7 Mango, so it seems that the potential number of users of your apps/games is going to growht.

- Positioning. Your app/games uploaded to Marketplace have less competence that another platforms like Android Market or AppStore so it's easier to have a better rating and visibility. If your app is the first to do something it will be a reference for Windows Phone users.

- Knowledgment: Maybe there are not much market to attack, but if you adquire the knowledment to migrate iOS apps/games to Windows Phone now, you will be one step beyond the competence if this platform begins to growth.

- Easy to develop: If you are a iOS developer, programming for windows phone is much easier than do it for iOS. I'm doing both things and believe me, it's easier.

- Facilities: Visual Studio for Mobile Development is free for developers, Microsoft developer license for one year has the same cost as iOS one...

- Lack of fragmentation. Like iOS, Windows Phone has fixed characteristics so you haven't to get crazy with differentes screen resolutions, different ROMs, different OS...

If after reading this article, you are convinced to migrate your apps, don't forgot this blog. We're going to publish coding tips, examples with both C# and Objective-C code for comparing, and a serie of articles to help you to accomplish this task.

For fist reading have a look to this guide published by Microsoft: Windows Phone 7 Guide for iPhone Application Developers

And the first link to have under Five Flames Mobile Blog ;) :Windows Phone Interoperability Bridges

Enjoy it!

Monday, September 5, 2011

[HOW TO] Intercept javascript events with Windows Phone WebBrower control

If you need intercept javascript events in Windows Phone web browser control, you must use the ScriptNotify event. This event is launched when the window.external.Notify() function is called from javascript code.

First you must enable IsScriptEnabled property.

IsScriptEnabled

Second, you must handle the ScriptNotify event, you could make this from xaml code or from code behind, as you prefer.

webBrowser1.ScriptNotify+=new EventHandler<NotifyEventArgs>(webBrowser1_ScriptNotify);

EventScript

HTML Example:

<html>
<head> 
    <title>Demo Windows Phone</title>
    <script type="text/javascript">
function SendNotify() { window.external.Notify(‘demo’); }
</script>
</head>
<body>
    <div>
        <input type="button" value="Notify" onClick="SendNotify()"/>
    </div>
</body>
</html>

You can load this html code with NavigateToString function from WebBrowser control.

private void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    if (e.Value == "demo")
    {
        MessageBox.Show(e.Value);
    }
}

Note: You can call javascritp functions from c# code, using InvokeScript method.