JSON POSTDATA...
@property NSMutableURLRequest *loginURLRequest;
@property NSURLSessionConfiguration *loginURLSessionConfg;
@property NSURLSession *loginURLSession;
@property NSURLSessionDataTask *loginDataTask;
- (IBAction)loginTapped:(id)sender {
self.loginURLRequest=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://www.brninfotech.com/pulse/modules/admin/ValidateLogin.php"]];
[self.loginURLRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
self.loginURLRequest.HTTPMethod=@"POST";
NSString * post=[[NSString alloc] initWithFormat:@"registeredEmail=%@®isteredPassword=%@&funcName=%@",self.email.text,self.password.text,@"verifyLogin"];
NSData * postData=[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString * postLength=[NSString stringWithFormat:@" %lu ",(unsigned long)[postData length]];
[self.loginURLRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[self.loginURLRequest setHTTPBody:postData];
self.loginURLSessionConfg=[NSURLSessionConfiguration defaultSessionConfiguration];
self.loginURLSession=[NSURLSession sessionWithConfiguration:self.loginURLSessionConfg];
self.loginDataTask=[self.loginURLSession dataTaskWithRequest:self.loginURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[AppDelegate SharedAppDelegateImplementation].profileDictionary=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"got response from server %@",[AppDelegate SharedAppDelegateImplementation].profileDictionary);
[self.loginDataTask resume];
}
ReplyDeleteimport UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UsersPostServiceCall(url: "http://testingmadesimple.org/training_app/api/Service/songSearch", userId: "5cc0071e06f2a3151dfa115e", searchText: "m")
}
func UsersPostServiceCall(url : String , userId : String , searchText : String){
let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
request.httpMethod = "POST"
let postString = "userId=\(userId)&search=\(searchText)"
print(postString)
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
do {
if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject]{
print(responseJSON)
DispatchQueue.main.async {
}
if(responseJSON["status"] as! Int == 0)
{
DispatchQueue.main.async {
}
}
else if(responseJSON["status"] as! Int == 1)
{
DispatchQueue.main.async {
}
}
else{
DispatchQueue.main.async {
}
}
}
}
catch {
DispatchQueue.main.async {
}
print("Error -> \(error)")
}
}
task.resume()
}
}
https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0
ReplyDelete