ios - Unable to open new window on successful login Post - Swift -
i new swift. trying build app -
i have login screen , once user types in correct username , password, user redirected new window. have managed http post requests after unable open new window on successful login.
@ibaction func signintapped(sender: uibutton) { let parameters = "{\"email\": \""+txtusername.text+"\", \"password\": \""+txtpassword.text+"\"}" ;//as dictionary<string, string> //create url nsurl let url = nsurl(string: "https://myurl") //change url //create session object var session = nsurlsession.sharedsession() let request = nsmutableurlrequest(url: url!) request.httpmethod = "post" //set http method post var err: nserror? request.httpbody = parameters.datausingencoding(nsutf8stringencoding, allowlossyconversion: true);//nsjsonserialization.datawithjsonobject(parameters, options: nil, error: &err) // pass dictionary nsdata object , set request body request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept") //create datatask using session object send data server var task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in println("response: \(response)") let strdata = nsstring(data: data, encoding: nsutf8stringencoding) println(strdata) var err: nserror? var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves, error: &err) as? nsdictionary // did jsonobjectwithdata constructor return error? if so, log error console if(err != nil) { println(err!.localizeddescription) let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) println("error not parse json: '\(jsonstr)'") } else { // jsonobjectwithdata constructor didn't return error. but, should still // check , make sure json has value using optional binding. if let parsejson = json { // okay, parsedjson here, let's value 'success' out of var success = parsejson["success"] as? int println("succes: \(success)") } else { // woa, okay json object nil, went worng. maybe server isn't running? let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) println("error not parse json: \(jsonstr)") } } })
can 1 please give me example. appreciated.
many thanks
this crash log requested @dharmesh kheni
2015-06-20 16:53:44.028 login[29072:191664] *** assertion failure in -[uikeyboardtaskqueue waituntilalltasksarefinished], /sourcecache/uikit_sim/uikit-3347.44/keyboard/uikeyboardtaskqueue.m:374 2015-06-20 16:53:44.033 login[29072:191664] *** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: '-[uikeyboardtaskqueue waituntilalltasksarefinished] may called main thread.' *** first throw call stack: ( 0 corefoundation 0x000000010c932c65 __exceptionpreprocess + 165 1 libobjc.a.dylib 0x000000010e49dbb7 objc_exception_throw + 45 2 corefoundation 0x000000010c932aca +[nsexception raise:format:arguments:] + 106 3 foundation 0x000000010cdcf98f -[nsassertionhandler handlefailureinmethod:object:file:linenumber:description:] + 195 4 uikit 0x000000010d929936 -[uikeyboardtaskqueue waituntilalltasksarefinished] + 151 5 uikit 0x000000010d3ca952 -[uikeyboardimpl setdelegate:force:] + 473 6 uikit 0x000000010d67560d -[uiperipheralhost(uikitinternal) _reloadinputviewsforresponder:] + 1002 7 uikit 0x000000010d67d994 -[uiperipheralhost(uikitinternal) _preserveinputviewswithid:animated:reset:] + 504 8 uikit 0x000000010d30d531 -[uiviewcontroller _presentviewcontroller:modalsourceviewcontroller:presentationcontroller:animationcontroller:interactioncontroller:completion:] + 623 9 uikit 0x000000010d30e7ae -[uiviewcontroller _presentviewcontroller:withanimationcontroller:completion:] + 3079 10 uikit 0x000000010d310701 __62-[uiviewcontroller presentviewcontroller:animated:completion:]_block_invoke + 132 11 uikit 0x000000010d310625 -[uiviewcontroller presentviewcontroller:animated:completion:] + 229 12 login 0x000000010c70d420 _tffc5login7loginvc12signintappedfs0_fcso8uibuttont_u_ftgsqcso6nsdata_gsqcso13nsurlresponse_gsqcso7nserror__t_ + 3344 13 login 0x000000010c70d723 _ttrxfo_ogsqcso6nsdata_ogsqcso13nsurlresponse_ogsqcso7nserror__dt__xfo_itgsqs__gsqs0__gsqs1____it__ + 51 14 login 0x000000010c70abe1 _tpa__ttrxfo_ogsqcso6nsdata_ogsqcso13nsurlresponse_ogsqcso7nserror__dt__xfo_itgsqs__gsqs0__gsqs1____it__ + 81 15 login 0x000000010c70d753 _ttrxfo_itgsqcso6nsdata_gsqcso13nsurlresponse_gsqcso7nserror___it__xfo_ogsqs__ogsqs0__ogsqs1___dt__ + 35 16 login 0x000000010c70d7ba _ttrxfo_ogsqcso6nsdata_ogsqcso13nsurlresponse_ogsqcso7nserror__dt__xfdcb_dgsqs__dgsqs0__dgsqs1___dt__ + 90 17 cfnetwork 0x000000010f6a600b __49-[__nscflocalsessiontask _task_onqueue_didfinish]_block_invoke + 157 18 foundation 0x000000010cdf357f __nsblockoperation_is_calling_out_to_a_block__ + 7 19 foundation 0x000000010cd340b2 -[nsblockoperation main] + 98 20 foundation 0x000000010cd16774 -[__nsoperationinternal _start:] + 645 21 foundation 0x000000010cd16383 __nsoqschedule_f + 184 22 libdispatch.dylib 0x000000010ebc1614 _dispatch_client_callout + 8 23 libdispatch.dylib 0x000000010eba86a7 _dispatch_queue_drain + 2176 24 libdispatch.dylib 0x000000010eba7cc0 _dispatch_queue_invoke + 235 25 libdispatch.dylib 0x000000010ebab3b9 _dispatch_root_queue_drain + 1359 26 libdispatch.dylib 0x000000010ebacb17 _dispatch_worker_thread3 + 111 27 libsystem_pthread.dylib 0x000000010ef2e6cb _pthread_wqthread + 729 28 libsystem_pthread.dylib 0x000000010ef2c4a1 start_wqthread + 13 ) libc++abi.dylib: terminating uncaught exception of type nsexception
you can initiate new view controller way when user log in:
if let parsejson = json { // okay, parsedjson here, let's value 'success' out of var success = parsejson["success"] as? int println("succes: \(success)") dispatch_async(dispatch_get_main_queue()) { let storyboard = uistoryboard(name: "main", bundle: nil) let vc = storyboard.instantiateviewcontrollerwithidentifier("yourviewcontrollerid") as! uiviewcontroller self.presentviewcontroller(vc, animated: true, completion: nil) } }
and don't forget give identifier next view clicking on view controller in storyboard go identity inspector set storyboard id shown in below image:
hope you.
Comments
Post a Comment