Login function with activity indicator - ios

I am currently doing a login test and have to log in again to log in. After pressing the login button to send the request to the server, the activity indicator is displayed on the screen until the activity indicator is stopped and the activity indicator is stopped The next page, I would like to open another thread used to implement the activity indicator, but I do not know how to implement, please help!
And this code is modified from http://www.kaleidosblog.com/how-to-create-a-login-screen-page-in-swift-3-authenticate-an-user-and-keep-the-session-active.
class ViewController: UIViewController {
let login_url = "http://www.kaleidosblog.com/tutorial/login/api/Login"
let checksession_url = "http://www.kaleidosblog.com/tutorial/login/api/CheckSession"
#IBOutlet var username_input: UITextField!
#IBOutlet var password_input: UITextField!
#IBOutlet var login_button: UIButton!
var login_session:String = ""
var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
override func viewDidLoad() {
super.viewDidLoad()
username_input.text = "try#me.com"
password_input.text = "test"
activityIndicator.hidesWhenStopped = true;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray;
activityIndicator.center = view.center;
let preferences = UserDefaults.standard
if preferences.object(forKey: "session") != nil
{
login_session = preferences.object(forKey: "session") as! String
check_session()
}
}
#IBAction func DoLogin(_ sender: AnyObject) {
login_now(username:username_input.text!, password: password_input.text!)
}
#IBAction func backToMain(_ segue:UIStoryboardSegue){
let preferences = UserDefaults.standard
preferences.removeObject(forKey: "session")
self.dismiss(animated: true, completion: nil)
}
func login_now(username:String, password:String)
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(username, forKey: "username")
post_data.setValue(password, forKey: "password")
let url:URL = URL(string: login_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
self.login_session = session_data
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
DispatchQueue.main.async {
self.view.addSubview(self.activityIndicator)
}
}
}
})
task.resume()
LoginDone()
}
func check_session()
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(login_session, forKey: "session")
let url:URL = URL(string: checksession_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let response_code = server_response["response_code"] as? Int
{
if(response_code == 200)
{
DispatchQueue.main.async {
self.view.addSubview(self.activityIndicator)
}
}
else
{
}
}
})
task.resume()
}
func LoginDone()
{
self.performSegue(withIdentifier: "gotomenu", sender: nil)
}
func startActivityIndicator(){
activityIndicator.startAnimating()
}
func stopActivityIndicator() {
activityIndicator.stopAnimating()
}
}

Try to use SVProgressHUD. It's the simplest framework, that I have ever seen. SVProgressHUD on GitHub
Install:
In your podfile:
pod 'SVProgressHUD'
Then in terminal:
pod install
How to use:
import SVProgressHUD
to start:
SVProgressHUD.show()
to dismiss:
SVProgressHUD.dismiss()
Its very simple and usefull. Just try it.
Hope it helps

Replace this code code with old code:
class ViewController: UIViewController {
let login_url = "http://www.kaleidosblog.com/tutorial/login/api/Login"
let checksession_url = "http://www.kaleidosblog.com/tutorial/login/api/CheckSession"
#IBOutlet var username_input: UITextField!
#IBOutlet var password_input: UITextField!
#IBOutlet var login_button: UIButton!
var login_session:String = ""
var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
override func viewDidLoad() {
super.viewDidLoad()
username_input.text = "try#me.com"
password_input.text = "test"
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.center = view.center
activityIndicator.isHidden = true
self.view.addSubview(self.activityIndicator)
let preferences = UserDefaults.standard
if preferences.object(forKey: "session") != nil
{
login_session = preferences.object(forKey: "session") as! String
check_session()
}
}
#IBAction func DoLogin(_ sender: AnyObject) {
login_now(username:username_input.text!, password: password_input.text!)
}
#IBAction func backToMain(_ segue:UIStoryboardSegue){
startActivityIndicator()
activityIndicator.isHidden = false
let preferences = UserDefaults.standard
preferences.removeObject(forKey: "session")
self.dismiss(animated: true, completion: nil)
stopActivityIndicator()
activityIndicator.isHidden = true
}
func login_now(username:String, password:String)
{
activityIndicator.isHidden = false
startActivityIndicator()
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(username, forKey: "username")
post_data.setValue(password, forKey: "password")
let url:URL = URL(string: login_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
self.stopActivityIndicator()
self.activityIndicator.isHidden = true
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
self.login_session = session_data
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
DispatchQueue.main.async {
self.LoginDone()
}
}
}
})
task.resume()
}
func check_session()
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(login_session, forKey: "session")
let url:URL = URL(string: checksession_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let response_code = server_response["response_code"] as? Int
{
if(response_code == 200)
{
DispatchQueue.main.async {
self.view.addSubview(self.activityIndicator)
}
}
else
{
}
}
})
task.resume()
}
func LoginDone()
{
self.performSegue(withIdentifier: "gotomenu", sender: nil)
}
func startActivityIndicator(){
activityIndicator.startAnimating()
}
func stopActivityIndicator() {
activityIndicator.stopAnimating()
}
}

