This is my first API that I am calling from a demo iOS app. I just want to get the simple sentence I wrote in the php back to a label.
However the dictionary result is null when I get it in the app. How does this work? ***Corrected, code below works but not very secure
PHP file:
index.php hosted on localhost using XAMPP (http://localhost/tutorials/index.php) if I echo "hi"; it shows on the page so the server is working.
<?php
if(function_exists($_GET['fe'])) {
$_GET['fe']();
}
function getLabel(){
$response['name']="Hello first API ever";
echo json_encode($response);
}
Now in Xcode I have:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//Main parse
//Web data
NSDictionary*data=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSLog(@"%@",data);
NSString*label=[data valueForKey:@"name"];
_answer.text=label;
}
-(void)getData{
NSURL*url=[NSURL URLWithString:@"http://localhost/tutorials/index.php?fe=getLabel"];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
webData=[[NSMutableData alloc]init];
}
}
//Clear response
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[webData setLength:0];
}
//Append data
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//Main parse
//Web data
NSDictionary*data=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSLog(@"%@",data);
NSString*label=[data valueForKey:@"name"];
_answer.text=label;
}
Thanks