0

Well , I try to post data to my server as soon as the app launches with the help of NSURLConnection . Here's my code in swift

let requests : NSMutableURLRequest = NSMutableURLRequest(URL: url)
    let useragent : String
    if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
        useragent = "iPhone"
    }
    else {
        useragent = "iPad"
    }

    requests.setValue(useragent, forHTTPHeaderField: "User-Agent")
    requests.HTTPMethod = "POST"
    requests.setValue("application/json", forHTTPHeaderField: "Content-type")
    let jsonstring : String = String(data: urlData, encoding: NSUTF8StringEncoding)!
    NSLog("Data being posted to the server")
    NSLog("Post Data  : Data being posted to server")
    requests.timeoutInterval = 60.0
    requests.HTTPBody = jsonstring.dataUsingEncoding(NSUTF8StringEncoding)
    requests.setValue("\(jsonstring.characters.count)", forHTTPHeaderField: "Content-Length")
    let conn : NSURLConnection = NSURLConnection(request: requests, delegate: self)!
    conn.isProxy()

I tried printing things in the device logs and the url and the data to be posted (dictionary named json) are all perfect . The logs print these lines successfully as well

NSLog("Data being posted to the server")
    NSLog("Post Data  : Data being posted to server")

But after this , the app crashes and I could see "Connection invalid " in the device logs . Can anyone help in letting me know if I'm wrong at any point here. Thank you in advance.

AnxiousMan
  • 578
  • 2
  • 8
  • 25
  • Use "Postman" App to check the error – Payal Maniyar Apr 01 '16 at 06:44
  • Where in your code does the application crash? What type of crash are you seeing? Are any messages are printed to the console at the time the crash occurs. – Scott Thompson Apr 01 '16 at 06:45
  • I'm not sure of exactly where the problem is, as the code seems alright, but I'd suggest using `NSURLSession` instead, because `NSURLConnection` is deprecated in iOS 9 and above. Also replace `NSLog(string)` with `print(string)` because `print` is the method you're meant to use in swift for debugging. – kabiroberai Apr 01 '16 at 06:57
  • These are the comments in log – AnxiousMan Apr 01 '16 at 07:09
  • Apr 1 12:05:01 iPad com.apple.xpc.launchd[1] (UIKitApplication:com.manageengine.mdm.ios[0x7a62][2748]) : Service exited due to signal: Trace/BPT trap: 5 Apr 1 12:05:01 iPad SpringBoard[1643] : BSXPCMessage received error for message: Connection invalid Apr 1 12:05:01 iPad SpringBoard[1643] : HW kbd: Failed to set (null) as keyboard focus Apr 1 12:05:01 iPad SpringBoard[1643] : BSXPCMessage received error for message: Connection invalid – AnxiousMan Apr 01 '16 at 07:09
  • @kabiroberai , Is it necessary for me to use start() if i haven't used start immediately : true while initialising? – AnxiousMan Apr 01 '16 at 07:10
  • I have checked in postman app and it works fine there .. Should i include conn.start() after initialising connection @PayalManiyar? – AnxiousMan Apr 01 '16 at 07:12
  • @ManeeshSharma as per the `NSURLConnection` class reference, "Calling this method is necessary only if you create a connection with the `initWithRequest:delegate:startImmediately:` method and provide NO for the startImmediately parameter." So no, I don't think it is necessary for you to call `start()`. Have you tried the other option I suggested? – kabiroberai Apr 01 '16 at 07:13
  • @kabiroberai , NSURLSession ? If i do that , I have to change a lot of methods , So may be i'll do it . but as of now , I want this data posted which is working fine when i check using postman . – AnxiousMan Apr 01 '16 at 07:15
  • @ManeeshSharma I think yes you should use start(). – Payal Maniyar Apr 01 '16 at 07:16
  • @ManeeshSharma alright. Could you please update your code with the function body of `isProxy`? – kabiroberai Apr 01 '16 at 07:16
  • @kabiroberai , I know the methods are deprecated , but it should be working alright as the deployment target for my app is ios 8.0 and above – AnxiousMan Apr 01 '16 at 07:17
  • did not get u @kabiroberai , isProxy What ? – AnxiousMan Apr 01 '16 at 07:17
  • @ManeeshSharma `isProxy` doesn't seem to be a built-in method of `NSURLConnection`. Have you created it yourself? If so, could you please update your question with what `isProxy` does? – kabiroberai Apr 01 '16 at 07:18
  • @PayalManiyar , Do you see any other possibility for this connection not getting established ? – AnxiousMan Apr 01 '16 at 07:19
  • @ManeeshSharma I think I may know the error. Could you tell me whether the url uses the protocol `http` or `https`? – kabiroberai Apr 01 '16 at 07:20
  • @kabiroberai , it is there built in , it returns a bool value. – AnxiousMan Apr 01 '16 at 07:20
  • @kabiroberai , its there in this protocol ....public protocol NSObjectProtocol – AnxiousMan Apr 01 '16 at 07:21
  • @ManeeshSharma Check whether delegate method is called or not. – Payal Maniyar Apr 01 '16 at 07:21
  • @ManeeshSharma no I meant does the url have an "http://" or "https://"? – kabiroberai Apr 01 '16 at 07:22
  • What do u mean by Delagate method is called or not @PayalManiyar – AnxiousMan Apr 01 '16 at 07:23
  • @kabiroberai https – AnxiousMan Apr 01 '16 at 07:23
  • @ManeeshSharma huh... If it was `http`, then the error may have been caused by `App Transport Security`, but since it's `https`, I'm not too sure what the issue is – kabiroberai Apr 01 '16 at 07:24
  • @kabiroberai , exactly ... I'm all over the place because of this ! – AnxiousMan Apr 01 '16 at 07:25
  • @ManeeshSharma NSURLConnectionDataDelegate method. https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/ – Payal Maniyar Apr 01 '16 at 07:25
  • i have used didReceiveData , But that would be called only when there is a connection established .. I haven't used didFinishLoading. @PayalManiyar – AnxiousMan Apr 01 '16 at 07:28
  • @ManeeshSharma Use below method to make connection – Payal Maniyar Apr 01 '16 at 07:30
  • NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; – Payal Maniyar Apr 01 '16 at 07:30
  • @ManeeshSharma are you by any chance using the `CIContext` class? – kabiroberai Apr 01 '16 at 07:33
  • I'm working with swift , not with objective-c @PayalManiyar – AnxiousMan Apr 01 '16 at 07:34
  • @PayalManiyar's code in Swift would be `NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)` – kabiroberai Apr 01 '16 at 07:35
  • And , i'm not using CIContext – AnxiousMan Apr 01 '16 at 07:35
  • Should I really do that ? Because , response and error havent been declared yet , and moreover , the same code in objective-c is working fine – AnxiousMan Apr 01 '16 at 07:38
  • NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:requests delegate:self]; [conn isProxy]; – AnxiousMan Apr 01 '16 at 07:38
  • @ManeeshSharma http://stackoverflow.com/questions/36350581/initwithrequestdelegate-is-deprecated-first-deprecated-in-ios-9-0-use-nsu – Payal Maniyar Apr 01 '16 at 07:40
  • @PayalManiyar , The person has clearly mentioned that he's working with ios 9 ., so it's deprecated whereas I have told am working with ios 8 as deployment target. – AnxiousMan Apr 01 '16 at 07:42

0 Answers0