Related

How to save one person UID globally to retrieve it in any ViewController in Swift

After successful registration, I am getting UID in LoginViewController. I am saving UID in LoginViewController. and I am retrieving UID in RegistrationViewController. but here I am getting nil for all person why?
Please help me in the code.
saving UID in LoginViewController:
func logInService(){
let parameters = ["username":Int(userIdTextFielf.text ?? "") as Any,
"imei_number":"",
"password":passwordTextField.text as Any,
"name":"name"]
let url = URL(string: "https://dev.com/webservices/login")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters as Any, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of loginnnnnn \(json)")
self.Uid = json["id"] as? Int
let emailL = json["user_email"] as? String
KeychainWrapper.standard.set(emailL ?? "", forKey: "user_email")
KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")
let saveUserId: Bool = KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")
DispatchQueue.main.async {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeNavigation")
let appDelagate = UIApplication.shared.delegate
appDelagate?.window??.rootViewController = navigationController
}
}catch{
print("error")
}
}
}).resume()
}
retrieving UID in RegistrationViewController:
#IBAction func registerButton(_ sender: Any) {
if (nameTextField.text == "" || phoneNumTextField.text == "" || passwordTextField.text == "" || conformPasswordTextField.text == "")
{
registerButton.isHidden = false
sendOtpButton.isHidden = true
AlertFun.ShowAlert(title: "Title", message: "RequiredAllFields", in: self)
}
else{
registerButton.isHidden = true
sendOtpButton.isHidden = false
otpTextField.isHidden = false
resendButn.isHidden = false
DispatchQueue.main.async {
self.otpTextField.text = self.otpField as? String
}
registerService()
otpTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
}
}
#IBAction func sendOTPButton(_ sender: Any) {
otpService()
}
//MARK:- Service part
#objc func registerService()
{
print("register tapped")
let parameters = ["mobile_number": Int(phoneNumTextField.text ?? "") as Any,
"email":emailTextField.text as Any,
"password":passwordTextField.text as Any,
"name": nameTextField.text as Any]
let url = URL(string: "https://dev.anyemi.com/webservices/anyemi/register")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let userId: Int? = KeychainWrapper.standard.integer(forKey: "Uid")
print("login userid \(userId)")
if userId != nil{
AlertFun.ShowAlert(title: "Title", message: "user exist", in: self)
}
else{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json regggggggggggis \(json)")
let phNum = json["mobile_number"] as? Int
let status = json["status"] as? String
self.otpField = json["otp"] as? Int
}
}catch{
print("error")
}
}
}).resume()
}
#objc func otpService(){
let parameters = ["mobile_number": phoneNumTextField.text as Any,
"otp": otpTextField.text as Any]
let url = URL(string: "https://dev.com/webservices/otpverify")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of otppppppppp \(json)")
DispatchQueue.main.async {
if (self.otpTextField.text == String(self.otpField ?? 12)){
print("registration successfullllll...")
let mobileNum = json["mobile_number"] as! [String : Any]
//self.Uid = mobileNum["id"] as? String
let name = mobileNum["name"] as? String
let phNum = mobileNum["username"] as? String
print("otp name \(String(describing: name))")
print("otp phnumber \(String(describing: phNum))")
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.present(loginVC, animated: true)
}
else if self.otpTextField.text == ""{
AlertFun.ShowAlert(title: "", message: "Please enter OTP", in: self)
print("register fail")
}
else {
AlertFun.ShowAlert(title: "", message: "Invalid OTP", in: self)
print("register fail")
}
}
}catch{
print("error")
}
}
}).resume()
}
}
All the time I am going to else part why? where I did mistake.
KeychainWrapper is not a good approach to save user details. Better to go with "UserDefaults".

