我可以为我的 iPad 获取设备令牌,但问题是如何通过 php 代码将其发送到服务器中的数据库。
我已经测试了 php 代码并且它正在工作。
这是我发送设备令牌的功能:
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"%@",devToken);
NSString *sli = [NSString stringWithFormat:@"http://localhost:8888/insertiphone.php?number=%@",devToken];
NSURL *url = [NSURL URLWithString:sli];
NSData *test = [NSData dataWithContentsOfURL:url];
if ([test length] == 0) {
NSLog(@"no done");
}
}
它不工作..
这是php代码:
<?php
function inserttodatabase(){
$con = mysql_connect("localhost","root","root");
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO iphoneid (idip)
VALUES ($_GET[number])");
echo "done";
}
inserttodatabase();
?>
更新了,我也尝试了这些代码但没有工作
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSString *sli = [NSString stringWithFormat:@"http://localhost:8888/insertiphone.php? number=%@",devToken];
NSLog(@"%@",sli);
NSURL *url = [NSURL URLWithString:sli];
NSURLRequest *urlr = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:urlr delegate:self];
if (connection){
NSLog(@"connecting");
}else {
NSLog(@"error");
}
它说正在连接,但不会将任何内容插入数据库