swift - sendAsynchronousRequest was deprecated in iOS 9, How to alter code to fix -
below code getting issue with:
func parsefeedforrequest(request: nsurlrequest, callback: (feed: rssfeed?, error: nserror?) -> void) { nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue()) { (response, data, error) -> void in if ((error) != nil) { callback(feed: nil, error: error) } else { self.callbackclosure = callback let parser : nsxmlparser = nsxmlparser(data: data!) parser.delegate = self parser.shouldresolveexternalentities = false parser.parse() } } }
this deprecated of ios 9, , telling me use datataskwithrequest instead. can me change sendasync datatask, don't know how to.
use nsurlsession
instead below,
for objective-c
nsurlsession *session = [nsurlsession sharedsession]; [[session datataskwithurl:[nsurl urlwithstring:"your url"] completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { // handle response }] resume];
for swift,
var request = nsmutableurlrequest(url: nsurl(string: "your url")!) var session = nsurlsession.sharedsession() request.httpmethod = "post" var params = ["username":"username", "password":"password"] dictionary<string, string> request.httpbody = try? nsjsonserialization.datawithjsonobject(params, options: []) request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept") var task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in print("response: \(response)")}) task.resume()
for asynchronously query, apple docs
like networking apis, nsurlsession api highly asynchronous. returns data in 1 of 2 ways, depending on methods call:
to completion handler block returns data app when transfer finishes or error.
by calling methods on custom delegate data received.
by calling methods on custom delegate when download file complete.
Comments
Post a Comment