iOS URL 请求无效
iOS URL Request Not Working
我正在尝试将数据发送到 PHP 网页(它已经知道如何处理接收 URL)。单击特定按钮后,我的应用程序应该发送 URL 请求,但事实并非如此。我正在使用 "NSURLConnectionDelegate",但我没有实现任何方法,我是否正确遵守它?我有以下代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonClicked:(id)sender {
NSString *temp = [NSString stringWithFormat:@"www.private.com/recievedata.php?name=%@&address=%@&longitude=%@&latitude=%@",_nameLabel.text,_addressLabel.text,_longitudeLabel.text,_latitudeLabel.text];
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
@end
这是获得所需内容的最简单方法:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
//Handle here
}];
这是非常有限的,因为您无法取消此连接,如果需要更复杂的 SSL 质询,则无法提供。不过这对你来说似乎已经足够了。
更新: 从 iOS 9 开始,NSURLConnection
已弃用。相反,使用 NSURLSession
。这里和上面一样,用新的 API:
实现
[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
//Handle here
}] resume];
使用下面的代码代替[NSURLConnection connectionWithRequest:request delegate:self];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:
[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error)
{
//error message
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//handle server's response here...
});
}
}];
希望对you.Thank你有所帮助。
我正在尝试将数据发送到 PHP 网页(它已经知道如何处理接收 URL)。单击特定按钮后,我的应用程序应该发送 URL 请求,但事实并非如此。我正在使用 "NSURLConnectionDelegate",但我没有实现任何方法,我是否正确遵守它?我有以下代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonClicked:(id)sender {
NSString *temp = [NSString stringWithFormat:@"www.private.com/recievedata.php?name=%@&address=%@&longitude=%@&latitude=%@",_nameLabel.text,_addressLabel.text,_longitudeLabel.text,_latitudeLabel.text];
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
@end
这是获得所需内容的最简单方法:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
//Handle here
}];
这是非常有限的,因为您无法取消此连接,如果需要更复杂的 SSL 质询,则无法提供。不过这对你来说似乎已经足够了。
更新: 从 iOS 9 开始,NSURLConnection
已弃用。相反,使用 NSURLSession
。这里和上面一样,用新的 API:
[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
//Handle here
}] resume];
使用下面的代码代替[NSURLConnection connectionWithRequest:request delegate:self];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:
[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error)
{
//error message
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//handle server's response here...
});
}
}];
希望对you.Thank你有所帮助。