How do I make a request to a REST API, with a certificate, in swift?

I want to pass pfx file through rest call api in swift. pfx file stored in local drive.I will done in python so sample code is below
var json=["{\"panInquiry\":{\"Header\":{\"TranID\":\"12345\",\"Corp_ID\":\"ZSA01\",\"Maker_ID\":\"\",\"Checker_ID\":\"\",\"Approver_ID\":\"\",\"Nsdl_UserID\":\"B3456789\"},\"Body\":{\"panNumbers\":[{\"pan1\":\"abba#123\"}]}}}"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url=URL(string:"https://dshdjksdhsjhd")
// let pathToCert = Bundle.main.path(forResource: "mychatbot", ofType: "pfx")
// let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)!
// var pemPath: String? = "/Users/rohitverma/Desktop/A/mychatbot.pfx"
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let str="ZSA:pas#12345"
let utf8str = str.data(using: String.Encoding.utf8)
let base64Encoded = utf8str?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
//print("Encoded: base64Encoded")
request.setValue("Basic \(base64Encoded)", forHTTPHeaderField: "Authorization")
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()
}
}
This will be process but I don't know how pass ssl certificate via request in swift.
In python it will be done by following code
response = requests.post(url, json=json.loads(jsonstr, object_pairs_hook=OrderedDict),headers={"Authorization": "Basic %s" % b64Val},cert='C:\\Users\\lenovo\\Desktop\\mychatbot.pem', verify=True)
You have to implement URLSessionDelegate and do the following :
static func getCertSession() -> URLSession {
let sessionDelegate = MySessionDelegate()
let conf = URLSessionConfiguration.default
conf.requestCachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
conf.timeoutIntervalForRequest = 1000
conf.timeoutIntervalForResource = 1000
return URLSession.init(configuration: conf, delegate: sessionDelegate, delegateQueue: OperationQueue.main)
}
class MySessionDelegate : NSObject : URLSessionDelegate {
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let localCertPath = Bundle.main.url(forResource: PATH_OF_YOUR_CERT, withExtension: "pfx"),
let localCertData = try? Data(contentsOf: localCertPath)
{
let identityAndTrust:IdentityAndTrust = extractIdentity(certData: localCertData as NSData, certPassword: PASSWORD_OF_YOUR_PFX)
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate {
let urlCredential:URLCredential = URLCredential(
identity: identityAndTrust.identityRef,
certificates: identityAndTrust.certArray as [AnyObject],
persistence: URLCredential.Persistence.forSession);
completionHandler(URLSession.AuthChallengeDisposition.useCredential, urlCredential);
return
}
}
challenge.sender?.cancel(challenge)
completionHandler(URLSession.AuthChallengeDisposition.rejectProtectionSpace, nil)
}
}
Finally perform your task using getCertSession().task(...) instead of URLSeesion.shared.task(...)
Edit
public struct IdentityAndTrust {
public var identityRef:SecIdentity
public var trust:SecTrust
public var certArray:NSArray
}
public func extractIdentity(certData:NSData, certPassword:String) -> IdentityAndTrust {
var identityAndTrust:IdentityAndTrust!
var securityError:OSStatus = errSecSuccess
var items: CFArray?
let certOptions: Dictionary = [ kSecImportExportPassphrase as String : certPassword ];
// import certificate to read its entries
securityError = SecPKCS12Import(certData, certOptions as CFDictionary, &items);
if securityError == errSecSuccess {
let certItems:CFArray = items as CFArray!;
let certItemsArray:Array = certItems as Array
let dict:AnyObject? = certItemsArray.first;
if let certEntry:Dictionary = dict as? Dictionary<String, AnyObject> {
// grab the identity
let identityPointer:AnyObject? = certEntry["identity"];
let secIdentityRef:SecIdentity = identityPointer as! SecIdentity!;
// grab the trust
let trustPointer:AnyObject? = certEntry["trust"];
let trustRef:SecTrust = trustPointer as! SecTrust;
// grab the certificate chain
var certRef: SecCertificate?
SecIdentityCopyCertificate(secIdentityRef, &certRef);
let certArray:NSMutableArray = NSMutableArray();
certArray.add(certRef as SecCertificate!);
identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: certArray);
}
}
return identityAndTrust;
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func pfxcl(_ sender: Any) {
print("Btn press")
var json=["{\"panInquiry\":{\"Header\":{\"TranID\":\"12345\",\"Corp_ID\":\"ASXE\",\"Maker_ID\":\"\",\"Checker_ID\":\"\",\"Approver_ID\":\"\",\"Nsdl_UserID\":\"ASFD454D\"},\"Body\":{\"panNumbers\":[{\"pan1\":\"qswerf98\"}]}}}"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url=URL(string:"https://apideveloper.com")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let str="ACZZ:password#345"
let utf8str = str.data(using: String.Encoding.utf8)
let base64Encoded = utf8str?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
//print("Encoded: base64Encoded")
request.setValue("Basic \(base64Encoded)", forHTTPHeaderField: "Authorization")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()
class MySessionDelegate : NSObject , URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let localCertPath = Bundle.main.url(forResource: "mychatbot", withExtension: "pfx"),
let localCertData = try? Data(contentsOf: localCertPath)
{
let identityAndTrust : IdentityAndTrust = extractIdentity(certData: localCertData as NSData, certPassword: "ACZZ:password#345")
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate {
let urlCredential:URLCredential = URLCredential(
identity: identityAndTrust.identityRef,
certificates: identityAndTrust.certArray as [AnyObject],
persistence: URLCredential.Persistence.forSession);
completionHandler(URLSession.AuthChallengeDisposition.useCredential, urlCredential);
return
}
}
challenge.sender?.cancel(challenge)
completionHandler(URLSession.AuthChallengeDisposition.rejectProtectionSpace, nil)
}
func getCertSession() -> URLSession {
let sessionDelegate = MySessionDelegate()
let conf = URLSessionConfiguration.default
conf.requestCachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
conf.timeoutIntervalForRequest = 1000
conf.timeoutIntervalForResource = 1000
return URLSession.init(configuration: conf, delegate: sessionDelegate as! URLSessionDelegate, delegateQueue: OperationQueue.main)
}
struct IdentityAndTrust {
public var identityRef:SecIdentity
public var trust:SecTrust
public var certArray:NSArray
}
func extractIdentity(certData:NSData, certPassword:String) -> IdentityAndTrust {
var identityAndTrust:IdentityAndTrust!
var securityError:OSStatus = errSecSuccess
var items: CFArray?
let certOptions: Dictionary = [ kSecImportExportPassphrase as String : certPassword ];
securityError = SecPKCS12Import(certData, certOptions as CFDictionary, &items);
if securityError == errSecSuccess {
let certItems:CFArray = items as CFArray!;
let certItemsArray:Array = certItems as Array
let dict:AnyObject? = certItemsArray.first;
if let certEntry:Dictionary = dict as? Dictionary<String, AnyObject> {
let identityPointer:AnyObject? = certEntry["identity"];
let secIdentityRef:SecIdentity = identityPointer as! SecIdentity!;
let trustPointer:AnyObject? = certEntry["trust"];
let trustRef:SecTrust = trustPointer as! SecTrust;
var certRef: SecCertificate?
SecIdentityCopyCertificate(secIdentityRef, &certRef);
let certArray:NSMutableArray = NSMutableArray();
certArray.add(certRef as SecCertificate!);
identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: certArray);
}
}
return identityAndTrust;
}
}
}
}

