lunes, 19 de diciembre de 2011

Execute methods asynchronously using NSOperation

Sometimes we want to run some methods in background, for example a service request.
Cocoa has a class ready for execute asynchronous methods, it's NSOperation. NSOperation is a key value coding and key value observing compilant, this means that you can observe some properties to perform different actions in your application, for example, if it finished executing (isExecuting).


  1. //We init a new queue
  2. //Create a new operation
  3. NSInvocationOperation *operation = [NSInvocationOperation alloc] initWithTarget:self selector:@selector(getAge:) object:@"Nicolas"];
  4. //Add operation to the queue
  5. [queue addOperation:operation];


Right here we are creating a new queue and adding an operation to it, this way the callback is in the queue and will be called asynchronously.
So we need to declare the callback


  1. -(void)getAge:(id)sender
  2. {
  3.    //Do magic
  4. }


In this case sender will be an (NSString *) object with value "Nicolas", but you can send whatever object you want as a parameter to the callback.

How to use NSUserDefaults

NSUserDefaults is a useful class used to store data in the device, you can save and access information quickly.


  1. //First we get the singleton initialization for NSUserDefaults
  2. NSUserDefaults *myPreferences = [NSUserDefaults standardUserDefaults];
  3. // We are trying to save our volume value
  4. [myPreferences  setDouble:0.9 forKey:@"Volume"];
  5. //We will force to synchronize
  6. [myPreferences  synchronize];


Notice that in this case we have saved our volume value which is a double, but we can store integer, string, even an object to NSUserDefaults class.

So now we can call synchronize to synch all the values that we have been set. Synchronize method is not necessary because it is called during a period of time, but you can call it to ensure that your data is synched.

So now if we need to get the value of our volume, we can just write:

  1. //We use our NSUserDefaults object to get the Volume
  2. double volume = [myPreferences doubleForKey@"Volume"];

And that's all, we can store some values in our device using this simple class.

miércoles, 14 de diciembre de 2011

Working with NSNotifications

Notifications are a great way to communicate with all the application, you can send just a message or add an object to the notification too.
The way it works is very simple, you need to post a notification with a name, for example if i want to let the project know my name, i could write :

  1. //Post notification
  2. [[NSNotificationCenter defaultCenter] postNotificationName:@"myNameIs"];

Right there you can see that NSNotificationCenter class has a defaultCenter. Commonly defaultCenter provides a singleton design pattern which has exactly one instance.
This way you'll use always the same object at memory, but how could the application knows my name if i'm not sending it?

You can also add an object to the postNotification method, for example in this case:

  1. //Now we add an object...
  2. [[NSNotificationCenter defaultCenter] postNotificationName:@"myNameIs" object:@"Nicolas Vidal"];

Gotcha! Now all the project should know my name, but... we need a way to listen to this notification.

So let's see that another ViewController is telling himself... "Which is his name?", so here come's the magic.

In the ViewController event, "ViewDidLoad" we must add an observer to that notification and a callback to receive that object.
So why ViewDidLoad? Because this event is called just when the View is loaded, remember that we need to add just ONE observer for each class/controller , and then remove our observer when we not need it
anymore.

  1. //Listen to notification
  2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(knowHisName:) name:@"myNameIs" object:nil] ;

That way we need to implement the selector this way :
  1. -(void)knowHisName:(NSNotification *) nameNotification
  2. {
  3.    NSString *hisName = [nameNotification object];
  4.    NSLog("%@",hisName);
  5. }
We could observe that the NSNotification has an object, the same that we sent in postNotification, pretty simple, huh?

Now we need to remove the observer to not listen anymore to my notification :
  1. //Removing notification
  2. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"myNameIs" object:nil];

That's all... now you can send messages through all the application with this amazing class.

lunes, 12 de diciembre de 2011

Introduction to the Interface Builder

The interface builder is a great tool for designing the user interface (user interface) of our application.
From XCode 4, Interface Builder is integrated into XCode and can be used with ".xib" files.
XIB, short for Mac OS X Interface Builder, are XML files that contain the positions, types of elements and controls that contains the view you are creating.


The controls are part of the xib and must be IBOutlet type, so if we want to create a button in our view, we need to create it inside @interface.


  1. @interface xibTestViewController : UIViewController
  2. {
  3.       IBOutlet UIButton *_customButton;
  4. }


