Last active
May 3, 2018 08:39
-
-
Save randhirraj3130/0832b366b083e2d3a1400603510a112c to your computer and use it in GitHub Desktop.
webservice to hit the url and the result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// WebService.swift | |
// ArtCaller | |
// | |
// Created by Twistfuture on 6/9/15. | |
// Copyright (c) 2015 Twist. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
@objc protocol WebServiceDelegate { | |
@objc optional func CompleteDownloading ( _ service :WebService ,result : AnyObject) | |
} | |
class WebService : NSObject { | |
var delegate : WebServiceDelegate? | |
var View : UIView = UIView() | |
var Process : UIActivityIndicatorView = UIActivityIndicatorView() | |
var task : URLSessionDataTask! | |
func Start(VC: UIViewController, dic : [String:String]?,url : String, onCompletion : @escaping (AnyObject) -> Void) { | |
// print(dic) | |
let win:UIWindow = UIApplication.shared.delegate!.window!! | |
let urlString = "\(url)" | |
print(urlString) | |
let url = Foundation.URL(string : urlString ) | |
let request = NSMutableURLRequest(url : url!) | |
request.httpMethod="POST" | |
let boundary = NSString(format: "---------------------------147378355409831466499882746641449") | |
let contentType = NSString(format: "multipart/form-data; boundary=%@",boundary) | |
request.addValue(contentType as String, forHTTPHeaderField: "Content-Type") | |
let body = NSMutableData() | |
self.View.frame = CGRect(x: 0, y: 0, width: 50, height: 50) | |
self.View.backgroundColor = UIColor.gray | |
self.View.center = win.center | |
self.View.layer.cornerRadius = 5.0 | |
self.View.clipsToBounds = true | |
self.Process.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white | |
self.Process.center = CGPoint(x: 25, y: 25) | |
self.View.addSubview(self.Process) | |
self.Process.startAnimating() | |
VC.view.addSubview(self.View) | |
if dic != nil { | |
print(dic!) | |
for (key, text) in dic! { | |
print("Sending ----\(key)-----\(text)") | |
body.append(NSString(format: "\r\n--%@\r\n",boundary).data(using: String.Encoding.utf8.rawValue)!) | |
body.append(NSString(format:"Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n" as NSString).data(using: String.Encoding.utf8.rawValue)!) | |
body.append(text.data(using: String.Encoding.utf8, allowLossyConversion: true)!) | |
} | |
} | |
request.httpBody = body as Data | |
task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { | |
data, response, error in | |
DispatchQueue.main.async(execute: { | |
self.View.removeFromSuperview() | |
if error != nil | |
{ | |
let alert = UIAlertController(title: "Error", message: "Server is not responding properly.", preferredStyle: .alert) | |
let cancel = UIAlertAction(title: "Retry", style: .cancel, handler: { action in | |
self.task.resume() | |
}) | |
alert.addAction(cancel) | |
VC.present(alert, animated: true, completion: nil) | |
}else{ | |
let srt = NSString(data: data!, encoding: String.Encoding.ascii.rawValue) | |
print("Service : \(url) Data: \(srt)") | |
do { | |
let parsedObject = try JSONSerialization.jsonObject(with: data!, options: []) | |
if let object = parsedObject as? NSDictionary{ | |
print("Result is a dictionary"); | |
onCompletion(object) | |
}else{ | |
print("Result is an array"); | |
} | |
} catch { | |
print("json error: \(error)") | |
} | |
} }) | |
}) | |
if(Controller.isConnectedToNetwork()){ | |
print("internet connectivity found ") | |
task.resume() | |
}else{ | |
self.View.removeFromSuperview() | |
print("internet connectivity not found ") | |
let alert = UIAlertController(title: "Error", message: "internet connectivity not found", preferredStyle: .alert) | |
let cancel = UIAlertAction(title: "Retry", style: .cancel, handler: { action in | |
self.task.resume() | |
}) | |
alert.addAction(cancel) | |
VC.present(alert, animated: true, completion: nil) | |
} | |
} | |
} | |
var Service : WebService = WebService() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment