Saturday, August 10, 2013

Javascript is great to write cross platform client side scripts. With JQuery and Ajax, web requests are even more easier to write. Here is an example.
$.ajax({
 type: 'POST',
 url: 'http://openapi.starbucks.com',
data: { param1 : 'value1' },
beforeSend: function(){
 $('#ajax-panel').html('<div class="loading"><img src="/images/loading.gif" alt="Loading..." /></div>');
},
success:function(data){
$('#ajax-panel').empty();
$(data).find('item').each(function (i) {
$('#ajax-panel').append('<h4>' + $(this).find('title').text()+ '</h4>');
});
},
error:function(){
$('#ajax-panel').html('<p class="error"> There was an error</p>');
}
});
or we could make an asynchronous request:
$.ajax({
   type: "POST",
   async: true,
   url: "http://<myserver>/<resource>",
   data: "{" + args + "}",
   contentType: "application/json"
   dataType: "json",
   success: function show(result) {
    data = result.d;
   },
   fail: function err(xhr) {
   data = "ERROR";
   }
});

re-sign

I wanted to publish a WPF application such that the application files don't appear under the nested 'Application Files\AppName_Major_Minor_Build_Revision' folder but published flat alongside the xbap file. The Xbap file had references to the files in the nested folder and it carried a digital signature.
Luckily the application (appname.exe.deploy) file and its manifest had no reference to this nested folder.
Since the xbap file is an xml file, I opened it in notepad and removed the reference to the nested folder.
But this invalidates the digital signature. The digital signature comes with the signing tab of the project properties. The publish tab of the same project properties also mentions which files to include and sign. The certificate associated with the project ( this is included as .pfx file in references ) was used to sign and publish. So I used sn.exe to re-sign the file and update its digital signature. The sn.exe is a dot net tool available with the Microsoft SDK. Incidentally, if you wanted to re-sign the manifest, you could edit and re-sign it in Mage.exe or MageUI.exe which also comes with the SDK. 

Friday, August 9, 2013

Fiddler installs a virtual machine on Mac to run. There is no http proxy like Fiddler running natively on Mac. 

Thursday, August 8, 2013

Objective C tutorial

It's a combination of object oriented programming, managed runtime and C programming language.

Methods are invoked like this
output = [object method];
output = [object methodWithInput:input];
Methods can return a value

id myObject = [NSString string];
id means anyType not known at compile time
All objective C object variables are pointer types so instead of id above we could have written NSString*

Nested messages are those where the output of one method or message call is the input to the other. It is written as
[NSString stringWithFormat:[prefs format]];

Method names can be split:
-(BOOL) writeToFile:(NSString*)path atomically:(BOOL)useAuxilaryFile;
and invoked as
BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
writeToFile:atomically is the method name

Code inside square brackets means you are sending a message to an object or a class.

property accessors are camel-cased for set as in setCaption and no mention of get as in caption

Instead of object and method notation in the square brackets, you can use object.method;

 Objects are created by assignment and allocation
NSNumber* value = [[NSNumber alloc] initWithFloat: 1.0];
NSString* string2 = [[NSString alloc] init];
[string2 release]

Interfaces are defined as :
@interface Photo:NSObject {
            NSString* caption;
            NSString* photographer;
}

@end symbols ends the class declaration.

Dealloc method is called to free up memory

Reference counting is done on objects. You manage local references. If you create an object with alloc or copy, send it to autorelease the old object and retain the new one. You can use keywords such as autorelease, release,  retain and super dealloc.

You only need to release that which you alloc.

Logging is done with syntax like this:
NSLog( @"The current date and time is: %@", [NSDate date] );

The nil object is the functional equivalent of NULL.

Categories are one of the most useful features of Objective-C. It lets you add methods to an existing class just like C# extensions.
For eg: to add a method to NSString to determine if the contents are URL, you could specify:
@interface NSString (Utilities)
- (BOOL) isURL;
@end;
Utilities is the name of the Categories.
In this post, I will be talking about web requests and responses in Objective C for an IPhone app. The framework provides NS APIs to make URL Requests such as NSMutableURLRequest and NSURLConnection. Both GET and POST methods can be made with these. However, CFNetwork library is even more easier  to use for this. The caveat with most helper methods is that their quirks have to be understood The approach to use NSURLConnection is quite straightforward. However, setting and detecting proxies with this framework isn't as convenient as say in the ASI framework. In fact, ASI framework can not only detect the proxy settings and configurations, it can also specify another or with a .pac script file.  ASI wraps the CFProxySupportTool. Unlike the WinInet in Windows, the way to retrieve the proxy settings from iOS is via the high level wrapper APIs.

Here's an example:

   NSMutableURLRequest *request =
    [[NSMutableURLRequest alloc] initWithURL:
     [NSURL URLWithString:@"https://test.openapi.starbucks.com/v1/oauth/token"]];
    
    [request setHTTPMethod:@"POST"];
    
    NSString *postString = @"grant_type=client_credentials&client_id=your_client_id&client_secret=your_client_secret&scope=test_scope&api_key=your_api_key";
    
    [request setValue:[NSString
                       stringWithFormat:@"%d", [postString length]]
   forHTTPHeaderField:@"Content-length"];
    
    [request setHTTPBody:[postString
                          dataUsingEncoding:NSUTF8StringEncoding]];
    
    [[NSURLConnection alloc]
     initWithRequest:request delegate:self];
    
    
}

Wednesday, August 7, 2013

THe Xcode framework has MVC patterns similar to anywhere else so that different views can be navigated through controller actions. The actions can be invoked from the controls on the UI so this follows a well known pattern. Additionally the model can keep track of all object states. It can be persisted locally on the device if SQLLite provider is used. Otherwise, model can also be persisted as files. Cloud based storage for model could be possible via iCloud Service.
The controls can be dragged and dropped from the toolbox to the emulator. This is a very visual designer way of describing the page. And we can set various properties on the page as well. In addition, the same controls can be dragged to the controller to show what is associated with the actions. The Objective C framework let us use the objects directly. For example, a button can be dragged to the controller to describe what action is associated with it. The simulator to code connection is tracked with a line so it's easy to follow and to drag and drop.
In our case where we want to design a user interface to make web requests and responses, we could rely on the ASIHttpRequest library. That code can directly be included in your application and it makes GET and POST request easier than the native support. The form on our user interface can help with making the request while the response will simply be echoed out to the screen. This single page interface is sufficient for this kind of a request response view.  This doesn't mean we can't add more pages. Different pages can be added via different views and workflows to navigate between the views can be specified through the controller. The framework allows you to run the app in universal mode so that it can be used directly with both iPhone and iPad without customizing for any one. In addition, there are ways to sign and package your application so that it can be published to the app store.
As a way to test the application with full web request and response, there is a test project created with the source. This can be used to incrementally add features via test driven development. It is nevertheless important to have tests run on the code prior to publish even if this is strictly not followed. At the time of publish, while the code may not change, there may be additional changes coming in via picking out images and themes for the application. Hence, trying out the app from the app store will surely be recommended.
IPhone applications are written with XCode and IOS SDK. The apps are written with the building blocks provided by the framework. The language is Objective C. Creating a hello world application with a one page is easy because you choose the templates, pick the controls and write the associated controller logic. You can test the app in the simulator that comes with XCode.
Publishing an app is also easy using the following steps. Apps are published through the App store. You provide the app and the digital certificates. The App is then compiled and submitted to the App Store. You need the following : identity which is a unique id for an app, a distribution certificate and a private key  that is generated in the app store, a distribution provisioning profile, icons and loading screens.