Saturday, April 20, 2013

iOS Tip - Obtaining the height of a string to be displayed based on width, font and font size

A lot of times, you need to display a string of text on your iOS app UI. For example, you may need to create an image to contain a string of text (like displaying a bubble containing a SMS message) and hence need to calculate the height taken up by the text based on the width that you are using to display the text. The following code snippet calculates the height of text based on the width specified, as well as the standard system font and specified font size:


//---calculate the height needed to display the text
// based on given width, font, and font size---
-(CGFloat) textHeight:(NSString *) text withWidth:(int) viewWidth andSize:(int) fontSize{
CGSize maximumTextSize = CGSizeMake(viewWidth,9999);
CGSize expectedTextSize = [text sizeWithFont:[UIFont systemFontOfSize: fontSize]
                               constrainedToSize:maximumTextSize
                                   lineBreakMode:NSLineBreakByWordWrapping];
return expectedTextSize.height;
}


To use the textHeight:withWidth:andSize: method, simply call it like this:


    float textHeight = [self textHeight:@"The quick brown fox jumps over the lazy dog" 
                              withWidth:300 
                                andSize:15];


Have fun!

Android Tip - Waking up screen and unlocking the device

In Android, there are times where you need to programmatically unlock the device and wake up the screen. For example, you might have a broadcast receiver that receives some broadcasts in the background. When a broadcast is received, you want to wake up the screen (which is likely to be off as the device goes to sleep) and display a notification to the user. In addition, if your device is locked, you would also need to programmatically unlock it. The following code snippet will unlock your device and wake up the screen:


import android.view.Window;
import android.view.WindowManager.LayoutParams;

...

        Window window = this.getWindow();
        window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

Give it a try and have fun!

Tuesday, April 16, 2013

Mac courses - Developing iBooks using iBooks Author & Using iPad to Create Stunning Keynote Presentation Slides

In collaboration with Civil Service Club (CSC), I will be running two Mac courses:


Schedule at a Glance
Course
Date
Fee (S$)
MAC101 – Developing iBooks using iBooks Author
·      18 May 2013
·      Sat
·      9am to 12noon
·      $60 for CSC members
·      $70 for public officers
·      $80 for members of the public
MAC103 – Using iPad to Create Stunning Keynote Presentation Slides
·      18 May 2013
·      Sat
·      2pm to 5pm
·      $60 for CSC members
·      $70 for public officers
·      $80 for members of the public

For course outlines, please click here.
 

Learn how to write Android Apps - 11-12 May 2013

I am happy to schedule a weekend run for the Foundation of Android Programming class. For those of you who are keen to learn Android programming on your own weekend, this is the best chance to get started in just 2 days! Set your calendar to 11-12 May 2013. The usual fee of the course is S$997 (nett), but I would like to give those early birds a S$100 discount if you register before 30 April 2013. And if you bring along a friend, you get an additional $50 discount on top of the early bird discount. So hurry, register now as places are limited!
Course outlines and application form here.

Venue
Bayview Hotel
30 Bencoolen Street
Singapore 189621

Time
9am to 5pm

Wednesday, April 10, 2013

Android Tip: Signing your App

Once you have developed your Android app, the next step would be to export your application as an Android Package (APK). When you run your application on an Android Emulator or a real device connected to your computer, your application is code-signed using a debug certificate called debug.keystore. Apps that are signed using this debug.keystore can only be run on the emulator as well as on real devices (deployed through Eclipse). When you you are ready to release your app to the world, you need to sign it using your own certificate (or a cert issued by Google).

Very often, developers forgot to sign the app and went ahead to distribute the apps. What will happen it that when the user tries to install the unsigned APK, they will get the "App not installed" error. Obviously this error message is not very useful as it does not tell you much of the source of the problem. 

As a developer, you can perform the following steps to further examine the source of the problem. Fire up your Command window (or Terminal for Mac users). Issue the following command:

jarsigner -verify -verbose -certs name_of_APK.apk 

If the application is not signed, you will see something like:

  s = signature was verified 
  m = entry is listed in manifest
  k = at least one certificate was found in keystore
  i = at least one certificate was found in identity scope

no manifest.
jar is unsigned. (signatures missing or not parsable)

Applications that are unsigned cannot be installed on real devices. 

If the application is signed with the debug.keystore certificate, you will see something like this:

sm       652 Wed Mar 27 13:17:20 SGT 2013 res/layout/activity_main.xml

      X.509, CN=Android Debug, O=Android, C=US
      [certificate is valid from 3/19/12 8:23 PM to 3/12/42 8:23 PM]

sm       464 Wed Mar 27 13:17:20 SGT 2013 res/menu/main.xml

      X.509, CN=Android Debug, O=Android, C=US

Look for the "CN=Android Debug" phrase. This tells you that the APK is signed with the debug.keystore certificate, which means that it cannot be installed on a real device (unless through Eclipse). 

Wednesday, April 03, 2013

Android Tip - Updating Your UI Safely on a Separate Thread

Very often, in your Android applications, you have several threads running in the background. And sometimes you need to update your UI (or perhaps display a Toast) from a background thread. In this case, you need to take special care to ensure that the update is done in a thread-safe manner. If not, your application will simply crash.

To update your UI in a thread-safe fashion, use the runOnUiThread() method:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getApplicationContext(),
            "Some message to display", Toast.LENGTH_LONG).show();
    }
});

Tuesday, March 19, 2013

Android Specialist Training Roadmap

If you want to be a specialist in Android programming, check out our Android Specialist Training Roadmap for the month of April and May 2013.

Download application forms here.




iOS Specialist Training Roadmap

If you want to be a specialist in iOS programming, check out our iOS Specialist Training Roadmap for the month of April 2013.

Download application forms here.


New Course - Writing NFC (Near Field Communication) Android Apps

NFC is set of short-range wireless technologies that allow devices to communicate with each other, typically at a distance of 4cm or less.   Using NFC, you can enable two Android devices to exchange data. In this course, you will learn how to develop Android applications that support NFC. In addition, you will also learn how to write peer-to-peer applications using the Wi-Fi Direct feature in Android 4.0. 

Topics

* Understanding NDEF (NFC Data Exchange Format)
* Reading NDEF data from an NFC tag
* Using Android Beam
    Beaming NDEF messages from one device to another
* Project – Exchanging contact details
* Using WiFi-Direct
    Discovering Peers
    Connecting to Peers
    Exchanging Data between Peers
* Project – Developing a file exchange application

Date
* 21 May 2013
* 18 Jun 2013

9am to 5pm

Download course application form here.

Monday, March 18, 2013

New Course - Writing Secure Android Apps


Security is one of the major concerns of any mobile applications developers. In the case of Android, the openness of Android comes with a price, and this price is that of security.  In this course, you will learn how to secure your apps so that the data is not exposed to unauthorized users and apps. You will also learn how to protect your apps on the Google Play so that you do not become a victim of software piracy.  

Topics
* Understanding Application Isolation
* Preventing your Activities from being called by other apps
* Defining your own permissions
* Implementing Hashing
* Implementing Encryption and Decryption
    Using AES for symmetric encryption/ decryption  
    Using RSA for asymmetric encryption/decryption
* Saving data in Internal Storage vs. SD Card
* Specifying Separate Read and Write Permissions for Content Providers
* Preventing SQL Injection
* Communicating with Web Services Securely
* Preventing your Apps from Piracy using Google Play Licensing
* Using the KeyChain API

Date
* 19 Apr 2013
* 23 May 2013
* 14 Jun 2013

9am to 5pm

Download course application form here.