Call web services by alamofire respectively - ios

I'm creating an application that should call some web services in the begging of the app.
I've wrote a WebServiceManager that contains methods for calling web services and return result in completionHandler, here is one method of WebServiceManager:
extension WebServiceManager {
func checkVersion(completionHandler: #escaping (CheckVersionResponse?, Error?) -> ()) {
sessionManager?.adapter = BaseClientInterceptor()
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
let buildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
sessionManager?.request(Config.VERSION_URL + "/\(appVersion)/\(buildNumber)" + Config.STATUS, method: .get).responseData { response in
print(response.response?.statusCode)
switch response.result {
case .success(let value):
print("----WS: checkVersion Success-----")
let value = JSON(value)
print(value)
let response = CheckVersionResponse(responseCode: response.response?.statusCode ?? -1)
if (response.getResponseCode() == 200) {
response.setUrl(value["url"].string ?? "")
response.setNow(value["now"].intValue)
response.setStatus(value["status"].stringValue)
response.setMessage(value["message"].stringValue)
}
completionHandler(response, nil)
case .failure(let error):
print(error)
print("----WS: checkVersion Failed-----")
completionHandler(nil, error)
}
}
}
}
and I use some methods of this manager in SplashScreenViewController:
class SplashViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
WebServiceManager.shared.checkVersion() { response, error in
//...
}
WebServiceManager.shared.getCurrentDate() { response, error in
//...
}
WebServiceManager.shared.getUpdatedKeyValues() { response, error in
//...
}
if !UserDefaults.standard.bool(forKey: "hasRegistered") {
self.performSegue(withIdentifier: "ToRegistration", sender: self)
} else {
self.performSegue(withIdentifier: "ToMain", sender: self)
}
}
}
I expect that this method runs line by line but it goes through to if at first.
How can I call these web services in order then make a decision based on UserDefaults values ?

sessionManager.request methods are asynchronous, so they will not execute synchronously (line by line) on viewDidAppear (by the way you are missing super.viewDidAppear())
If you want some code to execute after those 3 services you can do something like this, not sure if its the best solution tho:
class SplashViewController: UIViewController {
private var allDone = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
WebServiceManager.shared.checkVersion() { response, error in
//...
checkUserDefault()
}
WebServiceManager.shared.getCurrentDate() { response, error in
//...
checkUserDefault()
}
WebServiceManager.shared.getUpdatedKeyValues() { response, error in
//...
checkUserDefault()
}
}
private func checkUserDefault() {
allDone += 1
/// When all 3 services are finish
if allDone == 3 {
if !UserDefaults.standard.bool(forKey: "hasRegistered") {
self.performSegue(withIdentifier: "ToRegistration", sender: self)
} else {
self.performSegue(withIdentifier: "ToMain", sender: self)
}
}
}
}
Hope it helps you!
EDIT: This is the simplest solution based on your existing code, if you want to do it 'the right way' you may want to check Promises: http://khanlou.com/2016/08/promises-in-swift/ https://www.raywenderlich.com/9208-getting-started-with-promisekit
You need to use PromiseKit and do something like this:
Replace your API methods so they use Promise as a response:
func checkVersion() -> Promise<CheckVersionResponse> {
sessionManager?.adapter = BaseClientInterceptor()
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
let buildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
// Return a Promise for the caller of this function to use.
return Promise { fulfill, reject in
// Inside the Promise, make an HTTP request
sessionManager?.request(Config.VERSION_URL + "/\(appVersion)/\(buildNumber)" + Config.STATUS, method: .get).responseData { response in
print(response.response?.statusCode)
switch response.result {
case .success(let value):
print("----WS: checkVersion Success-----")
let value = JSON(value)
print(value)
let response = CheckVersionResponse(responseCode: response.response?.statusCode ?? -1)
if (response.getResponseCode() == 200) {
response.setUrl(value["url"].string ?? "")
response.setNow(value["now"].intValue)
response.setStatus(value["status"].stringValue)
response.setMessage(value["message"].stringValue)
}
fulfill(response)
case .failure(let error):
print(error)
print("----WS: checkVersion Failed-----")
reject(error)
}
}
}
}
Then on the UIViewController call the WebServiceManager methods like this:
WebServiceManager.shared.checkVersion().then { response in
// ....
WebServiceManager.shared.getCurrentDate()
}.then { date in
// ...
WebServiceManager.shared.getUpdatedKeyValues()
}.catch { error in
print(error)
}

