When we want to afnetworking get registered login URL, sometimes found that host provides a dynamic IP, each time the URL is not the same, specifically encapsulated a method of acquiring IP
#import <Foundation/Foundation.h> #import "FQinterface.h" #import "AFNetworking.h"/** This is a category used to encapsulate the network request for this project */@interface AFHTTPRequestOperationManager (qfpkrequest)/** Further encapsulation of the afnetworking request before the request, will get the server's IP, if there is no IP, just before this request, go to request IP, and then the request this function parameters and Afnetworking parameters consistent */- ( afhttprequestoperation *) Getrequest: (nsstring *) urlstring parameters: (ID) parameters success: (void (^) (afhttprequestoperation *operation, id responseobject)) success &Nbsp; failure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure;/** further encapsulation of the request operation, making the requested code in the controller more concise */+ (afhttprequestoperation *) Getrequest: (nsstring *) urlstring parameters: (ID ) parameters success: (void ( ^) (afhttprequestoperation *operation, id responseobject)) success failure: (void (^) (afhttprequestoperation *operation, nserror *error)) failure; @end
#import "FQNetwork.h" static nsstring *hostip = nil; @implementation afhttprequestoperationmanager (qfpkrequest)- (afhttprequestoperation *) GETRequest: (NSString *) Urlstring parameters: (ID) parameters success: (void (^) (afhttprequestoperation *, id responseobject)) Success failure: (void (^) (afhttprequestoperation *, nserror *)) failure{ afhttprequestoperation *operation = nil; // If IP is not the first to request IP, if there is IP stitching into full path if (Hostip == nil) { // Request IP First, get IP and then do the actual request operation = [self gethostipcompletion:^ { // Call afnetworking Original Request method nsstring *fullurl = [nsstring stringwithformat:@ "http://%@:80%@", hostIP, urlstring]; nslog (@ "%@", FullUrl); [self get:fullurl parameters:parameters success:success failure:failure]; }]; } else { // if IP already exists // Call Afnetworking the original request method nsstring *fullurl = [ nsstring stringwithformat:@ "http://%@:80%@", hostip, urlstring]; nslog (@ "fullurl = %@ ", fullurl); operation = [self get: fullurl parameters:parameters success:success failure:failure]; } return operation;} + (afhttprequestoperation *) Getrequest: (nsstring *) Urlstring parameters: (ID) parameters success: (void (^) (afhttprequestoperation *,id responseobject)) Success failure: (void (^) (afhttprequestoperation *, nserror *)) failure{ // Download data afhttprequestoperationmanager *requestmanager = [ afhttprequestoperationmanager manager]; return [ Requestmanager getrequest:urlstring parameters:parameters success:success failure:failure] ;} - (afhttprequestoperation *) Gethostipcompletion: (void (^) (void)) completion{ afhttprequestoperationmanager *requestmanager = [ afhttprequestoperationmanager manager]; requestmanager.responseserializer = [afhttpresponseserializer serializer]; return [requestmanager get: url_hostip parameters:nil success:^ (afhttprequestoperation *operation, id Responseobject) { // turn binary data into text nsstring *responsetext = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; // Save IP hostIP = responseText; cOmpletion (); } failure:^ ( Afhttprequestoperation *operation, nserror *error) { completion (); nslog (@ "get host ip error: %@", error); }];} @end
This method is encapsulated in the afnetworking
The tagged parts of the figure need IP URLs to obtain IP, which is typically provided by the server
After the file import, you can use the following method to register or log in to parse
- (void) requestweb{ //parameters nsdictionary *params = @{@ "user_id": @ "0", @ "is_traditional": @ "0", @ "page": @ "1", @ "PageRecord": @ "3", @ "Phonetype": @ "1",}; [afhttprequestoperationmanager getrequest:url_cookbook parameters:params success:^ (AFHTTPRequestOperation * Operation, id responseobject) { // the data returned by the print server nslog (@ " %@ ", operation.responseobject); } failure:^ ( Afhttprequestoperation *operation, nserror *error) {// nslog (@ "%@", error); }];}
Dynamic access to host IP when registering for login