0

Ok I have been trying to figure this out for about three weeks and I don't think I will need to shave my head again!!! I have deconstructed the code as much as I possibly can, so I can focus on just getting the device token to my database. When I run the code I get a time stamp record in my database and that is it, so I know it is connected but nothing is there.

Here is my code

- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

NSString *tokenStr = [deviceToken description];
NSString *pushToken = [[[[tokenStr 
                          stringByReplacingOccurrencesOfString:@"<" withString:@""] 
                         stringByReplacingOccurrencesOfString:@">" withString:@""] 
                        stringByReplacingOccurrencesOfString:@" " withString:@""] retain];
// Save the token to server

NSString *urlStr = [NSString stringWithFormat:ServerApiURL];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

NSMutableData *postBody = [NSMutableData data];

[postBody appendData:[[NSString stringWithFormat:@"&token=%@", 
                       pushToken] dataUsingEncoding:NSUTF8StringEncoding]];

[req setHTTPBody:postBody];
[[NSURLConnection alloc] initWithRequest:req delegate:nil];
NSLog(@"%@", pushToken);
NSLog(@"%@", url);

}

Here is the very simple PHP

// initialize connection to database
$db = mysql_connect('localhost', 'xxxxxxxxx', 'xxxxxxxx');
$db_name = "xxxxxxxxxxxx";
mysql_select_db($db_name, $db);

// store incoming data as php variables
error_log(print_r($_POST, true));
$token = $_POST['token'];


// create mysql query

mysql_query("INSERT INTO iPhone (device_token)
VALUES ('$token')");
mysql_query($query);

mysql_close($db);
?>

and what i am getting in my database is the time stamp but nothing under the device_token. I would appreciate any and all help. Thanks.

Mike Owens
  • 936
  • 3
  • 8
  • 20

1 Answers1

0

Does NSLog(@"%@", pushToken); have the valid token you want to submit?

Also, you could try dumping the $_POST on the PHP page to see what it's receiving.

On another note, be sure to sanitize the input on the PHP page before inserting it into your database using mysqlrealescapestring(); or consider using PDO as the use of the old mysql functions is now discouraged. PDO also automatically sanitizes the input.

Here's more info on PDO:

http://us2.php.net/manual/en/ref.pdo-mysql.php

http://us2.php.net/manual/en/pdo.query.php

Edit:

This may be the cause of the problem. Replace

NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:@"&token=%@", pushToken] dataUsingEncoding:NSUTF8StringEncoding]];

with

NSData *postBody = [[NSString stringWithFormat:@"&token=%@", pushToken] dataUsingEncoding:NSUTF8StringEncoding];

The only difference is NSData instead of NSMutableData and a more direct way of setting it. I'm not sure if this is the cause of the problem, but it couldn't hurt to try.

Steffan Long
  • 337
  • 2
  • 12
  • Thanks for the reply. I should have said earlier that the NSLog call does return the correct token. I just seem to have a problem getting where I want it to go. I will go back to the PDO and try dumping the $_POST and see what happens. – Mike Owens Jul 13 '12 at 17:19
  • when I use var_dump it returns NULL. Any ideas? – Mike Owens Jul 14 '12 at 22:41
  • Sorry I should have made this more clear. Since you're POSTing from the app, var_dump is practically useless unless you're going to use the app to display the output. I'd recommend using print_r (and it's extra parameter) to dump the postvars into a file. Then use the app like you would normally and see what that file contains. I'm assuming you got NULL trying to run the PHP script from your web browser? If you got NULL from the app, then there has to be a problem with the postvars somewhere. – Steffan Long Jul 29 '12 at 04:16