viernes 30 de octubre de 2009

Save on Delicious (for the iPhone/iPod Touch)

To have Delicious on you iPhone, you can create a bookmark "Save on Delicious" in Safari and write as link the following text (cut&paste if you have 3.0 , if not you can try LazyText or have a good writing :) ):

This first version (hold this link copy and remove the first "R") is the original one, it creates a new window and you have the complete functionality for the tags, also the window closes itself when finished, but is not iPhone optimized:

javascript:(function(){f='http://delicious.com/save?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'&v=5&';a=function(){if(!window.open(f+'noui=1&jump=doclose','deliciousuiv5','location=yes,links=no,scrollbars=no,toolbar=no,width=550,height=550'))location.href=f+'jump=yes'};if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})()

This second one (hold this link copy and remove the first "R") comes from sillybean.net, doesn't open a new window and the page is the mobile version.

javascript:(function(){var newUrl='http://m.delicious.com/save?url=';var thisUrl=encodeURIComponent(location.href);var title='&title='+encodeURIComponent(document.title);location.href=newUrl+thisUrl+title;})();

lunes 13 de julio de 2009

Reusable Cells in UITableView

It is a really well known issue that you need to “reuse” the cells to increase the performance of the table… what it is not so well explained is how this works in practice.

This is a snap of the code for reusing a UITableViewCell or loading the cell from a NIB file when there is no cell available for reusing:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
}
// Set up the cell
return cell;
}

The first time I saw this I thought the cell was loaded from the Nib file just once and then reused all the time, WRONG!!.
This way of thinking was causing me a lot of problems, basically how was the Table able to manage all the complexity of the views’ behaviour with just one cell… But once I read how all this works in fact, all came clear, crystal clear.

Some facts:
1. All the cells that are visible in the Table have its one UITableViewCell.
2. The UITableView only put cells in the reusable queue when they go outside the visual window.
3. In the first time, all the visible cells in the table are loaded using the Nib file (7,8, 10 times, depending on the height of the cells).
4. Once you start scrolling the table is when UITableView starts to put UITableViewCells in the reusable queue and can be reused in other positions of the table.

Why this behaviour is not explained in dequeueReusableCellWithIdentifier reference is really amazing. Probably because it’s evident… this entry is dedicated to those who are as dim as me.

miércoles 3 de junio de 2009

Scalling UIView and all its content

I have been looking for several days to find a way to scale an UIView and all its content, at the end the solution is simple:

self.transform = CGAffineTransformMakeScale(1.1, 1.1);
(been self the UIView)

it is so simple that is embarrassing :), a code snip of my example, when you push the view it gets bigger 10%:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.transform = CGAffineTransformMakeScale(1.1, 1.1);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.transform = CGAffineTransformMakeScale(1, 1);
}

domingo 12 de abril de 2009

Understanding a bit better Interface Builder

If you have been playing with Interface Builder for the iPhone development and you have found it a bit difficult to understand, here is a post with some basic ideas to help you to understand how it works and interact with your code.

As a newbie on the iPhone, but more on Mac development, this tool is a bit weird and difficult to understand from the beginning.
The basic difference is that when you load Xib files you create instances of the objects defined inside, in opposition to other tools, like let’s say Visual Basic, which generates code. Interface Builder also creates the links among the components and actions defined in the Xib file and your code when it is loaded.

The first point is the three objects that appears the first time you open a Xib file:
File’s Owner, First Responder and the Application delegate.
Not all classes included in the Xib file are instantiated, there are also Proxy Objects, these proxy objects are in fact references to instances in your code, these are the links between your code and the objects created out of the Xib file, when the file is loaded.

To understand what is the File’s owner you need to see the code uses to load the Xib file:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@”CustomCell” owner:self options:nil];

In this line, self is the File’s owner. In the process of loading the file and creating the instances of the objects defined inside, this process also links the IBOutlets defined in the File’s Owner with the instances.
If you select File’s Owner and open the Connections Inspector (Tools->Connections Inspector (⌘ 2)) you will see the object names linked with the outlets of the selected object.

First Responder is also a proxy object, it points to the first responder in the responder chain. This allows you to send messages to the responder chain throw this element. With the Identity Inspector (Tools -> Identity Inspector (⌘ 4)) you can see and add new Actions to the First Responder. You can then link, for instance, the push of a button to these actions.

It is more frequent to send actions to functions in objects using the IBAction keywork, so the fact is that usually you don’t send messages to the First Responder but you link actions in buttons to specific functions in classes, defined this way:
- (IBAction)touchGo:(id)sender;

The Application delegate is another proxy object that only appears in the “MainWindow.xib” when an application is created by XCode, there is a link automatically created among the File’s owner delegate Outlet and this Application delegate (to see this select the File’s owner and push ⌘ 2).

You can also see (selecting the Connections of the application delegate) how the outlets defined in AppDelegate.h are linked with the window and the UINavigationController (in case of an application of this kind) with those elements in the MainWindow.xib

@interface CustomTable1AppDelegate : NSObject {

IBOutlet UIWindow *window;
IBOutlet UINavigationController *navigationController;
}




