json - IOS app crash - but not in simulator - hardcoding URL works -


after successful build through xcode, app runs in simulator , iphone - when distribute testing, crashes when test users try preform search.

if hardcode url elements - works in both simulation mode , functions in users test trials.

i have crashlytics operating , says crash result of line of code

let allcontactsdata = try data(contentsof: urlresults!) 

i check in previous vc fields contain value - , "print" confirms same

company : accountant suburb  : southport state   : qld getting results from: http://www.myawsmurl.com.au/api/get_details.php?no=accountant&state=qld&suburb=southport 

var's set @ top of class via following:

var tosearchfor: string = "" var tosearchforsuburb: string = "" var tosearchforstate: string = "" 

and func causes issue:

func getresults() {     tosearchfor = searchingfor.trimmingcharacters(in: .whitespacesandnewlines)     tosearchforsuburb = searchingsuburb.trimmingcharacters(in: .whitespacesandnewlines)     tosearchforstate = searchingstate.trimmingcharacters(in: .whitespacesandnewlines)      print("company : \(tosearchfor)")     print("suburb  : \(tosearchforsuburb)")     print("state   : \(tosearchforstate)")      //tosearchfor = "accountant"     //tosearchforsuburb = "southport"     //tosearchforstate = "qld"      //print("company : \(tosearchfor)")     //print("suburb  : \(tosearchforsuburb)")     //print("state   : \(tosearchforstate)")      let searchurl = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + tosearchfor + "&state=" + tosearchforstate + "&suburb=" + tosearchforsuburb)     let urlresults = url(string:searchurl)     print("getting results from: \(searchurl)")      {         let allcontactsdata = try data(contentsof: urlresults!)         let allcontacts = try jsonserialization.jsonobject(with: allcontactsdata, options: jsonserialization.readingoptions.allowfragments) as! [string : nsarray]          etc etc     }     catch {      }      tableview.reloaddata()  } 

as mentioned before, if uncomment hardcoded vars, runs excepted without issues.

any appreciated still cant figure out why runs in simulation mode without issues fails on live testing though fields have value.

edit: (crash data)

#0. crashed: com.apple.main-thread 0  thisawsmapp                    0x1000752e4 viewcontroller.getresults() -> () (viewcontroller.swift:158) 1  thisawsmapp                    0x100076180 specialized viewcontroller.viewdidappear(bool) -> () (viewcontroller.swift) 2  thisawsmapp                    0x100071e3c @objc viewcontroller.viewdidappear(bool) -> () (viewcontroller.swift) 3  uikit                          0x18cb0ddb0 -[uiviewcontroller _setviewappearstate:isanimating:] + 856 4  uikit                          0x18cb0e31c -[uiviewcontroller _endappearancetransition:] + 228 5  uikit                          0x18cbc4d64 -[uinavigationcontroller navigationtransitionview:didendtransition:fromview:toview:] + 1224 6  uikit                          0x18cc93c5c __49-[uinavigationcontroller _startcustomtransition:]_block_invoke + 232 7  uikit                          0x18cc1aa1c -[_uiviewcontrollertransitioncontext completetransition:] + 116 8  uikit                          0x18cd68fac __53-[_uinavigationparallaxtransition animatetransition:]_block_invoke.99 + 724 9  uikit                          0x18cb2e9d0 -[uiviewanimationblockdelegate _didendblockanimation:finished:context:] + 492 10 uikit                          0x18cb2e4f8 -[uiviewanimationstate senddelegateanimationdidstop:finished:] + 312 11 uikit                          0x18cb2e314 -[uiviewanimationstate animationdidstop:finished:] + 160 12 quartzcore                     0x189cdf0d4 ca::layer::run_animation_callbacks(void*) + 260 13 libdispatch.dylib              0x18587e9a0 _dispatch_client_callout + 16 14 libdispatch.dylib              0x1858835e8 _dispatch_main_queue_callback_4cf + 996 15 corefoundation                 0x1869750c0 __cfrunloop_is_servicing_the_main_dispatch_queue__ + 12 16 corefoundation                 0x186972cdc __cfrunlooprun + 1572 17 corefoundation                 0x1868a2d94 cfrunlooprunspecific + 424 18 graphicsservices               0x18830c074 gseventrunmodal + 100 19 uikit                          0x18cb5b130 uiapplicationmain + 208 20 thisawsmapp                    0x1000646d8 main (appdelegate.swift:16) 21 libdyld.dylib                  0x1858b159c start + 4 

  1. url(string:) may return nil if string not correctly formatted url. not safe force unwrap urlresults!, if constructing user input.

it safer this:

if let url = urlresults {     let allcontactsdata = try data(contentsof:url)      // etc } else {     // log error debugging } 
  1. you should make sure strings url encoded

string.trimmingcharacters removes characters beginning , end of string. input variables may contain embedded spaces, or other illegal characters url string.

translate input strings this:

tosearchfor = searchingfor.addingpercentencoding(withallowedcharacters: .urlqueryallowed) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -