2

I am writing a simple application for iOS in swift that makes an ajax call to a server of mine. Here is the relevant swift code:

class Request : NSObject {
    func send(url: String, f: (NSData)-> ()) {
        var request = NSURLRequest(URL: NSURL(string: url)!)
        var response: NSURLResponse?
        var error: NSErrorPointer = nil
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: error)
        //var reply = NSString(data: data!, encoding: NSUTF8StringEncoding)
        f(data!)
    }
}

class myObj: NSObject {

    let baseURL: String
    var message: String

    init() {        
        self.baseURL = XXXXXXXXXXXXX
        self.message = "No Message"
    }

    func check() -> Bool {

        let locationURL = self.baseURL

        self.message = locationURL
        var request = Request()
        request.send(locationURL, f: {(result: NSData)-> () in
            let jsonData: NSData = result
            var error: NSError?

            let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as! NSDictionary
            self.message = jsonDict["title"] as! String
        })

        return true
    }
}

Here is the server side code that I return dummy JSON with:

<?php
    header('Content-Type: application/json; charset=utf-8');
    if(isset($_GET['location'])) {
        echo json_encode(Array( 'success' => true, 'message' => 'I came from the server.', 'title' => 'tittttt'));
    } else if(isset($_POST['message'])) {
        echo json_encode(Array( 'success' => true, 'message' => 'message received', 'title' => 'ttt'));
    } else {
        echo json_encode(Array( 'success' => false, 'message' => 'invalid params', 'title' => 'title from server'));
    }
?>

When I switch out my URL for a dummy json url (I am using http://jsonplaceholder.typicode.com/posts/1?a=b) it works; when I use my own URL it fails with the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

What am I doing wrong here?

EDIT: Here is the actual JSON response from the server:

{ success: true, message: "I came from the server.", title: "tit-le" }

8
  • On which line does the error occur? Commented Aug 23, 2015 at 6:54
  • On the line: let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as! NSDictionary Commented Aug 23, 2015 at 6:55
  • Your php appears to be returning an array and your swift appears to be forcing it to be a dictionary Commented Aug 23, 2015 at 7:18
  • This seems like your PHP response is malformed in some way, although it is not due your use of PHP's array, as that's how PHP handles dictionaries. Frank, can you post a sample response (with redacted/modified data if needed)? Commented Aug 23, 2015 at 7:28
  • 1
    header("Access-Control-Allow-Origin: *"); <---- Does your PHP have this? Commented Aug 23, 2015 at 9:39

1 Answer 1

2

Have u tried adding header("Access-Control-Allow-Origin: *"); To your PHP

Sign up to request clarification or add additional context in comments.

1 Comment

Wohooo.. OK so just to explain.. This is to allow cross domain connection. Any requests which are not from the same domain are usually blocked. This allows the request to come through.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.