Where to store custom templates in Xcode 4.3 ?


Since Xcode 4.3 onwards is a packaged application, you won’t be able to go to /Library/Developer/Xcode folder anymore.

Instead use the path:

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/

to save any custom project template you want to store.

I spent some time investigating this as i was following the iOS heroku tutorial and it still refers to the old location.

https://devcenter.heroku.com/articles/ios-core-data-buildpack-app

,

Leave a comment

Using GCD with Blocks


GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects.

Queues are a C typedef viz. dispatch_queue_t. GCD offers three kinds of queues, main, concurrent and serial.

Main queue is automatically created by the system and associated with your application. You can get it by using dispatch_get_main_queue().

Concurrent queue can be created using dispatch_get_global_queue and it executes the tasks, submitted in the form of block in FIFO order, however, the tasks run concurrently and can finish in any order.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

 

Serial queues execute tasks in a strict FIFO serial order one at a time. To create serial queue use dispatch_queue_create which takes the name of the queue as first argument and options (currently NULL) as second argument.

dispatch_queue_create(“com.example.MyQueue”, NULL);

Submitting a block to Queue for Execution

You can submit a block to queue for execution using two of GCD provided functions:

dispatch_async OR dispatch_sync

Both the functions accept queue as the first argument and block as the second argument. dispatch_async returns immediately and executes the task asynchronously while dispatch_sync blocks execution until the provided block returns.

One good demonstration of using GCD API’s with concurrent and main queue and blocks is this blog entry from Jeff Kelly’s blog:

http://blog.slaunchaman.com/2011/02/28/cocoa-touch-tutorial-using-grand-central-dispatch-for-asynchronous-table-view-cells/

I also like Andy’s blog entry on testing or debugging block based API’s using the code snippet he created:

http://mrox.net/blog/2011/08/04/a-tip-for-testing-block-based-apis/

In fact, you can use Jeff’s idea of async loading of table cells along with Andy’s code snippet to test the async loading.

 

 

 

 

 

Leave a comment

Objective C – Some basics


If you are developing for the iOS platform, its invaluable to have foundational knowledge in Objective C. This brief blog posts covers some fundamental terms in objective C, hope you will find it useful.

1. Protocols – Protocols in objective C are equivalent to Interfaces in Java. They contain method definitions, either required or optional methods, which the implementing class implements. A good example is UITableViewDataSource Protocol. A class can implement multiple Protocols. For more details, refer to :

https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-SW1

2. Categories – A category allows you to add methods to an existing class even if you do not have a the source code available for that class! Categories offers a neat alternative to subclassing by allowing you to extend the functionality of a class. For example, to add functionality to class named MyCategoryClass you can write the following code:

#import “MyCategoryClass.h”

@interface MyCategoryClass (CategoryName)

//method declarations

@end

 

The category name indicates that the methods are additions to a class declared elsewhere, not a new class. Note that you cannot use a category to add additional instance variables to a class.

3. Properties – Properties provide a simple way to declare and implement an object’s accessor methods. There are two key parts to understanding properties in objective C, declaration and implementation.

You would normally declare a property in class header file.

Property Attributes —

Mutability – readwrite or readonly, default is readwrite

e.g. @property (readonly) int voltage;

Lifetime Specifiers — unsafe-unretained (default) e.g. @property int voltage (voltage in this case is unsafe unretained)

strong — ensures that a strong reference or ownership is kept to the passed-in object.

weak — does not imply ownership of the object pointed to. E.g. in case of parent child relationship, parent should have a strong reference to child, however, the child should have a weak reference. So that when child is deallocated, the parent reference to child can be released.

copy — in this case the setter will make a copy of the new object passed and set the pointer to point to the copied object.

e.g. @property (copy) NSString *serialNumber;

– (void) setSerialNumber: (NSString *) newSerialNumber {

newSerialNumber = [newSerialNumber copy];

[serialNumber release]; // releasing the original pointer

serialNumber = newSerialNumber;

}

4. Key Value Coding

KV is the ability to set and read property using its name. e.g. if we have a property name productName declared in a class whose instance is ‘a’, we can use the following code:

[a setValue@”Washing Machine” forKey@”productName”];

 

 

 

 

Leave a comment

Android 101 – The basic building blocks of an Android Application


This post talks about the basics and fundamentals of Android development. So if you know java and eager to program for the Android platform, read on.

I recently developed an iOS app for a large organization and published it on itunes. Received some good feedback, however, turns out that half of the users have Android phones. So i started looking at developing an Android version of the iOS app. Plus according to Google, more than 350,000 android devices are being shipped on any given day which translates to 1 million new Android users every 3 days!

Compared to iPhone, Android has quite a few vendors shipping different kinds of Android phones (LG, Samsung, Motorola, HTC, etc.). However, good news is that now there is a one common platform on which to develop Android apps – the latest one is called honeycomb🙂

So lets start with the basics. And Android app is a collection of several different components viz. Activity, Service, Content Provider, Broadcast Receiver, Intents; all of them are defined in AndroidManifest.xml.

1. Activity – This is how you write an Android user interface. A single activity defines a single screen with a user interface. When you write a user interface for your home screen it will most likely extend the Activity class. It defines simple lifecycle methods like onCreate, onResume, onPause which lets you handle interruptions.

2. Service – it does the majority of processing for your application. Its the logic behind the user interface. E.g. performing a long running process in the background. You can sub class the Service class to write your own custom service.

3. Content Provider – This provides a uniform singular interface to the content & data. The content can be from your app being exposed to the system or other apps or it could be system content e.g. pictures on your phone, contact lists exposed or available to your app via the Content Provider interface. It provides a consistent interface to retrieve/store data via RESTful model supporting CRUD operations.

4. Broadcast Receiver – The application modules communicate with each other using the broadcast receiver. Messages can be initiated by the system or the app. E.g. if a user is using your app and a phone call comes in, the Broadcast Receiver interface will notify your app and your can code to handle that interruption (by possibly delegating to your service). You can opt to receive messages statically via declaring in the manifest file or dynamically by programmatically registering it the code which extends the Broadcast Receiver interface.

5. Intents – Of all the main components we discussed so far, this is the most interesting. Intents can be implicit or explicit. E.g. an explicit intent can be to invoke another screen when a button is pressed on the Activity (UI) in context. An implicit intent is when you create an intent and hand it off to the system to handle it. A good example discussed in one of google I/O boot camp was the barcode example, where the app creates an implicit intent of type SCAN and provide it to another app which handles scanning and returns the resultCode back to the calling application.

Typically you define all the components of your Android app in the manifest file. You can state the features, permissions and sdk version in the manifest. The application tag will have sub tags defining activities, services, receivers and intents.

Having discussed the main components, lets move on now to our first basic task – creating a simple user interface.

In the Android World a UI is a hierarchy of Views and ViewGroups which can be defined either programmatically or in XML layout files. E.g. to create a simple linear layout i can define the following in my xml file (e.g. main.xml under the layout folder of your application resource package):

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout …>

<TextView android:id=”@+id/text”

android:text=”Hello World!” …/>

</LinearLayout>

Though we used linear layout here, its considered best practice to use RelativeLayout to cater for multiple size screens.

In the next post we will use the above discussed concepts to build a simple Android application.

Feedback and Questions are appreciated.

, , , , , ,

Leave a comment

Android 101 – The basic building blocks of an Android Application


This post talks about the basics and fundamentals of Android development. So if you know java and eager to program for the Android platform, read on.

I recently developed an iOS app for a large organization and published it on itunes. Received some good feedback, however, turns out that half of the users have Android phones. So i started looking at developing an Android version of the iOS app. Plus according to Google, more than 350,000 android devices are being shipped on any given day which translates to 1 million new Android users every 3 days!

Compared to iPhone, Android has quite a few vendors shipping different kinds of Android phones (LG, Samsung, Motorola, HTC, etc.). However, good news is that now there is a one common platform on which to develop Android apps – the latest one is called honeycomb🙂

So lets start with the basics. And Android app is a collection of several different components viz. Activity, Service, Content Provider, Broadcast Receiver, Intents; all of them are defined in AndroidManifest.xml.

1. Activity – This is how you write an Android user interface. A single activity defines a single screen with a user interface. When you write a user interface for your home screen it will most likely extend the Activity class. It defines simple lifecycle methods like onCreate, onResume, onPause which lets you handle interruptions.

2. Service – it does the majority of processing for your application. Its the logic behind the user interface. E.g. performing a long running process in the background. You can sub class the Service class to write your own custom service.

3. Content Provider – This provides a uniform singular interface to the content & data. The content can be from your app being exposed to the system or other apps or it could be system content e.g. pictures on your phone, contact lists exposed or available to your app via the Content Provider interface. It provides a consistent interface to retrieve/store data via RESTful model supporting CRUD operations.

4. Broadcast Receiver – The application modules communicate with each other using the broadcast receiver. Messages can be initiated by the system or the app. E.g. if a user is using your app and a phone call comes in, the Broadcast Receiver interface will notify your app and your can code to handle that interruption (by possibly delegating to your service). You can opt to receive messages statically via declaring in the manifest file or dynamically by programmatically registering it the code which extends the Broadcast Receiver interface.

5. Intents – Of all the main components we discussed so far, this is the most interesting. Intents can be implicit or explicit. E.g. an explicit intent can be to invoke another screen when a button is pressed on the Activity (UI) in context. An implicit intent is when you create an intent and hand it off to the system to handle it. A good example discussed in one of google I/O boot camp was the barcode example, where the app creates an implicit intent of type SCAN and provide it to another app which handles scanning and returns the resultCode back to the calling application. 

Typically you define all the components of your Android app in the manifest file. You can state the features, permissions and sdk version in the manifest. The application tag will have sub tags defining activities, services, receivers and intents.

Having discussed the main components, lets move on now to our first basic task – creating a simple user interface.

In the Android World a UI is a hierarchy of Views and ViewGroups which can be defined either programmatically or in XML layout files. E.g. to create a simple linear layout i can define the following in my xml file (e.g. main.xml under the layout folder of your application resource package):

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout …>

   <TextView android:id=”@+id/text”

                   android:text=”Hello World!” …/>

</LinearLayout>

Though we used linear layout here, its considered best practice to use RelativeLayout to cater for multiple size screens.

In the next post we will use the above discussed concepts to build a simple Android application.

Feedback and Questions are appreciated.

Leave a comment

Installing Heroku CLI and Git …in 10 minutes!


If you are planning on playing with heroku, this post will help you get started in setting up your local machine with heroku CLI (command line interface) and Git (open source cloud based version control system).

Note that this post is a simplified and more detailed version of heroku quickstart article available at :

http://devcenter.heroku.com/articles/quickstart