Related

RXSwift: Subscriber never gets call back

I have this function:
func makeRepoRequest() -> Single<[String: Any]> {
return Single<[String: Any]>.create {[weak self] observer in
guard let something = self?.temp else {
let disposeBag = DisposeBag()
self?.getRepo("364").subscribe(onSuccess: { content in
observer(.success(content))
}, onError: { error in
observer(.error(error))
}).disposed(by: disposeBag)
return Disposables.create()
}
observer(.success(something))
return Disposables.create()
}
}
is subscribe to this function:
func getRepo(_ repo: String) -> Single<[String: Any]> {
return Single<[String: Any]>.create { single in
print(repo)
let url = "https://api.github.com/repositories?since=\(repo)"
print(url)
let task = URLSession.shared.dataTask(with: URL(string:url)!) { data, _, error in
if let error = error {
single(.error(error))
return
}
guard let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []),
let result = json as? [String: Any] else {
let error = NSError(domain: "Decoding", code: 0, userInfo: nil)
single(.error(error))
return
}
single(.success(result))
}
task.resume()
return Disposables.create()
}
}
but for some reason the subscription it never gets a call back. Any of you knows why the subscription never gets a call back?
I'll really appreciate your help.
Your makeRepoRequest() is defined incorrectly. The disposable you create inside the closure should be the one that you return. There shouldn't be any disposeBag in there, also you need to unwrap self and make sure something is emitted if self doesn't exist, also if you are going to keep a cache in temp you really should assign to it...
func makeRepoRequest() -> Single<[String: Any]> {
return Single<[String: Any]>.create { [weak self] observer in
guard let this = self else {
observer(.error(MyError.missingSelf))
return Disposables.create()
}
guard !this.temp.isEmpty else {
return this.getRepo("364").subscribe(onSuccess: { content in
this.temp = content
observer(.success(content))
}, onError: { error in
observer(.error(error))
})
}
observer(.success(this.temp))
return Disposables.create()
}
}
However, since you are just emitting content with no changes, you don't even need to use .create(_:). So something like this:
func makeRepoRequest() -> Single<[String: Any]> {
if !temp.isEmpty {
return getRepo("364")
.do(onSuccess: { [weak self] in self?.temp = $0 })
}
else {
return Single.just(temp)
}
}
Lastly, you aren't properly canceling your network request in your getRepo(_:) method. It should end with return Disposables.create { task.cancel() }
I suggest you read up more on Disposables.

How to get an array from URLSession

