iOS 9.1 导致我的 JSON NSURL 连接到 PHP 页面时出错

iOS 9.1 has caused an error with my JSON NSURL connection to a PHP page

在 iOS 9.1 之前,我的应用程序已成功与 Web 上的 .php 文件通信以获取一些数据库信息。这一切的交流都是通过JSON来存储两人之间的信息完成的。我知道有一个问题似乎与发送到我的 .php 文件的数据包有关。

if($_POST == null){ // Check for proper posting.

    $handle  = file_get_contents('php://input');
    $body = json_decode($handle, true);
}
else{
    $body == $_POST;        
}

上面的代码似乎是失败点。我收到一个不为空的 $_POST

-(void)postAuthorize:(NSString*)code :(NSString*)phone{

   //build up the request that is to be sent to the server
   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com/ex.php"]];

   [request setHTTPMethod:@"POST"];
   [request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];

   //create data that will be sent in the post
   NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
   [dictionary setValue:code forKey:@"authorize"];
   [dictionary setValue:phone forKey:@"device"];

   NSLog(@"Dictionary: %@", dictionary);

   //serialize the dictionary data as json
   NSData *data = [[dictionary copy] JSONValue];

   NSLog(@"Data: %@", data);

   [request setHTTPBody:data]; //set the data as the post body
   [request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
   NSLog(@"data length: %lu", (unsigned long)data.length);
   NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   if(!connection){
       NSLog(@"Connection Failed");
   }

}

以上是iPhone上的代码。我检查了 URL 连接正文中的 JSON 以确保其格式正确。好像不是。

这是我得到的错误:

Domain=NSCocoaErrorDomain Code=3840.

但我认为这只是因为 PHP 代码失败了。如果我绕过上面的代码并只发回一个静态值,一切都会顺利进行。

我真的需要帮助。请如果有人能指出什么是错的。我不知道为什么这已经工作了 2 年,现在它从 iOS 9.0 -> 9.1

中断了

问题出现在我的 PHP 中。我猜 Apple 在 9.0/9.1 中用 URL 改变了 $_POST 方法的连接改变了一些东西。

$handle  = file_get_contents('php://input');
$body = json_decode($handle, true);

把你的php代码改成这个-

$handle  = file_get_contents('php://input');

if($handle == null || $handle == ""){
    $body = $_POST; 
}else{
    $body = json_decode($handle);
}