Update — If you are using Mac OSX, you might want to just download the Heroku Toolbelt (http://toolbelt.heroku.com) The toolbelt (heroku CLI & Git ) will provide you everything you need to get started using Heroku!

Steps

1. Download Git for your OS and sign up for free Git account http://git-scm.com/download

2. A Heroku user account. Signup is free and instant.

3. Since heroku CLI is a ruby gem, in order to install it, you will need to install ruby locally on your machine.Installing the latest Ruby version with RVM is simply done by typing :

rvm install 1.9.3.

Note that if you do not have rvm installed, you can install it on mac/linux by:

$ bash curl -s https://rvm.beginrescueend.com/install/rvm.

You might need to prefix the    command using sudo ($ sudo curl -s https://rvm.beginrescueend.com/install/rvm)

4. Ensure that you have ruby installed by typing ruby –version

5. Using command line install heroku by typing:

$gem install heroku

Note that this will prompt you for your heroku credentials (which you used at step 2). After this step is finished your email address and API token will be saved to ~/.heroku/credentials

Also note that you might want to prefix the command with sudo (sudo gem install heroku) to make the command write the gem to the destination directories. If this still fails, try running:

$sudo gem install addressable

and then execute $sudo gem install heroku

6. It’s generally a good idea to add your public key immediately after installing the heroku gem so that you can use git to push or clone Heroku app repositories:

$heroku keys:add

sample output:

Enter your heroku crendentials:

Email: ssharaf@salesforce.com

password:******

Uploading ssh public key /Users/ssharaf/.ssh/id_rsa.pub.

7. At this point, you should be ready to start playing with the coolest PAAS (platform as a service) aka heroku!

Note that for application development you might want to install maven and OpenJDK v6 (for Java)

, , , , ,

Leave a comment

JavaScript – Using AJAX with JSON to retrieve weather information


JavaScript has been around for over a decade now. Recent years has seen a growth in its popularity especially with the introduction of AJAX, JSON and some other frameworks and libraries e.g. dojo.

In this post, we will create a simple web page which will display a simple form for a user to enter zip code. Based on the zip code entered, we will show how to make an AJAX call to retrieve weather information from Yahoo in JSON data format.

Step 1 –

If you are using Eclipse, create an empty web project. In case if you want to use maven, you can use the following command to create a simple java web project:

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DartifactId=JSWebApp -DgroupId=com.ssharaf.webapp -Dversion=1.0-SNAPSHOT -Dpackage=com.ssharaf.webapp

The above command will create a web project with name JSWebApp (as specified in the maven command line parameter -DartifactId=JSWebApp).

Navigate to
JSWebApp/src/main/webapp

Step 2 –

And open index.html in your favorite editor (mine is VI). Replace the existing content in the file by the one below:

<html>

<head>

<script src=”ajaxlab.js” type=”text/javascript”></script>

</head>

<body>

<h1>Weather Portlet</h1>

Enter zip code to find weather:<br/>

<input type=”text”></input>

<button>Submit</button>

<div id=”output”></div>

</body>

</html>

Now add the following javascript file to the location (JSWebApp/src/main/webapp).

====================begin javascript code======================

ajaxlab = {};

ajaxlab.query = “select * from weather.forecast where location=”;
//append zipcode to query to get proper YQL

ajaxlab.yahoourl = “http://query.yahooapis.com/v1/public/yql&#8221;;

ajaxlab.getJSONP = function(url, params, callback) {
var scriptTag = document.createElement(“script”);
scriptTag.type = “text/javascript”;
var functionName = “x”+(new Date().getTime());
var firstParam = true;
for (var key in params) {
url += (firstParam ? ‘?’ : ‘&’) + key + ‘=’ + params[key];
firstParam = false;
}
scriptTag.src = url + “&callback=ajaxlab.” + functionName;
ajaxlab[functionName] = function(data) {
callback(data);
delete ajaxlab[functionName];
}
document.body.appendChild(scriptTag);
}

ajaxlab.start = function() {
document.getElementsByTagName(“button”)[0].addEventListener(“click”, function(e){
var zip = document.getElementsByTagName(“input”)[0].value;
ajaxlab.getJSONP(ajaxlab.yahoourl, { q : encodeURIComponent(ajaxlab.query + zip), format:”json” }, ajaxlab.render);
});
}

ajaxlab.render = function(result) {
console.log(result);
result.query.results.channel.description
document.getElementById(“output”).innerHTML =

” + result.query.results.channel.description + “


+ result.query.results.channel.item.description;
}

window.onload = ajaxlab.start;

====================end javascript code======================

Step 3 –

Add the following jetty plugin to your project’s pom.xml file:

<build>
<plugins>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.17</version>
</plugin>
...
</plugins>
</build>

Once the Jetty plugin has been set up in the project POM file, the jetty:run goal is available:

$ mvn jetty:run

When running the preceding command for the first time, Maven will automatically download all dependencies and start the Jetty plugin.

Code View: Scroll / Show All
[INFO] Starting jetty 6.1.26 ...
2010-11-16 07:37:00.135:INFO::jetty-6.1.26
2010-11-16 07:37:00.247:INFO::No Transaction manager found - if your webapp requires one, please configure one.
2010-11-16 07:37:00.484:INFO::Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

Now testWebApp is running on localhost:8080/JSWebApp!

, , ,

Leave a comment