Friday, March 30, 2012

Building xCode project and generating IPA file using Ant

Following is the procedure to generate IPA file.

Under “project settings” change “derived data location” to “project-relative”, and make it "DerivedData/dist/". If you want any other location, change the same in the code. This will be the location where ".app" file and all other project info is stored.

Set the values to the fields in CAPITAL LETTERS, and save the file as build.xml

-----------------------------------------------
<project default="buildAndPackage" basedir="." name="APP-FOLDER">
<property name="local.projectPath" location="WHERE THE PROJECT IS LOCATED" />
<property name="local.ipaPath" location="WHERE TO SAVE THE IPA FILE" />

<property name="app.id" value="APP-ID" />
<property name="scheme" value="APP-SCHEME(MOSTLY APP-ID)" />
<property name="version" value="VERSION-NO" />
<property name="distributorName" value="NAME USED IN IOS PROVISIONING PORTAL" />

<property name="ProvisioningProfile" location="WHERE YOU KEPT THE FILE" />
<property name="configuration" value="Release/debug/AdHocDistribution/AppStore SELECT THE APPROPRIATE" />

<target name="buildAndPackage">
<exec executable="/Developer/usr/bin/xcodebuild">
<arg value="-project" />
<arg value="${local.projectPath}/${app.id}.xcodeproj" />
<arg value="-scheme" />
<arg value="${scheme}" />
<arg value="-configuration" />
<arg value="${configuration}" />
<arg value="clean" />
<arg value="build" />
</exec>

<exec executable="xcrun">
<arg value="-sdk" />
<arg value="iphoneos" />
<arg value="PackageApplication" />
<arg value="-v" />
<arg value="${local.projectPath}/DerivedData/dist/Products/${configuration}-iphoneos/${app.id}.app" />
<arg value="-o" />
<arg value="${local.ipaPath}/${app.id}-${version}.ipa" />
<arg value="--sign" />
<arg value="iPhone Distribution: ${distributorName}" />
<arg value="--embed" />
<arg value="${ProvisioningProfile}" />
</exec>
</target>
</project>

--------------------------------

Then run it from command line by typing "ant"

Saturday, March 24, 2012

Integrating AdMob Ads in a PhoneGap Project (iOS)


  1. Open the phonegap project to which AdMob is to be added.

  2. Import GoogleAdMobAdsSDK which can be found here.

  3. Add MessageUI.framework to the project. To do this " click on project name , go to summary tab, scroll down to the framework section and click +. Then select MessageUI.framework".

  4. In AppDelegate.h import "GADBannerView.h" and add "GADBannerView *bannerView_;" , so that it looks like@interface AppDelegate : PhoneGapDelegate {

    NSString* invokeString;

    GADBannerView *bannerView_;

    }

  5.  In AppDelegate.m –  #define MY_BANNER_UNIT_ID @”Your AdMob Publisher ID”

  6. Add the following code at the beginning of webViewDidFinishLoad:    bannerView_ = [[GADBannerView alloc]init];

    [bannerView_ setDelegate:self];

    [bannerView_ setFrame:CGRectMake(0, 45, 320, 95)];//Position of Ad banner

    bannerView_.adUnitID = MY_BANNER_UNIT_ID;

    bannerView_.rootViewController = self.viewController;

    [self.viewController.view addSubview:bannerView_];

    [bannerView_ loadRequest:[GADRequest request]];

You can find the sample code here

Friday, March 23, 2012

Auto-mount exFAT devices in Ubuntu

exFAT is natively supported by OSX and Windows (even XP!) the only thing was to make it work with Ubuntu. So here we go:

Execute these commands one by one
sudo add-apt-repository ppa:relan/exfat
sudo apt-get update
sudo apt-get install fuse-exfat

Now we are able to manualy mount exFAT-formatted drives. To automate it we have to install couple more things:
sudo apt-get install build-essential
sudo apt-get install ncurses-dev
sudo apt-get install util-linux

Everything should be working nicely now

Troubleshooting:
If repository cannot be added:
sudo -E add-apt-repository ppa:relan/exfat
If install failed, because of fuse-utils:
sudo apt-get install exfat-fuse exfat-utils

Sunday, March 4, 2012

Anagram search from a file

// All the words are in the file Dictionary.txt

