dongmi1995 2015-05-25 02:40
浏览 34

使用Objective-C发送文本以评论PHP站点

I want to sent comment in controller to PHP site.

The following is my code for doing that:

NSString *word = _commentText.text;
NSString *user =[keychain objectForKey:(__bridge id)(kSecAttrAccount)];
NSSTring *post = 500 //webpage content site 
if(word !=nil){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Finished" message:@"Comment Finished" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    //NSString *filenames=[NSString stringWithFormat:@"TextLabel"];
    NSString *postString = [[NSString alloc] initWithFormat:@"id=%@&no=%@&comment=%@",user,code,word];
    NSData *myRequestData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]];

    NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://mylink.com/comment_upload2.php"]];
    [request setHTTPMethod:@"POST"];
    NSLog(@"%@",_commentText.text);
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody: myRequestData];

    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
    [NSURLConnection connectionWithRequest:request delegate:self];
    NSLog(@"%@", request);
    NSLog(@"%@",response);
    NSData *retunData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
     NSString *returnString = [[NSString alloc]initWithData:retunData encoding:NSUTF8StringEncoding];
    [request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
     NSLog(returnString);
     NSLog(@"Commented");

}//if

But when I touch on the comment, the comment does not appear in my site.

  • 写回答

1条回答 默认 最新

  • dongtang1944 2015-05-25 03:53
    关注

    Make sure your php code get the data after you send the request. Something like this:

    if (isset($_POST['id'] && isset($_POST['no'] && isset($_POST['comment']) {
      // log data here
    }
    

    Then return the data in json format using json_encode() function so you'll know that you get the data right. You can use the code below to perform the request.

        NSString *post       =  [[NSString alloc] initWithFormat:@"id=%@&no=%@&comment=%@",user,code,word];
        NSData *postData     =  [post dataUsingEncoding:NSASCIIStringEncoding];
        NSString *postLength =  [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
        NSString * urlstring =  [YOUR_API_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
        NSURL *url                      =   [NSURL URLWithString:urlstring];
        NSMutableURLRequest *request    =   [NSMutableURLRequest requestWithURL:url];
        request.HTTPMethod              =   @"POST";
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
    
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
         {
             if(connectionError) {
                 NSLog(@"Connection Error:
    %@",connectionError.description);
             } else {
                 NSDictionary* responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                 NSLog(@"responseDictionary : %@", responseDictionary);
             }
         }];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作