In case of events, for example when you press the button (event TouchUpInside:), action must be IBAction. Tapping the button will call an event like this:

  1. //Touch event
  2. -(IBAction) customButtonTouched:(id)sender;

After writing this in the interface, we can go to ".xib" file and drag a button to the view, as we see in the picture





Pressing right button we will see that it will list the events you can call, and also the ability to reference the IBOutlet.
Remember that the IBOutlet will serve to change control properties, such as text, size, among others.


We will see something like this:



If we hold down on the circle at the right of each event, and drag on "File's Owner", we will be able to see all IBOutlet controls that are in the interface, in this case, the buttons.


This is a quick introduction to the interface builder, I will post later more advanced articles on it.

domingo, 11 de diciembre de 2011

Basic language syntax III

Properties



Properties help us to define a piece of information, either a variable or object, and that it is visible to all classes that are going to consume it.
To create a property on the interface we must write the following

  1. //Property example
  2. @property(nonatomic,retain) NSString *example;

The parentheses "()" will specify the attributes of our property, for example, I decided to use nonatomic and retain.

Nonatomic means that if the setter or getter is being accessed from multiple threads, can not be interrupted, also nonatomic properties are much faster to access.

Making alloc and init makes retain count increases by 1.

On implementation side we call the @synthesize, to tell the compiler to generate the methods getter / setter that are necessary for the attributes that we have specified in the property.
For example:

  1. //@Synthesize generates our getter/setter functions
  2. @synthesize example;

To use it we call it this way:

  1. //Use self. to call our properties
  2. self.example = @"This is a test";

If we want to have a class variable and maintain the state both inside the class and when accessed from outside, we do as follows:

Interface

  1. @interface Test : NSObject
  2. {
  3.     NSString *_example;
  4. }
  5. @property(nonatomic,retain) NSString *example;

Implementation

  1. @implementation Test
  2. @synthesize example = _example;

When we use it

  1. self.example = @"Test";
  2. //Or...
  3. _example = @"Test";

sábado, 10 de diciembre de 2011

Basic language syntax II

It once learned as a class consists in Objective-C, next thing to learn is functions and methods


Functions and methods


As explained above, a class in Objective-C had an interface (. H) and implementation (. M).
Therefore the interface must list all methods and functions, an example of method could be:

  1. //Declare...
  2. -(void)addFriendWithName:(NSString *)name;

In the implementation ...

  1. -(void)addFriendWithName:(NSString *)
  2. {
  3.        //Code for our method
  4. }

We can see that there is a method start (-) to indicate that this method will be used
when you instantiate the class that contains it.

For example we could use the class importing the header (interface), in this case we could call it "Friend".

Then it would be something like:

  1. //Import header file
  2. #import "Friend.h"

And when we call it, we must instantiate it like

  1. Friend *objFriend = [[[Friend alloc] init] autorelease];
  2. [objFriend addFriendWithName:@"Nicolas"];

In this way we create a pointer to memory using alloc, initialize and invoke autorelease to tell the compiler to add the pointer to a release stack. This means that when not needed, this object will be removed automatically.
Recall that in Objective-C, we use memory management, so the objects must be freed as we don't need them anymore.

We could also start the method with a (+), which means it will be a class method or function. You can call directly without being allocated, either instantiated.
So when we write the call...

  1. //Call method
  2. [Friend addFriendWithName:@"Nicolas"];

If we write a method that contains more than one parameter:

  1. //More parameters!
  2. -(void)addFriendWithName:(NSString *)name withEmail:(NSString *)email;

miércoles, 7 de diciembre de 2011

Basic language syntax


Objective-C language can be a strange and uncomfortable digging into this technology. At first it may be awkward syntax, but once used to call methods, statements, and other memory management, things get easier.


Classes


Classes in Objective-C are composed of an interface or protocol (".h" extension), and an implementation (".m" extension)


Protocols


A protocol is a public contract that lists the statements of the methods and properties, in turn the protocol defines which is the parent class that inherits from.


What does this mean? All classes that use the class created will have knowledge of what properties and methods can be used, as well as who inherits the class.


An example:

  1. #import <UIKit/UIKit.h>
  2. @interface iPhoneTestViewController: UIViewController <UITableViewDelegate,
  3. UITableViewDataSource>
  4. {  
  5. }

In the top line as we can see the interface iPhoneTestViewController inherits from UIViewController.
Later I will explain in detail the utility of each Controller in Cocoa.


