Wednesday, July 18, 2012

JavaScript - Singleton Pattern

The singleton design pattern is probably the simplest and most common pattern in JavaScript.

So why should you use the singleton pattern?
  • Encapsulation of members & functions
  • Creates its own Namespace
  • A singleton is a single instance object
  • Encourages code reuse
  • Improves readability because you can logically organise your code

The point of a singleton is to only have one instance. A shopping cart is a good example of something that you may want only a single instance of at one time.


The simplest form of Singleton is an object literal. This loose form of Singleton cannot be instantiated. All of the members are now accessible through the Singleton variable, accessible through dot notation.
varmyCart = {
   self: this,

   totalCost: 0,

   totalQty: 0,

   cart: {},

   getCart: function(){ },

   updateCart: function(){ }

};

alert( "Total cost: $"+ myCart.totalCost );

By using closures, we can create Private and Public Members and Functions. This pattern is more commonly called the Module Pattern.

 var myCart = function(){
    // Private

    var self = this;

    var totalCost = 0;

    var totalQty = 0;

    var cart = {};

    return {

        // Public

        getCart: function(){ },

        updateCart: function(){ }

    };

}();

HTML5 localStorage

Persistent Client-Side Key-Value Pairs

The localStorage object has four primary method:
  • localStorage.clear()

  • localStorage.getItem( key )

  • localStorage.removeItem( key )

  • localStorage.setItem( key, value )

When dealing with the localStorage object, it is important to remember that, like cookies, you are dealing with string values. Both the key and the value arguments must be strings. And, while the browser might not throw an error if you pass-in non-string values, you'll quickly find that it is simply calling the toString() method implicitly.

Run the following sample code and check the console:
<html>
<head>
<script type="text/javascript">
console.log("tim in localStorage —",("tim" in localStorage));
console.log("localStorage.getItem( tim ) —",localStorage.getItem( "tim" ));
localStorage.setItem( "tim", "nice" );
localStorage[ "bad" ] = "awesome";
console.log("tim in localStorage —",("tim" in localStorage));
console.log("localStorage.getItem( ‘bad’ ) —",localStorage.getItem( "bad" ));
var myObject = {};
localStorage.setItem( "myObject", myObject );
console.log("typeof( myObject ) —",typeof( localStorage.getItem( "myObject" )));
localStorage.removeItem( "tim" );
delete localStorage[ "bad" ];
console.log("tim in localStorage —",("tim" in localStorage));
console.log("bad in localStorage —",("bad" in localStorage));
var clientId;
var id = localStorage.getItem(clientId);
if (!id) {
id = Math.floor(Math.random()*10000);
localStorage[clientId] = id;
}
var x = "new";
localStorage[x] = "killer";
console.log(localStorage.getItem(x));
</script>
</head>
<body></body>
</html>

Monday, July 9, 2012

Download entire website from terminal using wget

This will help you to download entire website along with the links to other pages.i.e, you can almost browse the website offline. First install wget(if not present). Then run this from terminal.
wget --mirror -p --convert-links -P ./LOCAL-DIR WEBSITE-URL

–mirror : turn on options suitable for mirroring.
-p : download all files that are necessary to properly display a given HTML page.
–convert-links : after the download, convert the links in document for local viewing.
-P ./LOCAL-DIR : save all the files and directories to the specified directory.

Friday, July 6, 2012

How to format USB or external drive in Ubuntu

If you could find the device on the Launcher, just right-click and select "format". Otherwise, see this....

You can use the disk utility(installed by default) to format the drive. There is an option 'format volume' select that option.
How to format USB or external drive in Ubuntu

How to format USB or external drive in Ubuntu

How to format USB or external drive in Ubuntu

How to format USB or external drive in Ubuntu

How to format USB or external drive in Ubuntu

Thursday, July 5, 2012

Taking Screenshots in Mac OS X

  • Command-Shift-3: Take a screenshot of the screen, and save it as a file on the desktop

  • Command-Shift-4, then select an area: Take a screenshot of an area and save it as a file on the desktop

  • Command-Shift-4, then space, then click a window: Take a screenshot of a window and save it as a file on the desktop

  • Command-Control-Shift-3: Take a screenshot of the screen, and save it to the clipboard

  • Command-Control-Shift-4, then select an area: Take a screenshot of an area and save it to the clipboard

  • Command-Control-Shift-4, then space, then click a window: Take a screenshot of a window and save it to the clipboard

In Leopard and later, the following keys can be held down while selecting an area (via Command-Shift-4 or Command-Control-Shift-4):

  • Space, to lock the size of the selected region and instead move it when the mouse moves

  • Shift, to resize only one edge of the selected region

  • Option, to resize the selected region with its center as the anchor point

