Life & Technology

Handful lessons in different areas of technology and life in general.

Mac OS X Lion: Hide Guest User Account

Recently, I upgraded my MacBook Pro to the latest incremental update of Lion which is version 10.7.2. The upgrade went fine but I noticed that upon logging-in, the Guest user account is now visible/appearing from my login screen every time I boot up my Mac. I thought this was caused by iCloud and Find My Mac feature of OSX Lion but it’s not!

Hiding back the Guest user can be achieved by Checking a single checkbox found under Security & Privacy of your Mac’s System Preferences. Restart. Voila! Guest User account is now gone on my login screen window!

The Fix:
1. Go to System Preferences
2. Under Personal section, click the Security & Privacy icon
3. Click the ‘lock’ icon to make changes.
4. Under General tab, just check the option box titled “Disabling restarting to Safari when screen is locked
5. Click the lock key again to secure your preferences.
6. Restart your machine
7. No Guest User will be displayed on the Login Screen Window

Screenshot:

Security & Privacy Window

Implementing dynamic UITableViewCell height on UITableView

If you’ve used Twitter iOS application, then most probably you will notice that content streams differ in sizes based on the length of each tweets. And if we are implementing such as this in simplest way of doing this, we could be using UITableView’s heightForRowAtIndexPath delegate method and get/set the size of each string on the array:

For demonstrating this behavior, we’ll be creating a simple View based project that needs an NSMutableArray and a UITableView:

@interface TableTestViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *_quotes;
    UITableView *_tableView;
}
@end

And setting up our _tableView and adding some test data with varying length of characters:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _quotes = [[NSMutableArray alloc] initWithObjects:
               @"I do the very best I know how-the very best I can; and I mean to keep doing so until the end. If the end brings me out all right, what is said against me won't amount to anything. If the end brings me out wrong, ten angels swearing I was right would make no difference.",
               @"Be systematically heroic in little unnecessary points, do every day or two something for no other reason than its difficulty, so that, when the hour of need draws nigh, it may find you not unnerved or untrained to stand the test.",
               @"The trick of it, she told herself, is to be courageous and bold and make a difference. Not change the world exactly, just the bit around you. Go out there with your double-first, your passion and your new Smith Corona electric typewriter and work hard at ... something. Change lives through art maybe. Write beautifully. Cherish your friends, stay true to your principles, live passionately and fully well. Experience new things. Love and be loved if at all possible. Eat sensibly. Stuff like that.",
               @"I know of no more encouraging fact than the unquestioned ability of a man to elevate his life by conscious endeavor.", nil];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;        

    [self.view addSubview: _tableView];
}

Our _tableView is set up to be the same UIWindow height less the size of Status bar (480 – 20). And setting the current view implements its delegate. Then, add our _tableView as a subview to our main view.

Let’s implement _tableView’s delegate and dataSource:

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return [_quotes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* reuseIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.numberOfLines = 0;
    }

    cell.textLabel.text = [_quotes objectAtIndex:indexPath.row];
    return cell;
}
@end

Code above is pretty standard code for Table-view based application. The interesting code to notice is the numberOfLines = 0, this tells us that cell will contain a multi-line string with unknown size. If we forgot to set this property to 0, the default value of 1 will be in effect causing our text to display and truncated in a single line.

#pragma mark - UITableViewDelegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = [[_quotes objectAtIndex:indexPath.row]
                   sizeWithFont:[UIFont systemFontOfSize:14]
                   constrainedToSize:CGSizeMake(300, CGFLOAT_MAX)];
    return size.height + 10;
}

In order to achieve our dynamic height nature of our cells, heightForRowAtIndexPath will do the trick. So for each cell iteration, it also gets the size of the string including its font size to the count. The constrainedToSize parameter is the maximum acceptable size of the string. We set the height to be the constant max value of CGFLOAT but you can set it manually to a higher number, say 5000 or 9000. After we get the size of the current string on the cell, we added 10 more to the height for readability purpose.