The main idea, again, is that the objects that you create on the Xib file are in fact instances, created when the Xib file is loaded. And to have access to those objects you have to create Outlet to link you code with those instances, or if you load directly a Xib file calling loadNibNamed:owner:options: you can go through the array returned to access directly the objects instanciated.

It is quite frequent to have custom UITableViewCell in a Xib file and load it programmatically and access the cell with this code:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:XibName owner:self options:nil];
cell = [nib objectAtIndex:1];


Hopefully this has help you to understand how IB works.

sábado 7 de febrero de 2009

Navigation Controller with Toolbar

I was trying to do this, add a UIToolbar to a Navigation Controller (UINavigationController),

I found this example Navigation Controller + UIToolbar but it inserts the toolbar programmatically.

I have been playing with the Interface Builder until I have managed to include the toolbar with minimal code, this way...

First, you have to create a new Navigation-Based Application. Then you open MainWindow.xib with the Interface Builder and include a Toolbar bellow the Window.


You must drag the Toolbar from the Library to the Nib document window, bellow the Window object to look this way (selecting this list View Mode):


and then set the position of the toolbar inside the window at the bottom (you can hit twice on Toolbar or Window to make this window appears):


You need to do some coding to make the toolbar appears in the right position:
Inside the application delegate class you need to include an outlet as reference to the Toolbar, in <Applicaton>AppDelegate.h:

@interface Windows2AppDelegate : NSObject {

IBOutlet UIWindow *window;
IBOutlet UINavigationController *navigationController;
IBOutlet UIToolbar *toolbar;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain) UIToolbar *toolbar;

@end


and in your <Application>AppDelegate.m:

@implementation Windows2AppDelegate

@synthesize toolbar;

[...]
- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Configure and show the window
[window addSubview:[navigationController view]];
[window addSubview:toolbar];
[window makeKeyAndVisible];


(and the release if you like in the dealloc method).

In the code above the toolbar should be added to the window (addSubview:) after the navigationController, this way the toolbar is visible.

And finally link the Toolbar in the Interface Builder with toolbar outlet in our code.



and that’s it.
If you are interested, The original article provides additional source code about how to move among views.

martes 27 de enero de 2009

iPhone/Objective-C development resources

After some months developing for the iPhone I have found useful code, libraries, resources that could be useful to other developers and save time (a note, I have used only some of them):

SQLite ORMs
Persistent Objects, it allows to save and load a complex data structure in SQLite in a complete transparent way (you don't have to define even the database file name), I love it. This has a cost, the database structure is not the optimal.
ActiveRecord, I found this one complex to install for iPhone development. The approach is different, this one creates the classes that map the database, PersistentObjects creates the database that maps the objects.

HTTP Request (for RESTful APIs, POST/GET and more)
Objective-C REST Client, short library, easy to adapt to your own requirements.

JSON
JSON Framework, I used this one...
TouchJSON, ... but people seems to like more this one (I haven't try)

Inter-threading communication
ActorKit

Books
The iPhone Developer's Cookbook: Building Applications with the iPhone SDK by Erica Sadun, I think this one was the first one and, in any case, is the one I have. For me a good book and the source code is available here, besides that she has set up a google group iPhoneSDK with lot of activity.

Other books are appearing, for instance, this one for beginners Beginning iPhone Development: Exploring the iPhone SDK , in iPhone development, not in Objective-C (it has a lot of success, I can’t talk about the quality I don’t know).

Google Groups
I have found two groups with significant activity:
iPhoneSDK , mentioned previously, and
iPhone SDK Development

Curiosities
Today I have found this Google docs spreadsheet with twitter URLs of iPhone developers. (mine for SmartTime is included ;) )
You can include new ones in this form.

That’s all.
Comments? Something missed?

--edu

sábado 3 de enero de 2009

Nozbe2SmartTime iPhone application


I have started a project, an application for the iPhone: Nozbe2SmartTime, that allows to see in the iPhone the tasks stored in Nozbe.com and copy them into the SmartTime application.

I'm using Nozbe for some months now and I found it very useful for tracking all my TODOs. I started to use also SmartTime, which is more focused on organizing the day by day tasks so they complement each other very well, and they fix very well in my way of doing things.

But I found myself in the tedious task of reviewing the Nozbe page and copy the text into the SmartTime application frequently so I started this project to make all this process smoother.

I'm doing this on my spare time so to motivate myself you can follow my work here:
* The website of the project.
* Nozbe2SmartTime Twitter with the day by day progress.

Even if you don't have SmartTime I plan to add some functionality to control de Tasks in Nozbe from the same application (although Nozbe is planning to release an iPhone appication and also although the public API has "some" limitations), this application allows to see all your tasks and projects in one view (which is one of the shortcomings of Nozbe).

For those who have the jailbraked iPhone you can find the last IPA file in the download area, or also download the source code with Subversion. The license of the code is GPLv3 (sincerely, mainly because the people of leftcoastlogic.com didn't provide the minimal help with the URL they use to make backup of the tasks, which btw is the method I use for the integration).

If you use this application, please let me know.

Update: some pictures of the application: http://twitpic.com/photos/Nozbe2SmartTime

--Edu