Submitting Your App to AppStore

Submitting your app to the App Store is a multistep process involving several tools. First, create a distribution provisioning profile using iOS Provisioning Portal. Then create an archive, validate it, and submit it to the App Store using Xcode. When your app is approved, set the date the app will be available to customers using iTunes Connect. Finally, don’t forget to respond to user issues after you ship your first version.

Before You Begin

Before starting, you should have a distribution certificate and the status of your iTunes Connect app record should be “Waiting for Upload” or later, as described in “Creating Your App Record in iTunes Connect.”

Now, create a distribution provisioning profile using the iOS Provisioning Portal.

Create a Distribution Provisioning Profile

When the app is ready for publication, you create a distribution provisioning profile by selecting App Store as the method of distribution. The steps are similar to creating an ad hoc provisioning profile for testing except that you select an app ID only. You do not select any signing certificates or device IDs.

To create a distribution provisioning profile . . .
  1. Log in to the iOS Provisioning Portal from the Member Center.
  2. Select Provisioning in the sidebar.
  3. Select the Distribution tab.
  4. Click New Profile.
  5. Select App Store as the distribution method.
    Submitting Your App to AppStore

  6. Enter a profile name.
  7. Confirm that your distribution certificate is displayed.
  8. Choose app ID.
  9. Click Submit.

    When the status of the ad hoc provisioning profile changes from Pending to Active, you can begin using it. You may need to refresh the webpage in the iOS Provisioning Portal to see the status change. If you are using Safari, select View > Reload Page.

Configuring Your Project for App Store Submission

To submit your app for publication on the App Store, you need to build the app using your team’s distribution code signing identity.

To configure your project to facilitate the building of distribution-identity–signed archives of your app:
Submitting Your App to AppStore
  1. In your project, duplicate the Release build configuration and name the duplicate “AppStore”.
  2. Set the Code Signing Identity build setting to your team’s distribution code signing identity through the app’s distribution provisioning profile for the AppStore build configuration.
  3. In the target, delete the Validate Build Product build setting specification for the Release build configuration.
  4. Goto manage schemes and duplicate the scheme that builds your app and name the duplicate  “<App_Name>-AppStore”.
  5. In the AppStore scheme, set the build configuration for the Archive action to AppStore.

Create and Validate the Archive


To create the archive . . .
  1. Select the Xcode project window.
  2. Choose iOS Device from the scheme toolbar menu.



  3. Choose Product > Archive.
    The Archives organizer appears and displays the new archive.
To validate the archive . . .
Submitting Your App to AppStore
  1. In the Archives organizer, select the archive.
  2. Click the Validate button.
  3. Enter your iTunes Connect credentials and click Next.
  4. Select the app you want to share and the appropriate signing identity, and click Next.
  5. Review validation issues found, if any, and click Finish.
You need to fix any validation issues, create a new archive, and validate it again. You cannot proceed until the archive passes the validation tests.

Submit the Archive

Only after passing validation tests, can you submit your app to the App Store. In fact, Xcode validates your archive again during the submission process.
If you find "submit" button, click on it and give the "iTunes Connect" credentials and select the corresponding distribution provisioning profile.Else, follow this procedure



To submit the archive to the App Store . . .
Submitting Your App to AppStore
  1. In the Archives organizer, select the archive.
  2. Click the Distribute button.
  3. Select “Submit to the iOS App Store” and click Next.
  4. Enter your iTunes Connect credentials and click Next.
  5. Select the app you want to share and the appropriate signing identity, and click Next.
    Xcode runs the validation tests again. If issues are found, click Cancel and fix them before continuing.
    Submitting Your App to AppStore


  6. Enter a filename and location for the App Store package and click Save.
Xcode transmits the archive to Apple, where it is examined to determine whether it conforms to the app guidelines. If the app is rejected, correct the problems that were brought up during app approval and resubmit it. Before you submit the app, you should read iOS Human Interface Guidelines and App Store Review Guidelines for iOS Apps to avoid problems.

Ship Your App

Use iTunes Connect to set a date when the app is available on the App Store. For example, you can choose a date that immediately releases the app to the App Store after it is approved, or you can set a date for sometime in the future. Using a later availability date allows you to arrange other marketing activities around the launch of your app.
To set the availability date . . .
Submitting Your App to AppStore
  1. Log in to iTunes Connect .
  2. Select Manage Your Applications.
  3. Select your app in iOS App Recent Activity.
  4. Click Rights and Pricing.
  5. Choose a date from the Availability Date pop-up menus.
  6. Optionally, edit the other fields on this form.
  7. Click Save.
