Saturday, July 15, 2017

NSNotificationCenter is Communication tools internal to your app

In App Development some time we need internal communication to pass message to one ViewController to other ViewController. As Example. I am using Google login using Firebase now login task doing AppDelegate Now i need message when login success or fail on ViewController.  Then here we can use NSNotificationCenter.

1. Create Globally define a"Special Notification Key" constant that can be broadcast. on ViewController

   let mySpecialNotificationKey = "com.arkay.specialNotificationKey" 


2. Set Observer on ViewController.
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateNotificationSentLabel), name: NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil)

3. Create Func that can call when Notify ViewController

 func updateNotificationSentLabel(){
        btnSignIn.setTitle("Login Out",for: .normal)
    }]

4. Now Call When need. Right now i am calling from AppDelegate.

        NotificationCenter.default.post(name: Notification.Name(rawValue: mySpecialNotificationKey), object: self)


That's it.

Friday, July 14, 2017

Getting start with CocoaPods iPhone app Using XCode.

As you know without lib it's to difficult to completed project. In Android we use Gradle  as Dependency manager but in Swift and Objective-c it's CocoaPods is Dependency Manager. This tutorial will explain how to getting start with CocoaPods on XCode.

1. Create iPhone new project using XCode.
2. Now Close this Project.
3. Execute Following command.
sudo gem install cocoapods



4.  Open Terminal and goes on your Project location.
cd ~/Desktop/CocoapodsTestApp
 
5.  Now Simple Execute following command.
pod init
 
6. Now check project location it will be generate PodFile.


7. Open that Pod File on Text Editor It will be look like below.

8. Now you can add as my POD at # Sing. Let's try add Firebase Pod There.


9.  Now Save this file. 
10. And Execute following command same as project location.
pod install



11.  its done.. Thanks..
12. Now Open xcode Proejct. Be CAREFUL when open .xcworkspace file not .xcodeproj

Thanks. 









Monday, September 12, 2011

Blackberry Color Label Field

Following code example use for create custom color label filed.


import net.rim.device.api.i18n.ResourceBundleFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

public class ColorLableField extends LabelField{
private int color = 0x000000;
public ColorLableField(String text){
super(text);
}
public ColorLableField(Object text, int offset, int length, long style){
super(text, offset, length, style);
}
public ColorLableField(Object text, long style){
super(text, style);
}
public ColorLableField(ResourceBundleFamily rb, int key){
super(rb, key);
}

//Set the color of the text in this field
public void setColor(int newColor){
color = newColor;
}

//Set the foreground color and then call paint method of the parent class
public void paint(Graphics graphics){
graphics.setColor(color);
super.paint(graphics);
}
}

public final class MyScreen extends MainScreen{

public MyScreen()
{
setTitle("MyTitle");
ColorLableField lbl = new ColorLableField("this is text");
lbl.setColor(Color.RED);
this.add(lbl);
}
}

Monday, August 1, 2011

Call RSS Feed In android

Here Example of Call RSS Feed in android application or Java.
public void writeNews() {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL("http://www.itpro.co.uk/blogs/feed/"); // your feed url
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
System.out.println("Title: " + getElementValue(element, "title"));
System.out.println("Link: " + getElementValue(element, "link"));
System.out.println("Publish Date: " + getElementValue(element, "pubDate"));
System.out.println("Author: " + getElementValue(element, "dc:creator"));
System.out.println("Description: " + getElementValue(element, "description"));
System.out.println();
}//for
}//try
catch (Exception ex) {
ex.printStackTrace();
}
}

private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
} catch (Exception ex) {
}
return "";
} //private String getCharacterDataFromElement

protected float getFloat(String value) {
if (value != null && !value.equals("")) {
return Float.parseFloat(value);
} else {
return 0;
}
}

protected String getElementValue(Element parent, String label) {
return getCharacterDataFromElement((Element) parent.getElementsByTagName(label).item(0));
}

Friday, July 1, 2011

One difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same Object instance. for example.


public class Main {

   public static void main(String[] args) {
     RunnableThread r = new RunnableThread();
     Thread t1 = new Thread(r, "Thread A");
     Thread t2 = new Thread(r, "Thread B");
     ExtendsThread t3 = new ExtendsThread("Thread C");
     ExtendsThread t4 = new ExtendsThread("Thread D");
     t1.start();
     t2.start();
     t3.start();
     t4.start();

   }
 }



class RunnableThread implements Runnable {

private int counter;

  public void run() {
   try {
     for (int i = 0; i != 2; i++) {
   System.out.println(Thread.currentThread().getName()+ ":"counter++);
      Thread.sleep(1000);
      }
    } catch (InterruptedException ine) {
    System.err.println(ine);
   }
 }
}

class ExtendsThread extends Thread {
  private int counter;
  ExtendsThread(String name) {
  super(name);
 }

 @Override
 public void run() {
  try {
   for (int i = 0; i != 2; i++) {
   System.out.println(Thread.currentThread().getName() + ": "+ counter++);
   Thread.sleep(1000);
   }
  } catch (InterruptedException ine) {
   System.err.println(ine);
 }
 }
}

Thursday, June 30, 2011

9 ways to improve your programming skills.

1. Learn a new programming language.
2. Read a good, challenging programming book.
Like: The Art of Computer Programming
Structure and interpretation of computer Programs.
A Discipline of Programming
3. Join an Open Source Project.
You Can join: GitHub, SourceForge,gitorius, bitBucket, Ohloh
4. Solve Programming puzzles.
5. Program Start writing a program. scratch. design all of the architechture and implement.
6. Read And Study Code.
7. Hang Out as programming sites and read blogs.
8. Write about coding.
9. learn low level programming like C, Assembly language.

JUnit Test Case with Example.

Here is Sample example on Junit Text Case.

Let's Test Some Code

public class Main {

public static void main(String[] args) {
}
static public int add(int a, int b) {
return a + b;
}

}


now i want to test about class using Junit Test case. fot test we require to import "junit.framework.*" and extend TestCase

import junit.framework.*;
public class TestMaths extends TestCase{

public void testMaths() {
int num1 = 2;
int num2 = 3;
int total = 5;
int sum = 0;
sum = Main.add(num1, num2);
assertEquals(sum, total);
}
}


Here i am using Netbeans. now run TestMaths file you can see out put on console.

Summary:
That's it! This example may be help full to getting start JUnit.

enjoy!