1

对于我的 iphone 应用程序,我需要使用 url 中的参数向服务器发出请求。我是开发 ios 应用程序的新手,我不知道如何将我设置的值传递到 url。

我已经尝试过这样的事情,但它说 URLWITHString 的“参数太多”。我如何以正确的方式写下来?

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://test.localhost/api/request.php?url=%@", passedValue] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

在此先感谢,尼克

4

3 回答 3

1
NSString *urlString = [NSString stringWithFormat:@"http://test.localhost/api/request.php?url=%@", passedValue]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];

编辑 -

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://test.localhost/api/request.php?url=%@", passedValue]] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
于 2012-05-14T10:09:09.750 回答
1

试试这个:-

NSString *tempURL = [NSString stringWithFormat:@"http://test.localhost/api/request.php?url=%@", passedValue]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:tempURL] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
于 2012-05-14T10:11:28.413 回答
1

你必须给你的 urlwithstring 参数一个 NSString 对象

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://test.localhost/api/request.php?url=%@", passedValue]] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
于 2012-05-14T10:11:30.100 回答