Php 使用NSDictionary发布请求

Php 使用NSDictionary发布请求,php,html,ios,objective-c,nsdictionary,Php,Html,Ios,Objective C,Nsdictionary,大家好:D请向我解释我应该如何在你们的类NSDictionary中使用(在这个问题上)。我应该说什么request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];我的程序是否正常工作? 这是我的密码 #import "MainClass.h" @implementation MainClass - (IBAction)actionButton:(id)sender { NSMutableURLRequest* request

大家好:D请向我解释我应该如何在你们的类NSDictionary中使用(在这个问题上)。我应该说什么
request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];我的程序是否正常工作?

这是我的密码

#import "MainClass.h"

@implementation MainClass

- (IBAction)actionButton:(id)sender {

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL
                                                                    URLWithString:@"https://login.dnevnik.ru"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];


request.HTTPMethod = @"POST";
NSString *username = _loginLogin.text;
NSString *password= _passwordLogin.text;
GlobalUserName = username;
GlobalPassword = password;
NSDictionary *param = @{@"Username": GlobalUserName,@"password": GlobalPassword};



/*NSUserDefaults *loginData = [NSUserDefaults standardUserDefaults];
NSString *username1 = [loginData objectForKey:@"username"] ;
NSString *password1 = [loginData objectForKey:@"password"];*/


//Параметры посылаемого запроса
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8"
forHTTPHeaderField:@"Content-Type"];


NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


if (connection) {
    _otvet.text = @"Соединение установлено";
    NSLog(@"ama ama");

}
else
{
    _otvet.text=@"Проблема с соединением";
    NSLog(@"ama ama faza");
}

}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse 
*)response    
{
[receivedData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Ошибка" message:@"Пожалуйста, проверьте
соединение с Интернетом." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];          
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString * data = [[NSString alloc] initWithData:receivedData    
encoding:NSUTF8StringEncoding];

if ([data   isEqualToString: @"OK"]) {

    _otvet.text = @"Вы успешно залогинены.";
    NSLog(@"Вы успешно залогинены.");
}
else
{
    NSLog(@"Ошибка авторизации %@,%@",GlobalUserName,GlobalPassword);
}
}
在我的例子中,编译时的xcode表示

“NSDictionary”没有可见的@interface声明选择器 '数据使用编码:'


您应该将NSString作为主体传递

这样做:

NSString *postString = [NSString stringWithFormat:@"Username=%@&password=%@",username,password];
request.HTTPBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
或者,如果您希望将NSDictionary编码为JSON字符串,并在后端对其进行解码,那么这是一种更好的方法,但需要做更多的工作:)


我知道这是旧的。但希望你能回应。您在Mainclass.hs中对GlobalUserName和GlobalPassword的定义是什么。。不能通过字典吗?因为我在将NSDictionary传递到php脚本时遇到问题。。。NSStrings可以工作,但NSDictionaryyou可以,只需使用:request.HTTPBody=[NSJSONSerialization dataWithJSONObject:dictionary选项:NSJSONWriting预打印错误:nil]
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:&error]