how to get back from whatsapp to my app in iOS7 - ios7

I have heard about URL-schemes but I cannot return back to my app after Whatsapp opens.
I referred this link http://www.whatsapp.com/faq/en/iphone/23559013
Here is code snippet which I copied into my app:
NSString * msg = #"YOUR MSG";
NSString * urlWhats = [NSString stringWithFormat:#"whatsapp://send?text=%#",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"WhatsApp installed." message:#"Your device has WhatsApp installed." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}

Related

Terminating app due to uncaught exception 'NSInvalidArgumentException' 5

i am getting this error while running my app:
my code is:
- (IBAction)yes:(id)sender {
UIAlertView *msgbox=[[UIAlertView alloc]initWithTitle:#"Warrning" message:nil delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
if ([_usernametextbox.text length]>0)
{
if ([_passwordtextfield.text length]>0)
{
NSMutableString *ms=[[NSMutableString alloc]initWithFormat:url,_usernametextbox.text,_passwordtextfield];
NSURL *serviceurl=[[NSURL alloc] initWithString:ms];
NSError *error=nil;
NSData *data=[NSData dataWithContentsOfURL:serviceurl options:NSDataReadingUncached error:&error];
NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
NSString *jsondata=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
SBJSON *parsedata=[[SBJSON alloc] init];
dic=[parsedata objectWithString:jsondata error:nil];
if([dic count]>0)
{
NSMutableString *st=[[NSMutableString alloc]initWithString:[dic objectForKey:#"loginResult:#""+"]]; //#" " withString:#"+"
if ([st isEqualToString:#"YES"])
{
UIAlertView * alert=[[UIAlertView alloc] initWithTitle:#"accept" message:#"correct username" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView * alert=[[UIAlertView alloc] initWithTitle:#"Error" message:#"Invalid UserName and Password" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
else
{
msgbox.message=#"Please Enter Password...";
[msgbox show];
}
}
else
{
msgbox.message=#"Please Enter Username...";
[msgbox show];
}
I get the error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[NSConcreteData
initWithContentsOfURL:options:error:]: nil URL argument'
on the line:
NSMutableString *st=[[NSMutableString alloc]initWithString:[dic objectForKey:#"loginResult:#""+"]]; //#" " withString:#"+" if ([st isEqualToString:#"YES"])

Why does isEqualToString not work for NSString?

I am trying to code the login process for an iPhone app in XCode. The problem is with the NSString serverOutput below. When I print it using printf(serverOutput.UTF8String); it prints 'Yes' to the console. However when I compare serverOutput to "Yes" it doesn't work. Any help would be appreciated. Here's my code:
- (IBAction) loginButton: (id) sender
{
// TODO: spawn a login thread
indicator.hidden = FALSE;
[indicator startAnimating];
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",userName.text, password.text];
NSString *hostStr = #"http://10.243.1.184/connectDB.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
printf(serverOutput.UTF8String);
if([serverOutput isEqualToString:#"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Congrats" message:#"You are authorized"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Username or Password Incorrect"
delegate:self cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
//[self.navigationController pushViewController:DashboardViewController animated:YES];
loginbutton.enabled = TRUE;
}
loginbutton.enabled = FALSE;
}
Based on helping others with similar situations I would say the problem is that the response from the server isn't just the string "Yes". Most likely there is some whitespace before and/or after the text. Perhaps a stray newline or two.
Try this:
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(#"Result = '%#'", serverOutput); // look for space between quotes
serverOutput = [serverOutput stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

encoding U00d8\U00aa\ NSString

I'm receiving remote notification with string
\U00d8\U00aa\U00d8\U00aa\U00d8\U00a7\U00d8\U00a7"
I would like to display this string in alert dialog.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Alert"
message:/*alertValue*/decodedString
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
but it oesn't shown correctly, what i need to do to encode it correctly?
I figure it out:
NSString *decodedString = [NSString stringWithUTF8String:[alertValue cStringUsingEncoding:NSUnicodeStringEncoding]];

Pull more than 1 echo from PHP

OK, I have a login screen that sends username and password to a PHP page. The PHP page echo's Yes or No based on the login status, but I would like it to echo the user id from the database for further use. Do I have to use JSON for this? I dont know how to retrieve more than one echo's. Currently the code is:
- (IBAction) login: (id) sender
{
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",usernameField.text, passwordField.text];
NSString *hostStr = #"https://www.mysite.com/login.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:#"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Business Manager" message:#"Login Successful"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
RootViewController *viewMenu = [[RootViewController alloc]
initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
self.rootViewController = viewMenu;
[viewMenu release];
[self.navigationController pushViewController:self.rootViewController animated:YES];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Username or Password Incorrect"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
}
Now I just want to pull one more echo called "ID". Forming JSON for this seems arbitrary.
Technically you can use whatever response format you want -- though it's best to stick with something standard as you'll be able to find libraries made for you already. A REST service with a JSON response is a pretty easy way to facilitate communication between a server and client, so it would probably be a good idea to start there.

Authenticating Iphone Login with PHP/MySQL and HTTP Responses

Im trying to create a iPhone Objective C login page using PHP/MySQL to authenticate. Im trying to use HTTP responses as a way of authenticating. Right now it always fails to login , regardless of what you enter.
`- (IBAction) login: (id) sender
{
// this part isnt really implemented yet!
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",usernameField.text, passwordField.text];
NSURL *url = [NSURL URLWithString:#"MY URL HERE.php"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)request;
responseStatusCode = [httpResponse statusCode];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:response error:nil];
if ([responseStatusCode statusCode] == 200) {
//do something
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"success" message:#"Your have logged in" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertsuccess show];
[alertsuccess release];
}
else {
NSLog(#"Login failed.");
//do something else
UIAlertView *alertfail = [[UIAlertView alloc] initWithTitle:#"fail" message:#"login fail" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertfail show];
[alertfail release];
}
`
and the PHP looks like
<?
session_start();
require("iphoneconnection.php");
$u = $_POST['username'];
$pw = $_POST['password'];
$check = "SELECT username, password FROM iphoneusers WHERE username='$u' AND password= '$pw'";
$login = mysql_query($check, $connect) or die(mysql_error());
// check user level and store in $row
if (mysql_num_rows($login) == 1) {
$row = mysql_fetch_assoc($login);
//$_SESSION['level'] = $row['level'];
$_SESSION['username'] = $u;
echo 'login success';
header("HTTP/1.1 200 OK");
//header("Location: index.php");
} else {
//header("Location: login.php");
//echo '403 Forbidden';
header("403 Forbidden");
}
mysql_close($connect);
?>
Anyone got any pointers? Even if its just to tell me if Im formatting my repsonses correctly in the PHP.
Thanks in advance.
Thanks for the response. I got it to work using:
- (IBAction) login: (id) sender
{
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",usernameField.text, passwordField.text];
NSString *hostStr = #"MY URL.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:#"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"success" message:#"You are authorized"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertsuccess show];
[alertsuccess release];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:#"Fail" message:#"Invalid Access"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alertsuccess show];
[alertsuccess release];
}
loginIndicator.hidden = FALSE;
[loginIndicator startAnimating];
loginButton.enabled = FALSE;
}
According to the sample you've given, I don't see that you're actually sending the username & password with the request. You're creating a string "post", but never including it in the request.
Also, this cast:
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)request;
doesn't look right. Why are you typecasting the request object to a response?