Reputation: 412
Hi I have given a link
"https://joysalescript.com/api/service?ws=1"
and they need to get the data from that link
I don't know how to achieve that I new to this programming can anyone help me.thanks
Upvotes: 0
Views: 98
Reputation: 1
You can also use SBJson Parser to call Api
-(void)resetCode
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *id = [prefs stringForKey:@"ID"];
NSString *post =[[NSString alloc] initWithFormat:@"newpassword=%@&confirmpassword=%@&id=%@",[_passwordTextField text],[_confirmPasswordTextfield text],id];
NSLog(@"PostData: %@",post);
NSURL * url = [NSURL URLWithString:@"https://joysalescript.com/api/service?ws=1"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
// NSError *error = [[NSError alloc] init];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger status = [(NSNumber *) [jsonData objectForKey:@"status"] integerValue];
NSLog(@"%ld",(long)status);
if(status == 1)
{
forgotPasswordViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
[self presentViewController:secondVC animated:YES completion:nil];
}
else
{
NSLog(@"yash");
}
}
else {
if (error) NSLog(@"Error: %@", error);
[self alertFailed:@"Login Failed!" :@"The network connection appears to be offline."];
[hud hideAnimated:YES];
}
}
Upvotes: 0
Reputation: 2082
Check this link in Postman. It is not showing response. My suggestion is use Alamofire for API call.
Upvotes: 0
Reputation: 636
**Simply Call and get yourData.**
-(void)getData
{
NSURL *url = [NSURL URLWithString:@"https://joysalescript.com/api/service?ws=1"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSError *erro = nil;
if (data!=nil) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];
//if data return type is json supposed
if (json.count > 0) {
for(int i = 0; i<10 ; i++){
[arr addObject:[[[json[@"feed"][@"entry"] objectAtIndex:i]valueForKeyPath:@"im:image"] objectAtIndex:0][@"label"]];
}
}
}
dispatch_sync(dispatch_get_main_queue(),^{
[table reloadData];
});
}];
[data resume];
}
Upvotes: 1