Changes you make to Rights and Pricing go live immediately (expect 24 hours for a full refresh of the changes on the App Store).

Creating Your App Record in iTunes Connect

When an app is sold in the App Store, the store displays a lot of information about the app, including its name, a description, an icon, screenshots, and contact information for your company. To provide that information, you log in to iTunes Connect, create a record for the app, and complete some forms. In this chapter you learn how to go through this process.

Note: Normally, you create your iTunes Connect app record late in the development process because there’s a time limit from when you create the record to when you must submit your app. However, some Apple technologies, including Game Center and In-App Purchase, require that an iTunes Connect record to be created earlier. For example, with In-App Purchase, you need to create the app record so that you can add the details of the items you want to sell. This content needs to be created before the development process is complete so that you can use it to test the code you added to implement In-App Purchase.

Before You Begin

Before you begin, make sure that you have these assets ready to enter into the forms:
  • The date when you want to ship your app (you can set the latest date allowed and change it later)

  • A brief description of your app that iTunes will display to customers

  • An app icon (512 x 512 pixels) ready for upload

  • At least one screenshot of your app ready for upload

  • An internal version number for your submission

  • A bundle ID that you've set to match your app ID
Therefore before you can create the actual iTunes Connect app record, you need to accomplish three main tasks, using Xcode:
  1. Capture screenshots.

  2. Set the launch image.

  3. Set your bundle ID.

Capture Screenshots

Using Xcode, you can capture screenshots of your app on a device and save the images on your desktop to upload to iTunes Connect later and use as a launch image.

To capture a screenshot on your device . . .
  1. Connect the device to your Mac.
  2. In Xcode, run your app on the device (as described in “To launch the app on the device”).
  3. Configure the app on the device the way you want it.
  4. In Xcode, open the Devices organizer.
  5. In the Devices section, click the disclosure triangle next to the iOS device.
  6. Select Screenshots .
  7. Click New Screenshot in the lower-right corner.


Creating Your App Record in iTunes Connect

To save a PNG file of a screenshot, drag the screenshot from Xcode to the desktop. Optionally, move the screenshots to a folder so that you can keep them all in one place.

Set the Launch Image

Besides capturing screenshots, you should set a launch image that acts as a placeholder for your application’s user interface at launch time. Do this at the same time that you capture screenshots for iTunes Connect.
To set your launch image . . .
  1. In the Xcode Devices organizer, select the screenshot in the Screenshots folder of the Library section.
  2. Select “Save as Launch Image.”
  3. Specify the name of the image and the target app, and click Next.
    Creating Your App Record in iTunes Connect
After performing these steps, the corresponding launch image is set in the Summary pane in Xcode. For example, if the screenshot was captured on an iPhone 4, the screenshot now appears in the Summary pane as the Retina Display launch image in iPhone/iPod Deployment Info.

Set the Bundle ID to Match the App ID

The record in iTunes Connect also includes a field for a bundle ID; the value you place in this field must exactly match the bundle ID for the app. The app name and version you enter in iTunes Connect must also match the Xcode project configuration.

You set the bundle ID for your app when you create your Xcode project as described in “Creating an Xcode Project.” For your convenience, the bundle ID defaults to a reverse-domain name using the company identifier followed by the product name that you entered when you created your Xcode project—for example,com.charlestaylor.HelloWorld, where com.charlestaylor is the company identifier and HelloWorld is the product name. The bundle ID is actually set in theInfo.plist file of your Xcode project and can be changed to any identifier that follows the RFC 1034 specification.

The bundle ID that you enter in iTunes Connect cannot be changed after the first version of your app is approved or you enabled some specialized technologies, such as iCloud storage or push notifications. Therefore, you should pick the final bundle ID for your app now.

To set your bundle ID in the Info.plist file . . .
  1. In the Xcode project navigator, select the project.
  2. Select your target in the Targets section, in the second column, to display the project editor.
  3. Click the Info tab.
  4. Enter the bundle ID in the Value column of the “Bundle identifier” row.

Create the iTunes Connect App Record

Next, you create the actual iTunes Connect app record and complete some forms. After you finish this process the status of your app is “Prepare for Download.” You need to answer additional questions about export compliance before the status changes to “Waiting for Upload.” The app record status needs to be at least “Waiting for Upload” to submit your app to the App Store.


