Hello All,
I am trying to create simple login and signup with php mysql in backend. I was following "https://github.com/EMacco/ios-tutorials/tree/yummie-network-connection/Yummie" this project"
I get the following error
The response is: {"data":[{"response":"Success","username":"Mohammad Tariq Shamim"}]}
Error Response could not be decoded
I am getting response from the server but JSON is not getting decoded. Following is my code
enum Route {
static let baseurl = "http://arabscholar.net/android/"
case login
var description: String {
switch self {
case .login: return "login.php"
}
}
}
struct User: Decodable{
let username: String?
let response: String?
}
struct NetworkService {
static let shared = NetworkService()
private init() {}
func signin(username: String, password: String, completion: @escaping(Result<User, Error>) -> Void) {
let params = ["login": "user", "email": username, "password": password]
request(route: .login, method: .post, parameters: params, completion: completion)
}
private func request<T: Decodable>(route: Route, method: Method, parameters: [String: Any]? = nil, completion: @escaping(Result<T, Error>) -> Void) {
guard let request = createRequest(route: route, method: method, parameters: parameters) else {
completion(.failure(AppError.unknownError))
return
}
URLSession.shared.dataTask(with: request) { data, response, error in
var result: Result<Data, Error>?
if let data = data {
result = .success(data)
let responseString = String(data: data, encoding: .utf8) ?? "Could not stringify our data"
print("The response is: (responseString)")
} else if let error = error {
result = .failure(error)
print("The error is " + error.localizedDescription)
}
DispatchQueue.main.async {
self.handleResponse(result: result, completion: completion)
}
}.resume()
}
private func handleResponse<T: Decodable>(result: Result<Data, Error>?, completion: (Result<T, Error>) -> Void) {
guard let result = result else {
completion(.failure(AppError.unknownError))
return
}
switch result {
case .success(let data):
let decoder = JSONDecoder()
guard let response = try? decoder.decode(ApiResponse.self, from: data) else {
completion(.failure(AppError.errorDecoding))
return
}
if let error = response.error {
completion(.failure(AppError.serverError(error)))
return
}
if let decodedData = response.data {
completion(.success(decodedData))
} else {
completion(.failure(AppError.unknownError))
}
case .failure(let error):
completion(.failure(error))
}
}
private func createRequest(route: Route, method: Method, parameters: [String: Any]? = nil) -> URLRequest? {
let urlString = Route.baseurl + route.description
guard let url = urlString.asUrl else {return nil}
var urlRequest = URLRequest(url: url)
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpMethod = method.rawValue
if let params = parameters {
switch method {
case .get:
var urlComponent = URLComponents(string: urlString)
urlComponent?.queryItems = params.map { URLQueryItem(name: $0, value: "($1)")}
urlRequest.url = urlComponent?.url
case .post:
let bodyData = try?JSONSerialization.data(withJSONObject: params, options: [])
urlRequest.httpBody = bodyData
}
}
return urlRequest
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I know this question is asked many times over here but I have tried all the solutions but none of them helped me out.
I am getting error in this line
myWebView.load(URLRequest(url: url))
I am using navigation controller and segue performs programatically when the user defaults are available from below
class ViewController: UIViewController {
let defaults = UserDefaults.standard
var username = ""
var password = ""
override func viewDidLoad() {
super.viewDidLoad()
if(defaults.bool(forKey: "isLoggedin") == true){
username = defaults.string(forKey: "username") ?? ""
password = defaults.string(forKey: "password") ?? ""
if(username != "" && password != ""){
self.navigationController?.pushViewController(WebViewViewController(), animated: true)
}
}
}
}
Following is my webview uiViewController
import WebKit
class WebViewViewController: UIViewController {
@IBOutlet weak var myWebView: WKWebView!
let defaults = UserDefaults.standard
var username = ""
var password = ""
override func viewDidLoad() {
super.viewDidLoad()
//myWebView = WKWebView()
navigationController?.setNavigationBarHidden(true, animated: true)
if(defaults.bool(forKey: "isLoggedin") == true){
username = defaults.string(forKey: "username") ?? ""
password = defaults.string(forKey: "password") ?? ""
if(username != "" && password != ""){
guard let url = URL(string: "https://google.com") else {return}
myWebView.load(URLRequest(url: url))
}
}
}
}
My story board screen shot
Hello everyone,
I am developing a very simple app which contains only 2 view controllers. First one for app introduction (I have used Collection view for sliding images) and second one for loading website in wkwebview. Issue is that the website is a little bit heavy and takes around 5-8 seconds to load.
I need that that wekwebview to be loaded in the first view controller and once it is loaded it automatically pushes to the second view controller in which wkwebview is.
Following is what I have done till now
First view controller
class OnBoardingViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var LoginBtn: UIButton!
@IBOutlet weak var SignupBtn: UIButton!
var slides: [OnboardingSlide] = []
var currenrPage = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
slides = [OnboardingSlide(image: UIImage(imageLiteralResourceName: "slide1")),
OnboardingSlide(image: UIImage(imageLiteralResourceName: "slide2")),
OnboardingSlide(image: UIImage(imageLiteralResourceName: "slide3"))]
let wvc = storyboard!.instantiateViewController(withIdentifier: "webViewController") as! ViewController
//wvc.modalPresentationStyle = .fullScreen
wvc.loadWebView {
self.navigationController?.pushViewController(wvc, animated: true)
//self.present(wvc, animated: true, completion: nil)
}
}
Second view controller (wkwebview is in this view controller)
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var myWebView: WKWebView!
var didLoadWebView: (() -> Void)!
func loadWebView(completion: @escaping () -> Void){
loadViewIfNeeded()
didLoadWebView = completion
myWebView.navigationDelegate = self
guard let url = URL(string: "https://fashionhind.com") else {return}
myWebView.load(URLRequest(url: url))
myWebView.allowsBackForwardNavigationGestures = true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationController?.isNavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
didLoadWebView() // Call back to the presenting view controller
}
}
Following is the issue
Whenever I run the wkwebview loads fine but when I click on any button on the website app crashed with following error in this line "self.navigationController?.pushViewController(wvc, animated: true)"
"Exception NSException * "Application tried to present modally a view controller <Fashionhind.ViewController: 0x14bf18ed0> that is already being presented by <UINavigationController: 0x14d81aa00>." 0x0000600001b2d320"
Thanks in advance for help