网络
HTTP请求
// URL
NSString *urlStr = @"http://xxxxx.com";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// 请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// URL
NSString *urlStr = @"http://xxxxx.com";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// 请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
数据解析
- JSON
- XML
- SAX: NSXMLParser
- DOM: GDataXML
NSURLConnection(iOS9已经过期)
NSURLSession
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[task resume];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
}];
[task resume];
// 创建session
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
// 设置请求头(告诉服务器第1024个字节开始下载)
[request setValue:@"bytes=1024-" forHTTPHeaderField:@"Range"];
// 创建任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
// 开始任务
[task resume];
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
// 打开NSOutputStream
// 获得文件的总长度
// 存储文件的总长度
// 回调
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
// 利用NSOutputStream写入数据
// 计算下载进度
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
// 关闭并且清空NSOutputStream
// 清空任务task
}
- 文件上传
- 设置请求头
- 拼接请求体
- NSURLSessionUploadTask
NSURLSessionUploadTask *task = [[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[task resume];
AFN
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"111",
@"pwd" : @"222"
};
[mgr GET:@"http://baidu.com" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
NSDictionary *params = @{
@"username" : @"111",
@"pwd" : @"222"
};
[mgr POST:@"http://baidu.com" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://baidu.com" parameters:@{@"username" : @"111"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/Mine/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
UIWebView
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
//OC调用JS
[webView stringByEvaluatingJavaScriptFromString:@"login();"];
**
* 通过这个方法完成JS调用OC
* JS和OC交互的第三方框架:WebViewJavaScriptBridge
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
- 利用NSInvocation实现performSelector无限参数
#import "NSObject+Extension.h"
@implementation NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
{
// 方法签名(方法的描述)
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
if (signature == nil) {
[NSException raise:@"错误信息" format:@"%@方法找不到", NSStringFromSelector(selector)];
}
// NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
// 设置参数
NSInteger paramsCount = signature.numberOfArguments - 2; // 除self、_cmd以外的参数个数
paramsCount = MIN(paramsCount, objects.count);
for (NSInteger i = 0; i < paramsCount; i++) {
id object = objects[i];
if ([object isKindOfClass:[NSNull class]]) continue;
[invocation setArgument:&object atIndex:i + 2];
}
// 调用方法
[invocation invoke];
// 获取返回值
id returnValue = nil;
if (signature.methodReturnLength) { // 有返回值类型,才去获得返回值
[invocation getReturnValue:&returnValue];
}
return returnValue;
}
@end