To create an iTunes Connect app record . . .
  1. Log in to iTunes Connect (described in “To go to iTunes Connect”).
  2. Select Manage Your Applications.
  3. Click Add New App.
    Creating Your App Record in iTunes Connect
  4. Complete the forms by inserting your app's information.
  5. Click Done.
    Creating Your App Record in iTunes Connect
To complete the export compliance question . . .
  1. In iTunes Connect, select Manage Your Applications.
  2. Select your app.
  3. Click View Details.
    Creating Your App Record in iTunes Connect
  4. Click “Ready to Upload Binary.”
  5. Answer the Export Compliance question and click Save.
  6. Click Continue.
    The status of the app should change to “Waiting for Upload.”

Creating Distribution certificate

Before an app can be distributed, your team must have a valid distribution certificate linked to a distribution provisioning profile. Only team admins can create or install a distribution certificate. Each team can have only one active distribution certificate. The team admin can either use Xcode to create a distribution certificate or manually request and download one from iOS Provisioning Portal.

A team’s distribution certificate allows a developer to build an app for distribution. If your team wants to use another Mac to create a distribution build, you need to transfer a copy of the distribution certificate as described in, “Safeguarding and Transferring Your Signing and Provisioning Assets” in Tools Workflow Guide for iOS.


Using Xcode to Create a Distribution Certificate

You should use Xcode to create your distribution certificate. Xcode creates, downloads, and installs a development certificate, distribution certificate, and the iOS Team Provisioning Profile for you. Xcode can also restore missing certificates and renew expired certificates.

To create a distribution certificate . . .

  1. In Xcode, open the Devices organizer.

  2. Select Provisioning Profiles in the Library section.

  3. Click the Refresh button at the bottom of the window..

  4. Enter your user name and password and click Log in.

    After you log in to your account, a prompt appears, asking whether Xcode should request your distribution certificate.

  5. Click Submit Request.

  6. If a prompt appears, at the end of the refresh process, asking if you want to export your developer profile, click Export.
Your request for a distribution certificate is automatically approved. The distribution certificate is added to your keychain and appears in Xcode. You can view, download, or revoke the distribution certificate in the iOS Provisioning Portal.


Manually Managing a Distribution Certificate

Although it is easier to use Xcode to create a distribution certificate, you can manually create a distribution certificate. If you do not have access to Xcode, generate a Certificate Signing Request (CSR) with Keychain Access, submit your request, and download the certificate from the iOS Provisioning Portal.

Generating a Certificate Signing Request with Keychain Access

To generate a Certificate Signing Request manually . . .

  1. Open Keychain Access on your Mac (located in Applications/Utilities).

  2. Open Preferences and click Certificates. Make sure both Online Certificate Status Protocol and Certificate Revocation List are set to Off.

  3. Choose Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority.



  4. Enter your user email address and common name. Use the same address and name as you used to register in the iOS Developer Program. No CA Email Address is required.

  5. Select the options “Saved to disk” and “Let me specify key pair information” and click Continue.

  6. Specify a filename and click Save.

  7. For the Key Size choose 2048 bits and for Algorithm choose RSA. Click Continue and the Certificate Assistant creates a CSR and saves the file to your specified location.
Creating a CSR generates a public and private key pair. The private key is stored in the login keychain.

Submitting a Certificate Signing Request for Approval

  1. Navigate to the Certificates area of the iOS Provisioning Portal and click the Distribution tab. Click Request Certificate.

  2. Click Choose File, choose your CSR file, and click Submit.


After the CSR is approved, the certificate is listed under Current Distribution Certificate. If it doesn’t appear automatically, you may need to refresh the page.

Downloading and Installing Distribution Certificates

  1. Navigate to the Certificates area of the iOS Provisioning Portal and click the Distribution tab. Click Download next to the certificate.

  2. In the Finder, double-click the downloaded .cer file to open Keychain Access and install your certificate in your default keychain (usually the login keychain).

Distribution Certificates Must Be Renewed Periodically

A distribution certificate is valid for one year from date of issue. After it expires, you won’t be able sign and install apps on your devices although this will not affect any existing apps in the App Store.

To continue distribution, navigate to the Devices organizer in Xcode. Select the expired profile and click Renew Profile in the red bar at the top. This will renew your expired certificate and add it to the provisioning profile. After you get a new distribution certificate, you can submit new apps or app updates to the App Store.

If you are enrolled in the iOS Developer Enterprise Program, revoking your distribution certificate will make your app fail on any installed devices. Only revoke your certificate if your app or private key have been compromised. You can create a second distribution certificate six months before your existing certificate expires. The overlapping certificate allows you to continue to build and distribute your app once your first certificate expires.