Trying to make a program for a news site. I take information from the site through the api, everything works fine.
The only question is, how do I get this array out of the loop?
Here is my code:
import UIKit
class ViewController: UIViewController {
var news:[News] = []
override func viewDidLoad() {
super.viewDidLoad()
getUsers()
print(news)
}
func getUsers() {
guard let url = URL(string: "http://prostir.news/swift/api2.php") else {return}
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
news = try JSONDecoder().decode([News].self, from: data)
// print(self.news)
} catch let error {
print(error)
}
}
}.resume()
}
}
struct News:Codable, CustomStringConvertible{
let href:String?
let site:String?
let title:String?
let time:String?
var description: String {
return "(href:- \(href), site:- \(site), title:- \(title), time:- \(time))"
}
}
Declare news array in your class and assign the response to this array in getUsers method
var news:[News] = []
func getUsers(){
guard let url = URL(string: "https") else {return}
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
self.news = try JSONDecoder().decode([News].self, from: data)
print(self.news)
} catch let error {
print(error)
}
}
}.resume()
}
The fundamental problem is you are retrieving data asynchronously (e.g. getUsers will initiate a relatively slow request from the network using URLSession, but returns immediately). Thus this won’t work:
override func viewDidLoad() {
super.viewDidLoad()
getUsers()
print(news)
}
You are returning from getUsers before the news has been retrieved. So news will still be [].
The solution is to give getUsers a “completion handler”, a parameter where you can specify what code should be performed when the asynchronous request is done:
enum NewsError: Error {
case invalidURL
case invalidResponse(URLResponse?)
}
func getUsers(completion: #escaping (Result<[News], Error>) -> Void) {
let queue = DispatchQueue.main
guard let url = URL(string: "http://prostir.news/swift/api2.php") else {
queue.async { completion(.failure(NewsError.invalidURL)) }
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
queue.async { completion(.failure(error)) }
return
}
guard
let data = data,
let httpResponse = response as? HTTPURLResponse,
200 ..< 300 ~= httpResponse.statusCode
else {
queue.async { completion(.failure(NewsError.invalidResponse(response))) }
return
}
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
let news = try decoder.decode([News].self, from: data)
queue.async { completion(.success(news)) }
} catch let parseError {
queue.async { completion(.failure(parseError)) }
}
}.resume()
}
Then your view controller can fetch the news, passing a “closure”, i.e. code that says what to do when the asynchronous call is complete. In this case, it will set self.news and trigger the necessary UI update (e.g. maybe refresh tableview):
class ViewController: UIViewController {
var news: [News] = []
override func viewDidLoad() {
super.viewDidLoad()
fetchNews()
}
func fetchNews() {
getUsers() { result in
switch result {
case .failure(let error):
print(error)
case .success(let news):
self.news = news
print(news)
}
// trigger whatever UI update you want here, e.g., if using a table view:
//
// self.tableView.reloadData()
}
// but don't try to print the news here, as it hasn't been retrieved yet
// print(news)
}

UITextField to change API URL in Swift 5

I am new iOS Developer
I want to change the websiteLogo API with a textfield to change the URL.
how can I change the line with the ***
with a var and a textfield in my viewcontroller?
With screenshoot it's will be easier to understand what I want? Thank you !!! Guys. OneDriveLink. 1drv.ms/u/s!AsBvdkER6lq7klAqQMW9jOWQkzfl?e=fyqOeN
private init() {}
**private static var pictureUrl = URL(string: "https://logo.clearbit.com/:http://www.rds.ca")!**
private var task: URLSessionDataTask?
func getQuote(callback: #escaping (Bool, imageLogo?) -> Void) {
let session = URLSession(configuration: .default)
task?.cancel()
task = session.dataTask(with: QuoteService.pictureUrl) { (data, response, error) in
DispatchQueue.main.async {
guard let data = data, error == nil else {
callback(false, nil)
return
}
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
callback(false, nil)
return
}
let quote = imageLogo(image: data)
callback(true, quote)
print(data)
}
}
task?.resume()
}
First, please don't use screenshots do show your code. If you want help, others typically copy/paste your code to check whats wrong with it.
There are some minor issues with your code. Some hints from me:
Start your types with a big letter, like ImageLogo not imageLogo:
Avoid statics
Avoid singletons (they are almost statics)
Hand in the pictureUrl into getQuote
struct ImageLogo {
var image:Data
}
class QuoteService {
private var task: URLSessionDataTask?
func getQuote(from pictureUrl:URL, callback: #escaping (Bool, ImageLogo?) -> Void) {
let session = URLSession(configuration: .default)
task?.cancel()
task = session.dataTask(with: pictureUrl) {
(data, response, error) in
DispatchQueue.main.async {
guard let data = data, error == nil else {
callback(false, nil)
return
}
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
callback(false, nil)
return
}
let quote = ImageLogo(image: data)
callback(true, quote)
print(data)
}
}
task?.resume()
}
}
Store an instance of QuoteService in your view controller
Call getQuote on that instance, handing in the pictureUrl
class ViewController : UIViewController {
var quoteService:QuoteService!
override func viewDidLoad() {
self.quoteService = QuoteService()
}
func toggleActivityIndicator(shown:Bool) { /* ... */ }
func update(quote:ImageLogo) { /* ... */ }
func presentAlert() { /* ... */ }
func updateconcept() {
guard let url = URL(string:textField.text!) else {
print ("invalid url")
return
}
toggleActivityIndicator(shown:true)
quoteService.getQuote(from:url) {
(success, quote) in
self.toggleActivityIndicator(shown:false)
if success, let quote = quote {
self.update(quote:quote)
} else {
self.presentAlert()
}
}
}
/* ... */
}
Hope it helps.
I think you want to pass textfield Text(URL Enter By user) in Web Services
Add a parameter url_str in getQuote function definition first and pass textfield value on that parameters
fun getQuote(url_str : String, callback : #escaping(Bool, ImgaeLogo/)->void){
}

Cannot append data to array from GET request

I am trying to load data from a GET request using Alamofire library in swift and cannot append data from the requests. I am trying to populate an array of orders to load into a UITableView.
I have tried a few various ways of solving this issue but nothing is working for me. I have commented out the method I tried because with 2 separate calls to fetchAll...Orders and the second call always overwrites the first and then the tableView is loaded with missing items.
class DrinkOrdersTableViewController: UITableViewController {
var orders: [Order] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Current Orders"
}
override func viewWillAppear(_ animated: Bool) {
// fetchAllBeerOrders { orders in
// self.orders = orders!
// //print("Beer fetch: ", self.orders)
// self.tableView.reloadData()
// }
// fetchAllCocktailOrders { orders in
// self.orders = orders!
// //print("Cocktail fetch: ", self.orders)
// self.tableView.reloadData()
// }
fetchAllBeerOrders { orders in
self.orders.append(orders)
self.tableView.reloadData()
}
fetchAllCocktailOrders { orders in
self.orders.append(orders)
self.tableView.reloadData()
}
}
private func fetchAllCocktailOrders(completion: #escaping([Order]?) -> Void) {
Alamofire.request("http://127.0.0.1:4000/orders", method: .get)
.validate()
.responseJSON { response in
guard response.result.isSuccess else { return completion(nil) }
guard let rawInventory = response.result.value as? [[String: Any]?] else { return completion(nil) }
let currentOrders = rawInventory.compactMap { ordersDict -> Order? in
guard let orderId = ordersDict!["id"] as? String,
let orderStatus = ordersDict!["status"] as? String,
var pizza = ordersDict!["cocktail"] as? [String: Any] else { return nil }
pizza["image"] = UIImage(named: pizza["image"] as! String)
return Order(
id: orderId,
pizza: Pizza(data: pizza),
status: OrderStatus(rawValue: orderStatus)!
)
}
completion(currentOrders)
}
}
private func fetchAllBeerOrders(completion: #escaping([Order]?) -> Void) {
Alamofire.request("http://127.0.0.1:4000/orders", method: .get)
.validate()
.responseJSON { response in
guard response.result.isSuccess else { return completion(nil) }
guard let rawInventory = response.result.value as? [[String: Any]?] else { return completion(nil) }
let currentOrders = rawInventory.compactMap { ordersDict -> Order? in
guard let orderId = ordersDict!["id"] as? String,
let orderStatus = ordersDict!["status"] as? String,
var pizza = ordersDict!["pizza"] as? [String: Any] else { return nil }
pizza["image"] = UIImage(named: pizza["image"] as! String)
return Order(
id: orderId,
pizza: Pizza(data: pizza),
status: OrderStatus(rawValue: orderStatus)!
)
}
completion(currentOrders)
}
}
As of right now I am getting this error with code above: Cannot convert value of type '[Order]?' to expected argument type 'Order'. The ideal outcome of this code is to have the data that is gathered from each GET request to append to the array of Orders. I have verified that the GET requests are working and giving back the correct data. Please Help :]
You declared orders of type [Order] and your fetch methods compilation blocks return [Order]?. As you can see, you cannot convert value of type [Order]? to expected argument type Order when you wrote self.orders.append(orders).
To fix these, put a guard unwrap in fetch method invocations.
fetchAllBeerOrders { orders in
guard let _orders = orders else { return }
self.orders.append(_orders)
self.tableView.reloadData()
}
fetchAllCocktailOrders { orders in
guard let _orders = orders else { return }
self.orders.append(_orders)
self.tableView.reloadData()
}
Now, you have a potential memory leak in your code. fetchAllBeerOrders and fetchAllCocktailOrders are async methods with compilation blocks. You cannot use a strong reference to self here. Use weak to avoid a memory leak, like:
fetchAllBeerOrders { [weak self] orders in
guard let _orders = orders else { return }
self?.orders.append(_orders)
self?.tableView.reloadData()
}
fetchAllCocktailOrders { [weak self] orders in
guard let _orders = orders else { return }
self?.orders.append(_orders)
self?.tableView.reloadData()
}

unwrapping an Optional value