how make REST API Call with Model class in swift 3 ios

i want to make REST API call in swift 3.0 with model classes of API code . Can any one help me to solved my issue?
let USER_LIST_FIRMWISE_URL = "\(BASE_URL2)user_list_firm_wise.php"
func userListFirmwiseRequest(firmid:String) -> URLRequest {
let Url = URL(string: (utility?.USER_LIST_FIRMWISE_URL)!)
var request = URLRequest(url: Url!)
request.httpMethod = "POST"
request.cachePolicy = .reloadIgnoringCacheData
let paramString = "firm_id=\(firmid)"
request.httpBody = paramString.data(using: String.Encoding.utf8)
return request
}
func getuserList() -> Void {
var request = mainApp.reqRes?.userListFirmwiseRequest(firmid: firmID)
request?.httpMethod = "POST"
print("request")
EZLoadingActivity.show("", disableUI: true)
let task = URLSession.shared.dataTask(with: request!) { (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
do {
guard let data = data, error == nil else {
print("Error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200{
print("Status code should be 200, but is \(httpStatus.statusCode)")
print("Response String:\(String(describing: response))")
}
let jsonString = try? JSONSerialization.jsonObject(with: data, options: [])
guard let JsonObject = try? jsonString as AnyObject else{
return
}
guard let Success = try? JsonObject["success"] as! Int else{
return
}
print("Succee Resul :\(Success)")
if Success > 0{
guard let Message = try? JsonObject["message"] as Any else{
return
}
guard let JsonObjectForUser = try? Message as? AnyObject else {
return
}
}
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary ?? nil
if let parseJSON = json {
let dataDic = parseJSON.value(forKey: "message") as! NSMutableArray
self.arrayUserNameList = dataDic.value(forKey: "user_name") as! [String]
self.arrayUserData = dataDic
print(self.arrayUserNameList)
self.picker.reloadAllComponents()
EZLoadingActivity.hide()
}
} catch{
print(error)
EZLoadingActivity.hide()
}
}
}
task.resume()
}
i have used general code of NSurlsession for API call but it's too much code i have to write every time in every call. so i want common class for API CAll. but how to make common class for API call in swift 3.0
Try something like that
import Foundation
struct modelApi {
var arrayUserData: NSMutableArray = []
var arrayUserNameList: [String] = []
init(data:Data)
{
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary ?? nil
if let parseJSON = json
{
let dataDic = parseJSON.value(forKey: "message") as! NSMutableArray
self.arrayUserNameList = dataDic.value(forKey: "user_name") as! [String]
self.arrayUserData = dataDic
}
}
catch
{
print("Error")
}
}
}
import Foundation
class HttpRequest{
static let TIME_OUT_FOR_RESOURCE = 60.0
static let TIME_OUT_FOR_REQUEST = 30
static func sendHttpGetRequest(endPoint:String,responseMsg:#escaping (_ responseString:String?,_ error:String?)->Void) {
// let JSON_CONTENT_TYPE = "application/json"
// let HTTP_OK = 200
//Content Types
//1 ""Content-Type" = "text/html; charset=UTF-8";"
//2 "Content-Type" = "application/json";
let url = URL.init(string: endPoint)
let session = URLSession.shared
session.configuration.timeoutIntervalForRequest = TimeInterval(TIME_OUT_FOR_REQUEST)
session.configuration.timeoutIntervalForResource = TimeInterval(TIME_OUT_FOR_RESOURCE)
let task = session.dataTask(with: url!) {
data,
response ,
error in
guard error == nil
else{
responseMsg(nil,error!.localizedDescription)
return }
let responseString = String.init(data: data!, encoding: String.Encoding.utf8)!
responseMsg(responseString,nil)
}
task.resume()
}
static func setHttpPostRequest(endPoint:String,param:[String:Any],responseMsg:#escaping (_ responseJson:[String:Any]?,_ error:String?)->Void){
let url = URL.init(string:endPoint)
let session = URLSession.shared
session.configuration.timeoutIntervalForRequest = TimeInterval(TIME_OUT_FOR_REQUEST)
session.configuration.timeoutIntervalForResource = TimeInterval(TIME_OUT_FOR_RESOURCE)
var request = URLRequest.init(url: url!)
request.httpMethod = "POST"
do{
request.httpBody = try JSONSerialization.data(withJSONObject: param, options: .prettyPrinted)
}catch let error{
print(error)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
responseMsg(nil,error?.localizedDescription)
return
}
guard let data = data else {
responseMsg(nil,"Something went wrong")
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
responseMsg(json,nil)
// print(json)
// handle json...
}
} catch let error {
responseMsg(nil,error.localizedDescription)
// print(error.localizedDescription)
}
})
task.resume()
}}
BASEWEBSERVICE_ClASS:
// where RTCommonResponse is common response Model class. generated by ahmad ali json exporter tool.
import UIKit
import KRProgressHUD
import Foundation
import SystemConfiguration
protocol BaseWebserviceDelegate {
func sucessful(data : RTCommonResponse)
func failedWithMessage(message : String)
}
class BaseWebservice: NSObject
{
var webData : NSMutableData!
var statusCode : Bool!
var appDelegate : AppDelegate!
var baseDelegate : BaseWebserviceDelegate!
let opQueue = OperationQueue()
func initWithDelegate(aDelegate : BaseWebserviceDelegate) {
baseDelegate = aDelegate
appDelegate = UIApplication.shared.delegate as! AppDelegate
//return self
}
func callWithBaseRequest(requestData : Dictionary<String, Any>, aURL : String, method : String, showHUD : Bool ) {
let hudShow = showHUD && BaseWebservice.isInternetAvailable()
let url4 = URL(string: aURL as String)!
//let session4 = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: self.opQueue)
//let session4 = URLSession.shared
let session4: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 9999
configuration.timeoutIntervalForResource = 9999
return URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
}()
let request = NSMutableURLRequest(url: url4)
request.httpMethod = method
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
//let paramString = "data=Hello"
//request.httpBody = paramString.data(using: String.Encoding.utf8)
if method == Constants.POST {
let cookieHeader = (requestData.flatMap({ (key, value) -> String in
return "\(key)=\(value)"
}) as Array).joined(separator: "&")
let postData : Data = cookieHeader.data(using: .utf8)!
request.httpBody = postData
AppDelegate.printLog(log: "Request started for url : " + aURL + " Peram : " + cookieHeader)
}
//request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
let task = session4.dataTask(with: request as URLRequest) { (data, response, error) in
guard let _: Data = data, let _: URLResponse = response, error == nil else {
print("*****error " + (error?.localizedDescription)!)
DispatchQueue.main.async {
KRProgressHUD.dismiss()
self.baseDelegate.failedWithMessage(message: (error?.localizedDescription)!)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
return
}
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
// print("*****This is the data 4: \(String(describing: dataString))") //JSONSerialization
let dictionary: Dictionary? = self.convertToDictionary(text: dataString! as String)
//let content = Content.init(fromDictionary: dictionary!)
AppDelegate.printLog(log:"Responce recived for url : " + aURL)
if dictionary == nil {
DispatchQueue.main.async {
KRProgressHUD.dismiss()
self.baseDelegate.failedWithMessage(message: "Something went wrong, try after some time")
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
return
}
//self.baseDelegate.sucessful(data: commonResponse)
DispatchQueue.main.async {
let commonResponse : RTCommonResponse = RTCommonResponse.init(fromDictionary: dictionary!)
if commonResponse.status == "ok" {
self.baseDelegate.sucessful(data: commonResponse)
KRProgressHUD.dismiss()
}else{
let error : RTError = RTError.init(fromDictionary: commonResponse.data!)
KRProgressHUD.dismiss()
self.baseDelegate.failedWithMessage(message: error.error)
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
//let task = session4.dataTask(with: request as URLRequest)
if hudShow {
DispatchQueue.main.async {
KRProgressHUD.show()
}
}
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
// session4
task.resume()
}
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
static func isInternetAvailable() -> Bool
{
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
}
ForgotpasswordWS_Class:
// where RTForgotPasswordResponse is common response Model class. generated by ahamad ali json exporter tool.
import UIKit
protocol ForgotPasswordWebserviceDelegate {
func forgotPasswordSucessful(data : RTForgotPasswordResponse)
func forgotPasswordFailedWithMessage(message : String)
}
class ForgotPasswordWebservice: BaseWebservice {
var delegate : ForgotPasswordWebserviceDelegate!
init(aDelegate : ForgotPasswordWebserviceDelegate) {
super.init()
super.initWithDelegate(aDelegate: self as BaseWebserviceDelegate)
delegate = aDelegate
}
func initWithDelegate(aDelegate : ForgotPasswordWebserviceDelegate) {
super.initWithDelegate(aDelegate: self as BaseWebserviceDelegate)
delegate = aDelegate
//return self
}
func call(request : RTForgotPasswordRequest) {
let url = Constants.BASE_URL + "user/retrieve_password/"
self.callWithBaseRequest(requestData: request.toDictionary() , aURL: url, method: Constants.POST, showHUD: true)
}
}
extension ForgotPasswordWebservice : BaseWebserviceDelegate{
func sucessful(data : RTCommonResponse){
let content = RTForgotPasswordResponse.init(fromDictionary: data.data! )
//data.content = content
self.delegate.forgotPasswordSucessful(data: content)
}
func failedWithMessage(message : String){
self.delegate.forgotPasswordFailedWithMessage(message: message)
}
}
YourVIEWCONTROLLER_Class:
// just add extension to your view controller
extension yourViewController : ForgotPasswordWebserviceDelegate {
func forgotPasswordFailedWithMessage(message: String) {
let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline:when){
self.showSnackBar(message: message)
}
}
func forgotPasswordSucessful(data: RTForgotPasswordResponse) {
let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline:when) {
self.showSnackBar(message: data.msg)
//self.displayAlertMessage(messageToDisplay: "Reset Password Link Send")
}
}
}
// when you call API you have to write JUST line of CODE That's IT!!!
#IBAction func forgotbtntapped(_ sender: Any) {
let forgotws : ForgotPasswordWebservice = ForgotPasswordWebservice.init(aDelegate: self)
let forgotrequest : RTForgotPasswordRequest = RTForgotPasswordRequest();
forgotrequest.userLogin = forgotEmailTextField.text!
forgotws.call(request: forgotrequest)
}
Another simple way to call web API
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CCell = self.tblV.dequeueReusableCell(withIdentifier: "cell") as! CCell
cell.lblName?.text = "ABCD"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func getClassList(completion: ((NSArray?, NSError?) -> Void)?) {
let myUrl = URL(string: "http://papili.us/studycentral/api/getClassList.php");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
print("error=\(String(describing: error))")
completion?(nil, error as NSError?)
return
}
print("response = \(String(describing: response))")
//Convert response sent from a server side script to a NSDictionary object:
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if json != nil {
let newdata : NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
let datalist : KRootClass = KRootClass.init(fromDictionary: newdata as! [String : Any])
print(datalist.classID)
print(datalist.className)
print(datalist.teacherTitle)
print(datalist.teacherLastName)
print(datalist.teacherFirstName)
}
} catch {
print(error)
completion?(nil, error as NSError)
}
}
task.resume()
}

