Ios 目标c的NSURL连接
我已经为我的应用程序创建了一个页面,其中将显示前10名用户的个人资料,以及他们的姓名、年龄、城市和距离。目前我的页面看起来像这样 我必须这样显示 为此,我要求使用POST方法。其中需要显示路径、名称、年龄、城市和距离 谁能建议我如何点击网络服务,因为我不知道NSURL连接。请帮助Ios 目标c的NSURL连接,ios,objective-c,web-services,Ios,Objective C,Web Services,我已经为我的应用程序创建了一个页面,其中将显示前10名用户的个人资料,以及他们的姓名、年龄、城市和距离。目前我的页面看起来像这样 我必须这样显示 为此,我要求使用POST方法。其中需要显示路径、名称、年龄、城市和距离 谁能建议我如何点击网络服务,因为我不知道NSURL连接。请帮助 此代码可能对您有所帮助 This code may be help you Add NSURLConnectionDelegate delegate @interface YourViewController :
此代码可能对您有所帮助
This code may be help you
Add NSURLConnectionDelegate delegate
@interface YourViewController : UIViewController<NSURLConnectionDelegate>
And some where in function in same interface
// Send a synchronous request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if (error == nil) {
// Parse data here
}
And for more this link help you
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
添加NSURLConnectionLegate委托
@界面YourViewController:UIViewController
以及在同一接口中的函数中的某些位置
//发送同步请求
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:[nsurlUrlWithString:@]http://google.com"]];
NSURLResponse*response=nil;
n错误*错误=nil;
NSData*data=[NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&响应
错误:&错误];
如果(错误==nil){
//在这里解析数据
}
更多信息,请访问此链接
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
- 将AFNetworking 3.0导入项目,然后尝试以下操作:
NSString *urlString = @"https://www.example.com/info NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; //add parameters to dictionary AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@", responseObject);//you have to parse this object } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
sendSynchronousRequest
已被弃用,苹果强烈反对使用同步网络。