I am trying to login with macbook using code but I keep on getting this error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
// API to log an user in
func login(userType: String, completionHandler: #escaping (NSError?) -> Void) {
let path = "api/social/convert-token/"
let url = baseURL!.appendingPathComponent(path)
let params: [String: Any] = [
"grant_type": "convert_token",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"backend": "facebook",
"token": FBSDKAccessToken.current().tokenString,
"user_type": userType
]
Alamofire.request(url!, method: .post, parameters: params, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
switch response.result {
case .success(let value):
let jsonData = JSON(value)
self.accessToken = jsonData["access_token"].string!
self.refreshToken = jsonData["refresh_token"].string!
self.expired = Date().addingTimeInterval(TimeInterval(jsonData["expire_in"].int!))
completionHandler(nil)
break
case .failure(let error):
completionHandler(error as NSError?)
break
}
}
}
The error is referring to this line:
self.accessToken = jsonData["access_token"].string!
and this is LoginViewController code:
import UIKit
import FBSDKLoginKit
class LoginViewController: UIViewController {
#IBOutlet weak var bLogin: UIButton!
#IBOutlet weak var bLogout: UIButton!
var fbLoginSuccess = false
var userType: String = USERTYPE_CUSTOMER
override func viewDidLoad() {
super.viewDidLoad()
if (FBSDKAccessToken.current() != nil) {
bLogout.isHidden = false
FBManager.getFBUserData(completionHandler: {
self.bLogin.setTitle("Continue as \(User.currentUser.email!)", for: .normal)
// self.bLogin.sizeToFit()
})
}
}
override func viewDidAppear(_ animated: Bool) {
if (FBSDKAccessToken.current() != nil && fbLoginSuccess == true) {
performSegue(withIdentifier: "CustomerView", sender: self)
}
}
#IBAction func facebookLogout(_ sender: AnyObject) {
APIManager.shared.logout { (error) in
if error == nil {
FBManager.shared.logOut()
User.currentUser.resetInfo()
self.bLogout.isHidden = true
self.bLogin.setTitle("Login with Facebook", for: .normal)
}
}
}
#IBAction func facebookLogin(_ sender: AnyObject) {
if (FBSDKAccessToken.current() != nil) {
APIManager.shared.login(userType: userType, completionHandler: { (error) in
if error == nil {
self.fbLoginSuccess = true
self.viewDidAppear(true)
}
})
} else {
FBManager.shared.logIn(
withReadPermissions: ["public_profile", "email"],
from: self,
handler: { (result, error) in
if (error == nil) {
FBManager.getFBUserData(completionHandler: {
APIManager.shared.login(userType: self.userType, completionHandler: { (error) in
if error == nil {
self.fbLoginSuccess = true
self.viewDidAppear(true)
}
})
})
}
})
}
}
}
Swift 3
Xcode 9
iOS 10.2
I read several texts to find out the causes of this type of error but not succeed.
First of all, it is a bad practice to use force unwrap (!) everywhere. Try to avoid it, until you really need it and/or know what are you doing. Here you use SwiftyJSON. It helps you to extract data from JSON in a convenient way. But you shouldn't rely, that you will always get proper JSON from the backend. There are many reasons why it can return wrong JSON and there would not be needed value. There are two options:
You can use .stringValue instead of .string - it will return an empty string instead of nil
You can do in this way: if let token = jsonData["access_token"].string {...} or even use guard statement
Here is a good article to understand force unwrapping: https://blog.timac.org/2017/0628-swift-banning-force-unwrapping-optionals/
This error happens when you have a ! (force unwrap symbol), meaning that you're certain the data will be there; but in fact, the data isn't there - it's nil.
Try using a guard statement. For example:
guard let self.accessToken = jsonData["access_token"].string else {
// log error message, if desired; then exit the function
return
}
Alternatively, you could use an if let statement:
if let self.accessToken = jsonData["access_token"].string {
// do stuff if 'jsonData["access_token"].string' is valid
}
else {
// do other stuff if 'jsonData["access_token"].string' is nil
}
Now, why the data is not there (nil) - that's another question. Perhaps check your JSON function to ensure it's properly processing the JSON response. Also, check to make sure you're getting a valid response:
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode == 200 else {
// handle a bad response code
return
}
// handle the good response code stuff after the guard statement
Learn about using if let to handle possible nil values in the Swift Programming Language (Swift 4.2) Guide under Optional Binding in the Basics section.
Learn about guard statements under Early Exit in the Control Flow section of the Guide, and also in the Statements section of the Reference.

Resources