Friday, August 25, 2017

Swift: Load JSON data from Web and Parse it

Download This Example Code Here.
 

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()

        //let url = URL(string: "http://api.fixer.io/latest")
        let url = URL(string: "http://arkayapps.com/sellapp/edumcq/service/getchapterbysubject.php?subjectid=42");
        
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil
            {
                print ("ERROR")
            }
            else
            {
                if let content = data
                {
                    do
                    {
                        //Array
                        let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                        
                        print(myJson)
                            for index in 0...myJson.count-1 {
                                 let aObject = myJson[index] as! [String : AnyObject]
                                let chaptername = aObject["chaptername"] as! String
                                let chpid:Int? = Int(aObject["chapterid"] as! String)
                                let subid:Int? = Int(aObject["subjectid"] as! String)
                                
                                var chapter = Chapter(mark: chaptername, mycheptoriD: chpid!,mySubjectid: subid!)
                                print(chapter.chaptername)
                                print(chapter.chapterid)
                                print(chapter.subjectid)

                                
                        }
                        
                        
 
                    }
                    catch
                    {
                        
                    }
                }
            }
        }
        task.resume()
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    struct Chapter {
        var chaptername: String
        var chapterid: Int
        var subjectid: Int
        
        init(mark: String, mycheptoriD: Int, mySubjectid: Int) {
            self.chaptername = mark
            self.chapterid = mycheptoriD
            self.subjectid = mySubjectid
        }
        
    }


}

Wednesday, August 2, 2017

Parsing JSON Array to Java ArrayList

First Create Bean Class that you need to convert on ArrayList. Right now for sample i have just create country Class as below.
 

public class Country {
    private int countryID;
    private String countryName;


    public Country(int countryID, String countryName) {
        this.countryID = countryID;
        this.countryName = countryName;
    }

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

}

Now lets Create Json String for convert Java ArrayList Object. This string also come from JSON API.
    private String countryJson = "{\"category\": [{\n" +
            "            \"countryID\":\"1\",\n" +
            "            \"countryName\": \"India\",\n" +
            "            {\n" +
            "            \"countryID\": \"2\",\n" +
            "            \"countryName\": \"UK\",\n" +
            "            },\n" +
            "            {\n" +
            "            \"countryID\": \"3\",\n" +
            "            \"countryName\": \"US\",\n" +
            "            \n" +
            "            }\n" +
            "            ]\n" +
            "            }";

         Type listType = new TypeToken>() {}.getType();

        JsonParser jsonParser = new JsonParser();
        JsonObject jo = (JsonObject)jsonParser.parse(countryJson);
        JsonArray jsonArr = jo.getAsJsonArray("category");
        Gson googleJson = new Gson();
        countries = googleJson.fromJson(jsonArr.toString(), listType);

        System.out.println("List Elements are  : "+countries.get(0).getCountryName());

That's it.

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.