A sample screen below gives us the output of the codes above:

UITableView_DynamicHeight

Quotes on the sample are taken at Quotations Page.

iOS Series: Recreating ABS-CBN News iPhone Application

With this iOS blog series, I will try to recreate existing application found on the AppStore that I assume is not natively developed in Objective-C codes but rather using 3rd party frameworks that produces/generates binaries specific to platform.

My target iOS application, ABS-CBN News is an iPhone-only application which I think is built using Titanium and a good candidate for this series for its simplicity. And we will try to examine components of this app and build our own from the extracted features. And also, we could try adding features not currently implemented if possible.

With this exercise, we will be exploring and using some popular opensource libraries to achieve our goals and to also help other developers who’s just starting in iOS development familiarize themselves to these awesome libraries.

On its core, ABS-CBN News has these features:
* A horizontal tab based menu for news category. Clicking each category will refresh the main table and lists all news focusing on that category.
* A live twitter feeds for @abscbnnews and its anchors and reporters. A read-only type view of individual tweets and no options to comment/public reply or even retweet the message.
* A settings page which the user can select what to appear on his/her “My News” category. User can also set total number of news per category. Maximum is 20.

This iOS Series blog post will be splitted into 3 parts:
Part 1: Building the basic UI components and defining its use.
Part 2: Creating Data Models and Consuming current REST Web Services
Part 3: Enhancing current features and adding/suggesting some.

Stay tuned for the first part of this series.

Horizontal Menu with UITableView Tap Status Bar not working

HorizMenu_ScreenshotYesterday, I downloaded the ABS-CBN News iOS app to see news and articles and let myself update on what’s going on. The application looks good and it feels natural, very intuitive as they presented their menus as a horizontal tab placed on the header and then the content is being changed on the UITableView. Even the UIView Activity Indicator on the bottom is non-intrusive and very well implemented.

But one thing I noticed, when I scroll news up to the bottom and scroll back on top by clicking the status bar, nothing happen, no matter how I try or select different tab, still it won’t work. And upon investigating, the problem is being caused by two UIScrollViews on the main view which prevents the UITableView from responding to scrollToTop.

 

As per Apple documentation:

/*
 this is for the scroll to top gesture. by default, a single scroll visible scroll view with this flag set will get the call. if there is more than one visible with this
 flag set or the delegate method returns NO, the view isn't scrolled 
 */
@property(nonatomic) BOOL  scrollsToTop;          // default is YES. if set, special gesture will scroll to top of view after consulting delegate

To fix the issue, I made a sample iOS application which uses the excellent opensource MKHorizMenu and use his demo application to add a UITableView and a logic that will check and set the scrollsToTop property to false if the UIScrollView implementation is not UITableView as seen below:

- (void)viewDidLoad
{
    self.items = [NSArray arrayWithObjects:@"Headlines", @"UK", @"International", @"Politics", @"Weather", @"Travel", @"Radio", @"Hollywood", @"Sports", @"Others", nil];
    [self.horizMenu reloadData];

    [super viewDidLoad];
    self.title = @"Tap Status";

    for (UIView* v in [self.view subviews])
    {
        if ([v isKindOfClass:[UIScrollView class]])
        {
            if ([v isKindOfClass:[MKHorizMenu class]]) {
                ((UIScrollView *)v).scrollsToTop = NO;
            }
        }
    }
}

Revised Demo application of MKHorizMenu is available for download here.

Hope this helps.

3 Smarter Ways to screen lock Snow Leopard

In a Windows world, Ctrl+Alt+Del or WinKey + L will lock your machine and bring up the login screen and this shortcut  is really a time saver and considered to be the quickest approach to lock your screen. But when you’re in a Mac world, there’s no such thing as Ctrl+Alt+Del that will lock your screen automatically and I just don’t understand why Apple doesn’t bother to consider adding one to their Snow Leopard.