UITableView crashes at start at tableview.datasource

I have implemented two UITableview in seperate ViewController classes. After successfully adding the code for the second UItableView my project builds but stops after loading.
It stops with a red line on:
tableView.delegate.dataSource = self
With an error that reads:
Thread 1 EXEC_BAD_INSTRUCTION (code-EXC_i386_INVOP,subcode=0x0)
AND
fatal error: unexpectedly found nil while unwrapping an Optional value
Since this didn't happen until adding the second UItableview in the second view controller I'm wondering if maybe my code is conflicting with each other. I will post copies of both view controllers. What should I change?
By the way: FilmsViewController is where the app is crashing. Although it was working prior to adding MCViewController Films:
B:
class FilmsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
weak var tableView : UITableView!
var FilmArray = [String]()
let film_url = "https://www.testing.com/api/resources/films/1"
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FilmArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for:indexPath) as! FilmsAPITableViewCell
// Configuring Cell
cell.movieTitle.text = FilmArray[indexPath.row]
// Returning the cell
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let url:URL = URL(string: film_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "GET"
request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let paramString = ""
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
var json:Any?
do
{
if let existingData = data {
json = try JSONSerialization.jsonObject(with: existingData, options: [])
}
// Prasing JSON
if let parsedData = json as? [[String:Any]] {
for dict in parsedData {
if let title = dict["title"] as? String {
self.FilmArray.append(title)
print(json)
}
}
OperationQueue.main.addOperation({
self.tableView.reloadData()
})
}
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
// self.login_session = session_data
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
// DispatchQueue.main.async(execute: self.LoginDone)
}
}
})
task.resume()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
MC:
class MCViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let message_url = "https://www.testing.com/api/resources/get_film_message/film_id/3825"
let send_url = "https://www.testing.com/api/resources/send_film_message"
let film_id = "3825"
var messageArray = [String]()
weak var tableView : UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "msgContent", for:indexPath) as! MessageTableViewCell
// Configuring Cell
cell.msgContent.text = messageArray[indexPath.row]
// Returning the cell
return cell
}
#IBOutlet weak var MessageInput: UITextField!
#IBAction func Sendmsg(_ sender: Any) {
Sendmsg(username:MessageInput.text!, password: film_id)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view.
//let post_data: NSDictionary = NSMutableDictionary()
// post_data.setValue(username, forKey: "username")
// post_data.setValue(password, forKey: "password")
let url:URL = URL(string: message_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "GET"
request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
// Do any additional setup after loading the view.
var paramString = ""
// for (key, value) in post_data
// {
// paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
// }
//
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
if let parsedData = json as? [[String:Any]] {
for dict in parsedData {
if let title = dict["message"] as? String {
self.messageArray.append(title)
print(json)
}
}
}
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
// self.login_session = session_data
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
// DispatchQueue.main.async(execute: self.LoginDone)
}
}
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Sendmsg(username:String, password:String)
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(username, forKey: "message")
post_data.setValue(password, forKey: "film_id")
let url:URL = URL(string: send_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
// if let data_block = server_response["data"] as? NSDictionary
// {
// if let session_data = data_block["session"] as? String
// {
// self.login_session = session_data
//
// let preferences = UserDefaults.standard
// preferences.set(session_data, forKey: "session")
//
// DispatchQueue.main.async(execute: self.LoginDone)
// }
// }
//
})
task.resume()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Where are you initializing tableView? Seems like it might be nil....
tableView = UITableView(frame: frame)
If you're using storyboard/xib, you can also hook tableView up as an IBOutlet.

