我需要从 xcode 打开我的 php url,并且我在 url 中传递了一个变量。我正在执行以下代码,但它不起作用,我已经检查过在 Web 浏览器中输入此 url 是否有效。
NSString *urlformatted = [NSString stringWithFormat:@"http://arcamm.uc3m.es/notifications/saveID.php?id=%@",deviceToken];
NSLog(@"web service URL: %@", urlformatted);
[urlformatted stringByAppendingString:[urlformatted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: urlformatted] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: request delegate:self startImmediately:YES];
NSLog(@"connection: %@", [connection debugDescription]);
if (connection) {
NSLog(@"Connection succeeded");
} else {
NSLog(@"Connection failed");
}
它告诉我连接成功,但变量没有出现在我的数据库中。而且我也得到了 deviceToken 的正确值。
编辑:发现我的错误,我收到了格式化的 deviceToken,所以我做了以下操作来转义 <> 和“”
NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];