【问题标题】:What am I doing wrong? Trying to store device token我究竟做错了什么?尝试存储设备令牌
【发布时间】:2012-07-13 08:26:51
【问题描述】:

好的,我已经尝试解决这个问题大约三周了,我认为我不需要再剃光头了!!!我已尽可能多地解构代码,因此我可以专注于将设备令牌获取到我的数据库中。当我运行代码时,我会在我的数据库中得到一个时间戳记录,就是这样,所以我知道它已连接但什么都没有。

这是我的代码

- (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);

}

这是一个非常简单的 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);
?>

我在我的数据库中得到的是时间戳,但在 device_token 下没有任何内容。我将不胜感激。谢谢。

【问题讨论】:

  • $_POST['token'] 上使用var_dump 并查看其中的内容(很可能是NULL 或空字符串'')。数据库很可能不是问题。
  • 当我使用 var_dump 它确实返回 NULL。这是什么原因造成的?

标签: php iphone ios database devicetoken


【解决方案1】:

NSLog(@"%@", pushToken); 是否有您要提交的有效令牌?

另外,您可以尝试在 PHP 页面上转储 $_POST 以查看它收到的内容。

另一方面,请务必在使用 mysqlrealescapestring(); 将 PHP 页面上的输入插入数据库之前对其进行清理,或者考虑使用 PDO,因为现在不鼓励使用旧的 mysql 函数。 PDO 还会自动清理输入。

这里有更多关于 PDO 的信息:

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

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

编辑:

这可能是问题的原因。替换

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

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

唯一的区别是 NSData 而不是 NSMutableData 和更直接的设置方式。我不确定这是否是问题的原因,但尝试一下也无妨。

【讨论】:

  • 感谢您的回复。我之前应该说 NSLog 调用确实返回了正确的令牌。我只是似乎无法到达我想去的地方。我将返回 PDO 并尝试转储 $_POST 看看会发生什么。
  • 当我使用 var_dump 它返回 NULL。有什么想法吗?
  • 对不起,我应该更清楚地说明这一点。由于您是从应用程序发布的,因此 var_dump 实际上是无用的,除非您打算使用应用程序来显示输出。我建议使用 print_r (它是额外的参数)将 postvars 转储到文件中。然后像往常一样使用该应用程序并查看该文件包含的内容。我假设您尝试从 Web 浏览器运行 PHP 脚本时获得了 NULL?如果你从应用程序中得到 NULL,那么肯定是某个地方的 postvar 有问题。
猜你喜欢
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 2019-12-23
  • 2020-08-07
  • 2014-06-15
  • 1970-01-01
  • 2012-07-10
相关资源
最近更新 更多