But even the lack of the infamous shortcut in my Snow Leopard, I still managed to mimic the behavior that the Windows shortcut can offer to their users and I listed the 3 smarter ways to achieve the same task but with varying approach as listed below:

1. Using LockTight.
LockTight is a free tool that will sit on your System Preferences called LockTight that gives you quick access for assigning a shortcut key to activate your Desktop’s screensaver. So, if you’re the type of user that utilizes screen saver with a password on resume, this tool is for you.

Screenshot:
 LockTight_Screen02

Download and Installation:
First, download the .tbz file here and extract it via Terminal app like so:

   1: tar -xjvf <filename.tbz>

After extraction completed, go to the LockTight-0.1 Intel folder and just double-click the LockTight.prefPane to open up the System Preferences window and follow the on-screen instruction to install the AddIn.

LockTight_Screen01

2. Using Keychain Access

If you don’t like to hit any keystroke and just preferred to use the mouse, then using this method will be your choice. To lock screen your window using Keychain Access:

1. Go to /Utilities and click the Keychain Access. Or simply, Cmd+Space to search Keychain Access via Spotlight

2. Go to Preferences page as shown below

Screen shot 2011-06-19 at 12.27.39 AM

3. And just tick Show Status in Menu Bar check box. A lock icon will automatically be placed on your top menu bar on the right. When you click this icon, you will see that there’s a Lock Screen option. This command when clicked will make your screen black but when you touch your Trackpad/mouse or keyboard, a login prompt will be displayed to input your password to unlock your machine.

Screen shot 2011-06-19 at 12.27.23 AM

3. Using Automator

This is an advanced option but if you follow along, I guess you will be fine :) . The last approach involves creating a shell script that will turn into a Service and creating/assigning a shortcut on this service.

Steps:

1. Run Automator

2. Choose Service as a template

3. On the left pane, there’s a Library, select Utilities and select/drag the Run Shell Script on the empty right pane.

4. On the Server receives text on the upper window, select "no input" on the drop-down list.

5. Type the command script on the text area provided: Note: This is a one-liner script, don’t press Enter key

   1: /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

Automator
6. Click File/Save, name it as "Lock Computer" or "Lock Screen".

7. Exit Automator


Now to make a shortcut for this Service:

1. Go to System Preferences and click the Keyboard.

2. On the Keyboard Shortcuts tab, click Services on the left pane.

3. Scroll to the bottom list under General on the right panel until you see your Service named "Lock Computer" or "Lock Screen" as you name your service.

4. Select it, and just tap/click the far right of your selection to turn it into an editable textbox.

System Preferences

5. Hit your keyboard shortcut to activate this service. Mine is Command+Alt+L

6. Close the Preferences page.

Test your shortcut and voila, your screen will lock with 3D animation effect! Try it and you’ll love it :)

Of all these 3 smarter ways to lock your screen, my personal choice would be using the Automator as it feels like we mimic the way Windows does it, only much better with Snow Leopard with the added 3D animation/transition effect.

If you like this post, consider sharing this to others. Thanks and enjoy!

Easy RSS Syndication in .NET

My office colleague just asked me about the easy way to read and retrieve an RSS feed for a specific or multiple sites. And I say that Microsoft did indeed has a class called Syndication that resides under System.ServiceModel namespace that handles not only RSS but also Atom format too!

Below is a console application that I created to show how easy it is to read RSS via .NET:

Declare the import:

   1: using System.ServiceModel.Syndication;

Make sure you add the ServiceModel and ServiceModel.Web on your Reference.

live_writer_serviceModel

Basically, the main logic for reading RSS is to call the Url via the WebClient class and create an XmlReader object to hold/read the XML results being fetched on the WebClient request. And then, finally, just pass the XmlReader object to the Syndication object and it will parse all results into a feed collection.

live_writer_snippet01

and the Main() call would just be:

live_writer_snippet02

And when I run the project, the output will look similar on the screenshot below:

live_writer_rssConsole

Simple as that!

The RSSConsole Project File for this snippet is available for download here.

