AFN 응용 및 원리
- NSURLSession
+ AFURLSessionManager
+ AFHTTPSessionManager( HTTP )
* GET
* POST
* UIKit + AFNetworking
* NSProgress : KVO
- &
AFURLRequestSerialization : /
AFURLResponseSerialization : / JSON
-
+
* HTTPS
* AFSecurityPolicy
+
*
* AFNetworkReachabilityManager
AFN 요청 직렬 화 및 응답 반 직렬 화
:
AFHTTPRequestSerializer
AFJSONRequestSerializer JSON JSON
AFPropertyListRequestSerializer , Plist , Android
:
**AFHTTPResponseSerializer **
**AFJSONResponseSerializer , JSON **
AFXMLParserResponseSerializer SAX NSXMLParser(XML , )
AFXMLDocumentResponseSerializer (Mac OS X) Dom iOS
AFPropertyListResponseSerializer PList
AFImageResponseSerializer , SDWebImage
AFCompoundResponseSerializer
2 AFN 의 기본 사용 GET 요청 발송
- (void)getDemo{
//1.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2. (GET/POST/PUT/DELETE/HEAD)
/** 1: 2: ,get 3: , nil 4: 5: : 4, 5, AFN */
[manager GET:@"http://localhost/demo.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
/** 1:task 2:responseObject , **/
NSLog(@"%@----%@",responseObject,[responseObject class]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
/** 1:task 2:error **/
NSLog(@"%@",error);
}];
}
POST 요청 보 내기
- (void)postDemo{
//1.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2. POST
/** POST AFN parameters, NSString *postParamStr = @"username=zhangsan&password=zhang"; //2.3 request.HTTPBody = [postParamStr dataUsingEncoding:NSUTF8StringEncoding]; */
NSDictionary *params = @{
@"username":@"******",
@"password":@"*****"
};
[manager POST:@"http://localhost/login.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
작은 파일 업로드
:application/octet-stream
- (void)fileUpload{
//1.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2. POST
/** parameters , */
NSDictionary *textDict = @{
@"status":@" "
};
[manager POST:@"http://localhost/post/upload-m.php" parameters:textDict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// , formData
//1.
NSURL *fileURL1 = [[NSBundle mainBundle] URLForResource:@"head1.png" withExtension:nil];
/** 1: URL 2: head1 3: 4: */
[formData appendPartWithFileURL:fileURL1 name:@"userfile[]" fileName:@"head1.png" mimeType:@"image/png" error:NULL];
//2.
NSURL *fileURL2 = [[NSBundle mainBundle] URLForResource:@" .jpg" withExtension:nil];
NSData *fileData2 = [NSData dataWithContentsOfURL:fileURL2];
/*** 1: 2: head1 3: 4: */
[formData appendPartWithFileData:fileData2 name:@"userfile[]" fileName:@"ymd.jpg" mimeType:@"application/octet-stream"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@---%@",responseObject,[responseObject class]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
HTTPS 사이트 방문
- (void)AFNHTTPS{
//1.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//AFN 2.5.4
/** manager.securityPolicy.allowInvalidCertificates = YES; **/
//AFN 2.6.1 3.0.4, ,
manager.securityPolicy.validatesDomainName = NO;
// text/html,
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
//3. https://www.XXXX.com html,
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//4.
[manager GET:@"https://www.XXXX.com" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//NSLog(@"%@",responseObject);
//
NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",html);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
감청 망 상태
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.
#warning
// AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager manager];
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
/** AFNetworkReachabilityStatusUnknown AFNetworkReachabilityStatusNotReachable AFNetworkReachabilityStatusReachableViaWWAN AFNetworkReachabilityStatusReachableViaWiFi WiFi */
//2.
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"%zd",status);
if (status==AFNetworkReachabilityStatusReachableViaWWAN) {
NSLog(@" ");
}else if(status==AFNetworkReachabilityStatusReachableViaWiFi){
NSLog(@" ");
}
}];
//3.
[manager startMonitoring];
}
AFN 의 인 스 턴 스: (json 형식의 내용 을 서버 에 보 내 고 문자열 형식 으로 되 돌려 줍 니 다)
-(void)AFNDemo{
//1.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// JSON
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//2. POST
/** parameters {"name":"xiaohua","age":20}; AFN [NSJSONSerialization dataWithJSONObject:<#(nonnull id)#> options:<#(NSJSONWritingOptions)#> error:<#(NSError * _Nullable __autoreleasing * _Nullable)#>]; JSON */
NSDictionary *person = @{
@"name":@"kongkong",
@"age":@33
};
//1, ,AFN json, AFN text/plain
//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
// manager.responseSerializer=[AFJSONRequestSerializer serializer];
// , , ,
// ,
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:@"http://localhost/post/postjson.php" parameters:person progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//NSLog(@"%@",responseObject);
NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",html);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.