So there aren't many resources online for this problem I'm having. All of the tutorials I find involving external JSON/Databases end with putting the data into a table view, which makes the coding a bit different than what I am trying to do. I'm connecting with my Database just fine, but actually using the data isn't working because it's set up for a table view with a delegate for the data, so I assume the data from my Database is being parsed correctly and all of that, but I don't know how to output the data because I obviously can't tie UILabels to the ViewController delegate, etc. At least I think that's what's going on.
I have a Database that I set up, and I'm converting the data to JSON using PHP. Figuring out how to even set up the Database was an issue because I had never done it before, but here we are. The JSON looks like this:
I've already poured through documentation and various tutorials. They aren't much help. The examples they're using are a lot more complicated than what I'm trying to do. Most of the stuff that uses Obj-C is for older versions of iOS with depreciated methods and such. I had to change my info.plist to Allow Arbitrary Loads because the certificates aren't set up correctly on my server. I'm just trying to figure out the logistics of what I'm missing, and then I can work on "updating" the code to more current methods.
I got up to the Table View part of a tutorial and this is what I have:
I'm getting all of the proper logs except for the Dictionary values. I'm connecting, and loading the data just fine. xCode is telling me the expression result is unused from my [[NSURLConnection alloc] initWithRequest:request delegate:self]; bit. I understand that once your TableView is set up, you set the delegate and whatnot, so I'm assuming that is part of the problem.
How do I adapt this to use for UILabel as opposed to Table View?
I have a Database that I set up, and I'm converting the data to JSON using PHP. Figuring out how to even set up the Database was an issue because I had never done it before, but here we are. The JSON looks like this:
Code:
[{"id":"0","saleDate":"February 30th - February 31st","saleAddress1":"3333 Butterball Rd.","saleAddress2":"Muskegon, MI 53443"}]
I've already poured through documentation and various tutorials. They aren't much help. The examples they're using are a lot more complicated than what I'm trying to do. Most of the stuff that uses Obj-C is for older versions of iOS with depreciated methods and such. I had to change my info.plist to Allow Arbitrary Loads because the certificates aren't set up correctly on my server. I'm just trying to figure out the logistics of what I'm missing, and then I can work on "updating" the code to more current methods.
I got up to the Table View part of a tutorial and this is what I have:
Code:
@interface ViewController : UIViewController {
NSDictionary *saleDictionary;
IBOutlet UIImageView *imageOutlet2;
IBOutlet UILabel *saleDate;
IBOutlet UILabel *add1;
IBOutlet UILabel *add2;
NSArray *sale;
NSMutableData *data;
}
@property (nonatomic, copy) NSDictionary *saleDictionary;
Code:
@interface ViewController ()
@end
@implementation ViewController
@synthesize saleDictionary;
- (void)viewDidLoad {
imageOutlet2.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://upload.wikimedia.org/wikipedia/commons/4/49/Koala_climbing_tree.jpg"]]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://www.striatecortex.com/eesales/json/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
saleDate.text = [NSString stringWithFormat:@"%@",[saleDictionary objectForKey:@"saleDate"]];
add1.text = [NSString stringWithFormat:@"%@",[saleDictionary objectForKey:@"saleAddress1"]];
add2.text = [NSString stringWithFormat:@"%@",[saleDictionary objectForKey:@"saleAddress2"]];
NSLog(@"objectForKey : 1--- %@",[saleDictionary objectForKey:@"saleDate"]);
NSLog(@"objectForKey : 2--- %@",[saleDictionary objectForKey:@"saleAddress1"]);
NSLog(@"objectForKey : 3--- %@",[saleDictionary objectForKey:@"saleAddress2"]);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
data = [[NSMutableData alloc] init];
NSLog(@"didRespond");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[data appendData:theData];
NSLog(@"didData");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
sale = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"didLoad");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sports!"
message:@"You're connected!"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
NSLog(@"didFail");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Oops!"
message:@"You ****ed up!"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
I'm getting all of the proper logs except for the Dictionary values. I'm connecting, and loading the data just fine. xCode is telling me the expression result is unused from my [[NSURLConnection alloc] initWithRequest:request delegate:self]; bit. I understand that once your TableView is set up, you set the delegate and whatnot, so I'm assuming that is part of the problem.
How do I adapt this to use for UILabel as opposed to Table View?