Make Your Online Presence Be Known

Just sharing you this awesome and wonderful idea online – http://about.me . Make your online profile be known for others to see.

Your Benefits:
• It’s your business card online
• No need to talk, your online profile is your portfolio
• Career highlights – good for potential employer seeking candidates
• No matter what you do professionally, you will feel at home here
• Empower your online presence
• Delight your friends, colleagues and family
• Making proud of your achievements
• Increase your network and potential clients.

So there it is, all you have to do now is go to their site and start gaining these benefits!

Start registering yourself now and make your online presence be known!

Telerik’s JustCode AddIn for Visual Studio

I just received an email from Telerik stating that I was one of the lucky first 500 people to submit an email to decompiler@telerik.com. And with this, Telerik gave me a $249 worth of product for FREE with Subscription and Priority Support.

So now, I will be enjoying using this wonderful JustCode addin tool for Visual Studio 2010!

From Telerik’s website:

Visual Studio Productivity Plug-in with Real Time Code Analysis

JustCode is an agile development add-in for Visual Studio 2005, 2008 and 2010 that enhances .NET development productivity by providing blazing fast solution-wide, on-the-fly code analysis and error checking, smart code navigation, and refactoring features as well as a Unit Test Runner. With a cross-language engine, JustCode provides features for C#, VB.NET, ASP.NET, XAML, JavaScript and HTML and supports multi-language solutions.

 

Thank you, Telerik!

Smug Appventures – There’s An App for That!

Together with my good friend, Dennis, we have set-up our website named Smug Appventures that will focus on the following ideas:

  • Mobile related and technology news including but not limited to iOS and Android
  • Showcase of FREE apps created by our fellow Filipinos
  • Developer article posting encourages other Pinoy devs to participate and share their insights.
  • Home grown applications under Smug Appventures.
  • Continuous improvements on the site itself.
    With this, you will see some of my posts here to appear on our site as well.
    If you know or would like to participate on this adventure, please drop us a line or just send us a tweet. Share your passion with us and let us both grow!

iOS: Parsing XML file using APXML Library

Recently, I just got the chance to work on an assignment that requires me to read XML file from the internet and display the fetch data on a simple UITableView. And to be honest, this is the first time I am parsing an XML documents as I am more familiar with the JSON format instead and JSON is fairly simple to work with on an iOS project using the great json-framework open-source library.

There are plenty of resources out there that will do the job very quickly and easily but I opted to try the clean and simple open-source library APXML that does exactly what I like to do – to parse an XML file with minimal effort.

First of all, I need to create a sample XML file which can be found here. This XML file  holds sample books with its title, price, authors and description on every node and has a single attribute that holds the bookId.

Content of our sample books.xml is like this:

01

Reading the XML file and place it in my NSString is pretty straightforward:

02

For simplicity, I just use ASCII encoding on line 6 but you can quickly change it to UTF if you need to. Just for the demo, this approach loads the XML synchronously and will block the main thread until the XML file has been loaded. And on line 11 is a private method that will call the APXML library to perform the actual parsing of the XML file like this:

03

With the above code, APDocument initializes with the given XML string and calls its rootElement to determine the root value which is the <catalog> in order tor the parser to determine the object for which it can get all the childElements under it. Child elements is an NSArray of APElement which we can iterate until the last <book> node is read.

APXML library gives you simple method call to get the attribute name:

   1: [child valueForAttributeNamed:@"id"]

Or getting the value of each element in a given node (<book>):

   1: NSLog(@"Author: %@", [[subElements objectAtIndex:0] value]);

After the method parseBooks has been executed, the usual UITableView will fill-up all the books from our dictionary:

04

And if you click on each cell, we can display the details of the book with our UIAlertView:

05

The output of our test project is something like below:

xml_21  xml_22

Haven’t tried other XML libraries for iOS yet, but I’m sure APXML will do most of the job on parsing XML file. You could try this cool library also!

Follow

Get every new post delivered to your Inbox.