0

My Code is as below.This code does not work. But the services with https protocol works..Pls help

override func viewDidLoad() {
            super.viewDidLoad()

            let urlPath = "http://xx.com"
            let reposURL: NSURL = NSURL(string: urlPath)!
            let URLrequest: NSURLRequest = NSURLRequest(URL: reposURL)
            let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
            // 2
            do{
            if let JSONData: NSData = try NSURLConnection.sendSynchronousRequest(URLrequest, returningResponse: response){
            //if let JSONData = NSData(contentsOfURL: reposURL!) {
                // 3
                if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
                    // 4
                    if let reposArray = json["items"] as? [NSDictionary] {
                        // 5
                        for item in reposArray {

                        }
                    }
               }
            }
            }catch{}
    }
Darshaka
  • 179
  • 3
  • 12
  • Check this [http://stackoverflow.com/questions/32820320/nsdata-cannot-retrive-image-from-internet-anymore/32820622#32820622](http://stackoverflow.com/questions/32820320/nsdata-cannot-retrive-image-from-internet-anymore/32820622#32820622) – Gandalf Mar 09 '16 at 05:42
  • Not work in the sense the block is not getting called or getting any error? – Johnykutty Mar 09 '16 at 05:44
  • Thank you very much.. it worked.. :) – Darshaka Mar 09 '16 at 05:55

2 Answers2

2

Add App Transport Security Settings to your XCode .plist-file:

App Transport Security Settings   Dictionary   (1 item)

          Allow Arbitrary Loads   BOOLEAN      YES

Official documentation: https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

Also see this source: http://iosdevtips.co/post/121756573323/ios-9-xcode-7-http-connect-server-error

Vitaly Vesyolko
  • 558
  • 5
  • 22
1

Try this :

 func jsonParse()
    {
        let urlPath: String = "http://www.airforce.lk/test.php"
        let url: NSURL = NSURL(string: urlPath)!
        let request1: NSURLRequest = NSURLRequest(URL: url)
        let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
        do
        {
            let dataVal: NSData =   try NSURLConnection.sendSynchronousRequest(request1, returningResponse: response)
            let json: NSArray =  try (NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers) as? NSArray)!
            print("Synchronous\(json)")

            if let json: NSArray = try (NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers) as? NSArray)
            {
                print(json[0])
            }
        }
        catch
        {
        }
    }
Bhagyalaxmi Poojary
  • 1,213
  • 1
  • 12
  • 17