I am making this chat app with works with a .php file on my webserver to store the data in a SQL db
Here is my code so far:
#import "vtchatViewController.h"
@interface vtchatViewController ()
@end
@implementation vtchatViewController
@synthesize messageBox, messageView, Send, Refresh;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *usrName = [[NSUserDefaults standardUserDefaults] stringForKey:@"user_name"];
NSURL *url = [NSURL URLWithString:@"http://server.com/ios/messages.php"]; [messageView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (IBAction)refreshButton:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://server.com/ios/messages.php"]; [messageView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (IBAction)sendButton:(id)sender {
// server.com/ios/add.php?user=Username here&message=Message Her
[messageBox resignFirstResponder];
NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=",usrName "&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];
[messageView loadRequest:[NSURLRequest requestWithURL:add]];
messageBox.Text = @"";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
However on the line “viewDidLoad” I can’t get it to use the username from my settings bundle:
NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=",usrName "&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];
I get the error “Use of undeclared indentifier ‘usrName’ and also I have no idea if I did implent the “usrName” correct for use in this
I simply need it to send data to “server.com/ios/add.php?” where data should be in this format: “user=Username here&message=Message here”
I got it working if I just use a fixed username, but as said, I have no idea how to make the thing use the username stored in my settings file
Thanks in advance
You need to use string format tokens to get it to insert the username into the
urlStringvariable like this:That should help you understand why your format string wasn’t working and why you were getting that error. My code above isn’t escaping the values of
usrNameormessageBox.textso you’ll need to add those back into your code.