import java.io.*;
import java.util.*;
public class Anag
{
    static String[] words;
    public static void main(String args[]) throws IOException
    {
        Hashtable<String, List> ht;
        try {
            FileInputStream fstream = new FileInputStream("Dictionary.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            List<String> lines = new ArrayList<String>();
            String ele;
            while ((ele = br.readLine()) != null)
                lines.add(ele);
            words = lines.toArray(new String[lines.size()]);
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
        ht = getAnagrams();
        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter key number:");
        String wd = br1.readLine();
        char[] chars = wd.toCharArray();
        Arrays.sort(chars);
        String key = String.valueOf(chars);
        System.out.println(ht.get(key));
    }

    public static Hashtable<String, List> getAnagrams() {
        Hashtable<String, List> ht = new Hashtable<String, List>();
        Set<String> hasAnagram = new HashSet<String>();
        for (String wd : words) {
            if (wd.length() <= 1)
                continue;
            // sort the characters in this word
            char[] chars = wd.toCharArray();
            Arrays.sort(chars);
            String anaKey = String.valueOf(chars);
            // update the list of anagrams
            List<String> anagramList = null;
            if (ht.containsKey(anaKey)) {
                anagramList = ht.get(anaKey);
                hasAnagram.add(anaKey);
            }
            if (anagramList == null) {
                anagramList = new ArrayList<String>();
                anagramList.add(wd);
                ht.put(anaKey, anagramList);
            }
            if (!anagramList.contains(wd)) {
                anagramList.add(wd);
            }
        }
        return ht;
    }
}

ChildBrowser Android

First you need to get ready with the phonegap running properly.It's here. First download the source from here

Now follow these procedure:
 1. To install the plugin, move www/childbrowser.js to your project's www folder and include a reference to it in your html file after phonegap<version>.js.

<script type="text/javascript" charset="utf-8" src="phonegap<version>.js"></script>
<script type="text/javascript" charset="utf-8" src="childbrowser.js"></script>
2. Copy the image files folder www/childbrowser to your project's www folder.Here you can edit the code or add any other files, but keep the folder named "ChildBrowser" as it is. Because, it is being referenced from others locations.
3. Create a directory within your project called "src/com/phonegap/plugins/childBrowser" and copy "src/com/phonegap/plugins/childBrowser/ChildBrowser.java" into it.

3.1. If you would like to add these folder(childBrowser folder with file ChildBrowser.java) into your existing 'src' folder, then there are some minor changes. Open ChildBrowser.java and change the package name to the relevant one.
4. In your res/xml/plugins.xml file add the following line:

<plugin name="ChildBrowser" value="com.phonegap.plugins.childBrowser.ChildBrowser"/>

4.1. If you followed the step-3.1, ie., if you added the folder elsewhere, change the "value" tag.

5. Customize the links as per your need and build the project.


-------------------------------------------------------------------------------------------------------------------------------------------------

index.html
<html>

  <head>

    <meta name="viewport" content="width=320; user-scalable=no" />

    <meta http-equiv="Content-type" content="text/html; charset=utf-8">

    <title>Child Browser Example</title>

    <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">

    <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>

    <script type="text/javascript" charset="utf-8" src="childbrowser.js"></script>

    <script type="text/javascript" charset="utf-8">

    function init(){

        document.addEventListener("deviceready", onDeviceReady, false);

    }


     function onDeviceReady() {

        console.log("PhoneGap is ready");

    }


    function locationChanged(newurl) {

        console.log("The JS got this url = " + newurl);

    }

    function closed() {

        console.log("The JS got a close event");

    }

    function showToolbar(locbar) {

        window.plugins.childBrowser.onLocationChange = locationChanged;

        window.plugins.childBrowser.onClose = closed;
         window.plugins.childBrowser.showWebPage("http://thechaithanya.wordpress.com",{

            showLocationBar: locbar

        });

    }

    </script>

  </head>

  <body onload="init()">

    <a href="#" onclick="showToolbar(true);">Page with Toolbar</a>

    <a href="#" onclick="showToolbar(false);">Page without Toolbar</a>

  </body>

</html>

-------------------------------------------------------------------------------------------------------------------------------------------------

Example: If your source file is at src/com/chaithanya/sample,then

if "ChildBrowser.java" is at "src/com/chaithanya/sample/childBrowser", then in res/xml/plugins.xml add
<plugin name="ChildBrowser" value="com.chaithanya.sample.childBrowser.ChildBrowser"/>
else if "ChildBrowser.java" is at "src/com/phonegap/plugins/childBrowser", then in res/xml/plugins.xml add
<plugin name="ChildBrowser" value="com.phonegap.plugins.childBrowser.ChildBrowser"/>

Saturday, March 3, 2012

ChildBrowser iOS xCode

Today I tried to simplify the ChildBrowser execution in xCode, which I follwed earlier. To my surprise, I don't need to do anything rather than simply ignoring some steps. Here is the procedure:
  1. Download the ChildBrowser plugins here.
  2. Start xCode and create a phonegap-based application.
  3. In Supporting Files > PhoneGap.plist, add under Plugins (click the down arrow to the left of Plugins, then click on the + symbol to open a new row):
    Key: ChildBrowserCommand

    Type: String (default)

    Value: ChildBrowserCommand 
  4. You need to associate your external URLs on this page, too. In Supporting Files > PhoneGap.plist, add them under
    ExternalHosts, like:
    Key: Item 0

    Type: String (default)

    Value:  * or whatever URL you are using in the code(* is enough)
  5. Right-click on the Plugins folder in Xcode and select to “Add Files to…” the folder.
  6. Navigate to the phonegap-plugins > iPhone > Childbrowser folder, and select the ChildBrowser folder. Check “Copy items into. . .” and “Create groups for. . .” A ChildBrowser folder is added to the Plugins folder.

Sample Code:

<html>

<head>

<script type=”text/javascript” charset="utf-8" src=”phonegap.<version>.js”></script>

<script type=”text/javascript” charset="utf-8" src=”ChildBrowser.js”></script>
            <script type=”text/javascript” charset="utf-8">

                        var cb;

                        function onDeviceReady()    {
                                 cb = ChildBrowser.install();
                        }
                        function showChild()  {
                                 cb.showWebPage(“http://google.com”);
                        }

</script>

</head>
<body onload=”onDeviceReady()”>

<a href="#" onclick="showChild()">Click here</a>

</body>
</html>

Josephus Problem using Queue

//Josephus.java
//Out of  n members, every m'th person will be eliminated
import java.io.*;

//Stack---------------
class Node<T> {
    T value;
    Node<T> link;
}

class Stack<T> {
    Node<T> top;
    public Stack() {
        top = null;
    }
    public void push(T item) {
        Node<T> n = new Node<T>();
        n.value = item;
        n.link = top;
        top = n;
    }
    public T pop() {
        T item;
        item = top.value;
        Node<T> n = top;
        n = null;
        top = top.link;
        return item;
    }
    public void display() {
        Node<T> n = top;
        System.out.print("(top)");
        while (n != null) {
            System.out.print(" ->" + n.value);
            n = n.link;
        }
        System.out.println();
    }
}

// Queue---------------------------
class Queue<T> {
    Node<T> front, rear;
    Stack<T> s1 = new Stack<T>();
    Stack<T> s2 = new Stack<T>();
    public Queue() {
    }
    public void add(T item) {
        while (s2.top != null)
            s1.push(s2.pop());
        s1.push(item);
        rear = s1.top;
        while (s1.top != null)
            s2.push(s1.pop());
        front = s2.top;
    }
    public T remove() {
        T item = s2.pop();
        front = s2.top;
        return item;
    }
    public void display() {
        Node<T> n = s2.top;
        System.out.print("(front)");
        while (n != null) {
            System.out.print(" <-" + n.value);
            n = n.link;
        }
        System.out.println(" <-(rear)");
    }
}

// Josephus Problem-------------------
public class Josephus {
    public static void main(String[] args) throws IOException {
        Queue<Integer> q = new Queue<Integer>();
        Queue<Integer> q1 = new Queue<Integer>();
        int n, m, i;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter no. of members (n) : ");
        n = Integer.parseInt(br.readLine());
        System.out.println("Enter the elimination number (m) : ");
        m = Integer.parseInt(br.readLine());
        for (i = 1; i <= n; i++)
            q.add(i);
        Node<Integer> node = q.front;
        int l, k = 0;
        while (k != n - 1) {
            for (i = 1; i < m; i++)
                q.add(q.remove());
            l = q.remove();
            q1.add(l);
            k++;
        }
        System.out.println("Order of elimination is : ");
        while (q1.front != null)
            System.out.print(q1.remove() + ",");
        System.out.println("nWinner is : " + q.remove());
    }
}

Queue using Stack

import java.io.*;
//Node---------------
class Node<T> {
    T value;
    Node<T> link;
}

// Stack---------------
class Stack<T> {
    Node<T> top;
    public Stack() {
        top = null;
    }
    public void push(T item) {
        Node<T> n = new Node<T>();
        n.value = item;
        n.link = top;
        top = n;
    }
    public T pop() {
        T item;
        item = top.value;
        Node<T> n = top;
        n = null;
        top = top.link;
        return item;
    }
    public void display() {
        Node<T> n = top;
        System.out.print("(top)");
        while (n != null) {
            System.out.print(" ->" + n.value);
            n = n.link;
        }
        System.out.println();
    }
}

// Queue---------------------------
class Queue<T> {
    Node<T> front, rear;
    Stack<T> s1 = new Stack<T>();
    Stack<T> s2 = new Stack<T>();
    public Queue() {
    }
    public void add(T item) {
        while (s2.top != null)
            s1.push(s2.pop());
        s1.push(item);
        rear = s1.top;
        while (s1.top != null)
            s2.push(s1.pop());
        front = s2.top;
    }
    public T remove() {
        T item = s2.pop();
        front = s2.top;
        return item;
    }
    public void display() {
        Node<T> n = s2.top;
        System.out.print("(front)");
        while (n != null) {
            System.out.print(" <-" + n.value);
            n = n.link;
        }
        System.out.println(" <-(rear)");
    }
}

Generic Stack


import java.io.*;

class Node<T> {
    T value;
    Node<T> link;
}

class Stack<T> {
    Node<T> top;

    public Stack() {
        top = null;
    }

    public void push(T item) {
        Node<T> n = new Node<T>();
        n.value = item;
        n.link = top;
        top = n;
    }

    public T pop() {
        T item;
        item = top.value;
        Node<T> n = top;
        n = null;
        top = top.link;
        return item;
    }

    public void display() {
        Node<T> n = top;
        System.out.print("(top)");
        while (n != null) {
            System.out.print(" ->" + n.value);
            n = n.link;
        }
        System.out.println();
    }
}