How to populate TableView with jSON in Swift 3

Sorry guys it is lil old me again. I am reaching out as I need help with a second tableView I would like to implement in a another view controller. I have copied this code basically from another tableView controller which actually works. So I'm not sure where I messed up here but I would really appreciate the help.
class MCViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let message_url = "https://www.distribber.com/api/resources/get_film_message/film_id/3825"
let send_url = "https://www.distribber.com/api/resources/send_film_message"
let film_id = "3825"
var messageArray = [String]()
weak var messageView : UITableView!
func messageView(_ messageView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}
func messageView(_ messageView: UITableView, cellForRowAt indexPath: IndexPath) -> MessageTableViewCell {
let cell = messageView.dequeueReusableCell(withIdentifier: "msgContent", for:indexPath) as! MessageTableViewCell
// Configuring Cell
cell.msgContent.text = messageArray[indexPath.row]
// Returning the cell
return cell
}
#IBOutlet weak var MessageInput: UITextField!
#IBAction func Sendmsg(_ sender: Any) {
Sendmsg(username:MessageInput.text!, password: film_id)
}
override func viewDidLoad() {
super.viewDidLoad()
messageView.dataSource = self
messageView.delegate = self
// Do any additional setup after loading the view.
//let post_data: NSDictionary = NSMutableDictionary()
// post_data.setValue(username, forKey: "username")
// post_data.setValue(password, forKey: "password")
let url:URL = URL(string: message_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "GET"
request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
// Do any additional setup after loading the view.
var paramString = ""
// for (key, value) in post_data
// {
// paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
// }
//
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
if let parsedData = json as? [[String:Any]] {
for dict in parsedData {
if let title = dict["message"] as? String {
self.messageArray.append(title)
print(json)
}
}
OperationQueue.main.addOperation({
self.messageView.reloadData()
})
}
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
// self.login_session = session_data
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
// DispatchQueue.main.async(execute: self.LoginDone)
}
}
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Sendmsg(username:String, password:String)
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(username, forKey: "message")
post_data.setValue(password, forKey: "film_id")
let url:URL = URL(string: send_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
// if let data_block = server_response["data"] as? NSDictionary
// {
// if let session_data = data_block["session"] as? String
// {
// self.login_session = session_data
//
// let preferences = UserDefaults.standard
// preferences.set(session_data, forKey: "session")
//
// DispatchQueue.main.async(execute: self.LoginDone)
// }
// }
//
})
task.resume()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
You're implementing UITableViewDataSource but not implementing it's required methods.
func tableView(UITableView, cellForRowAt: IndexPath)
func numberOfSections(in: UITableView)
Implement those two and the not conforming to protocol error should go away.

Resources