You can also see that in "<>" possibility of adding delegates that will
observe the events dispatched by the controller.
You can also import other interfaces of other classes to be used in it,
in this case we are importing UIKit, library required for all iPhone applications.

Implementations



Just as we saw that there are protocols that define how you will involve the class, these are
accompanied by a file that defines the implementation of it (. m).
This can be used to develop the behavior of the methods, overwrite
methods of the parent class and implement the event behavior of some
delegate. An example might be

  1. #import "iPhoneTestViewController.h"      //This is the header   
  2. @implementation iPhoneTestViewController
  3. //Class constructor
  4. - (id)init {    
  5.     [super init];
  6.     return self;
  7. }


This is a simple example of a class implementation, where the protocol is imported and we have an init method. The init method is used as a constructor of a class to instantiate,
for now I will not go into detail on class constructors or overloads.



jueves, 9 de junio de 2011

iOS 5 - New Functionalities

As had been waiting for has come iOS 5. IOS is the operating system of our popular apple for mobile devices, as well as iPhone, iTouch or iPhone.
Has over 200 new enhancements and new features, among which we highlight the following

  • Improved notification system, if an application is running or playing a game, just a notification appears on the top of the screen, allowing the user to continue with your application and where required the top display a list of notifications.
  • Improved system tasks can be synchronized with Outlook, iCalendar and icloud. Besides that you can select the location point of them.
  • Twitter! For all the fans and users of twitter, iOS 5 has a very interesting module to offer ... Once the account can be configured to share in this great social network from your web browser Safari, Photos, Youtube or Google Maps! You can even share the location of the device.
  • For all users who lose time to take a photo unforeseen iOS 5 brings a very simple plug and easy to use. When the device is locked, you can quickly access your camera without having to unlock it! How? Just a little button next to the bar release.
  • No more wires! With iOS 5 you can download the latest updates of the operating system from Apple or restore it directly from icloud. Later in another icloud more detailed articles.
  • Improved multitasking and added new shortcuts for iPad devices. What is multitasking? The ability to run multiple applications at once, so I can be playing a game while it syncs my contact list!
As the first release of IOS 5 we will come across that there will be updates, which we will discuss in subsequent articles.

martes, 7 de junio de 2011

Connect to a web service



Before explaining how to connect to a Web service using Cocoa, you have to understand what is Web Service.
A Web Service is a site that contains methods and functions that can be consumed by other applications or websites. And whukeeping my methods and functions in the web service when I can have them in my application or website?
Sometimes you need to maintain the methods and functions outside of our application, when you need several applications or sites, re-use the same methods and web service functions, and not replicated for each application.
There are two types of web services

SOA. - Are the services that use a base architecture of communication messages.
ROA. - Are the services that use resources (resources) and communication based architecture, which is an implementation of REST, using technologies like XML, JSON, XHTML, plists.
Now going to the point in question, let's see how you can connect to a type of Web Service REST ...


For HTTP you can perform the following orders:


1.     GET
2.     POST
3.     PUT
4.     DELETE
5.     HEAD



In this case we will make a GET request to the service, so we need to follow the following steps:


- Know the web service information such as your address URI, or the methods that we check, and their respective parameters
HTTP-Request Ordering
-Obtain and crumble the result


The result that we will get will be something like this:


  1. <results>
  2. <head>
  3. <name>Nicolas</name>
  4. <entity>2</entity>
  5. <date>Tues Jun 07 06 GMT 2011</date>
  6. <description>Example of response</description>
  7. <method>ObtenerInformacion</method>
  8. <lon>-73.993525</lon>
  9. <title>iPhone Example</title>
  10. <url>http://iphoneexample:7074/.....</url>
  11. </head>
  12. </results>




Since we know that we're going to find once you make the request to the web service, let's move to write code:

  1. / / We set the web service URI (Remember that the @ is always strings below)
  2. NSString *urlWebService = @"http://ejemploiphone:7074/...";
  3.  
  4. / / Create an NSURL object with the web service
  5. NSURL *url = [NSURL URLWithString:urlWebService];
  6.  
  7. NSStringEncoding encoding;
  8. NSError *error;
  9.  
  10. NSString *doc = [NSString stringWithContentsOfURL:urlUsedEncoding:&encoding error:&error];
  11.  
  12. NSURLResponse *response;
  13. NSError *error;
  14. NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
  15.  
  16. //Handle the request
  17. NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
  18. NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];