swift


session.dataTask not working Swift


I have a method which retrieves data from an API request:
let session = URLSession.shared
let url = URL(string: "http://eventregistry.org/json/article?conceptUri=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAfghanistan&lang=eng&action=getArticles&articlesSortBy=date&resultType=articles")!
let task = session.dataTask(with: url) { (data, _, _) -> Void in
if let data = data {
print(JSON(data: data))
completion()
}
}
task.resume()
The link works perfectly when accessing through a browser, however when debugging line by line in XCode, none of the code within the let task = session.dataTask is executed and so the print is not printed within the log.
I am running through emulator and my laptop has internet, im just not sure why this is not working.
Does anyone understand why?
Try writing it this way. and also try to avoid the "Void"
First you need to make the urlRequest.
Example:
let urlRequest = URLRequest(url: URL(string: "Your url/API links here")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in {
//Your codes here
}
task.resume()
}
Hope it helps. If you need any further explanation, I will post a sample answer to retrieve the Registry Articles.

Related Links

enable NSButton if NSTextfield is not empty
Text retrieval from Firebase is not displaying in app
How to write curl command in Swift with Alamofire?
xmpp_messenger_ios Swift MUC swift
Set Cookies to HTTP POST Requests from Swift NSURLSession
Automatically set focus to first cell of UITableView when view loads
Using SCNNode.childNodes(passingTest: ) to search for nodes
Positioning miltiple views programmatically in swift
Nodes position within scene size
Restrict extensions on a type in Swift?
Realm, Swift and App Sandbox not working?
Delete rows and sections with commitEditingStyle
Array / memory operations in Swift
swift firebase retrieving data error [closed]
Xcode storyboard interface bind items to Popup Button [closed]
Quickest way to check multiple bools and changing image depending on that bool

Categories

HOME
json
sql-server
cluster-computing
vb6
system-verilog
performancecounter
webrtc
deep-linking
hyperledger-fabric
defragmentation
symfony-forms
x-frame-options
ida
titan
plupload
backpropagation
uiautomator
iup
header-files
bcrypt
iis-10
non-deterministic
croppic
hanami
emv
fetch-api
cgal
helper
partial-application
ioc-container
construct-2
m2e
freebase
spreedly
reactivemongo
service-locator
pox
broadleaf-commerce
html-agility-pack
jdbi
resuming-training
worker
rdw
nstouchbar
gauss
opencpu
vst
try-finally
listadapter
spring-repositories
emokit
twgl.js
bluesnap
handlebars.java
zuora
kendo-menu
response-headers
prezto
manjaro
rx-groovy
lines-of-code
adobe-indesign
jython-2.7
c++-actor-framework
cloudpebble
cocoascript
gyroscope-framework
em
dto
system.web
wss
alertifyjs
healthvault
wic
logentries
facebook-sdk-3.1
.net-cf-3.5
dig
manage.py
dwolla
database-permissions
zend-framework-modules
driving-directions
nssavepanel
mysql-error-1045
surf
dashcode
sendfile
svn-hooks
carbide
yajl
boost-date-time
pci-bus
web-analytics-tools

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App