IOS入门之网络请求

- (void)getPath:(NSString *)path 
     parameters:(NSDictionary *)parameters 
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
	NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self enqueueHTTPRequestOperation:operation];
}

- (void)postPath:(NSString *)path 
      parameters:(NSDictionary *)parameters 
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
	NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
	AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self enqueueHTTPRequestOperation:operation];
}

AFHTTPRequestOperation#responseString

TBXML *xml = [[TBXML alloc] initWithXMLString:raw_stringerror:nil];
TBXMLElement *element = [TBXMLchildElementNamed:@"child_name"parentElement:parent];
[TBXML nextSiblingNamed:@"element_name" searchFromElement:element]
text = [TBXML textForElement : element]

NSNotificationCenter的使用

NSNotificationCenter作用类似与Android中广播

+ (instancetype)defaultCenter;

- (instancetype)init;	/* designated initializer */

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;

eg: 

[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil]; 

 [[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];


dispatch_once  实现单例

+ (AFOSCClient *)sharedClient {
    static AFOSCClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFOSCClient alloc] initWithBaseURL:[NSURL URLWithString:kAFUrl]];
        [_sharedClient setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@", [Tool getOSVersion], [Config Instance].getIOSGuid]];
    });
    
    return _sharedClient;
}

简单数据存储

NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    [setting setObject:str forKey:key];
    [setting synchronize];


异步加载图片

EGOImagView

EGOImageView *imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];  
imageView.imageURL = [NSURL URLWithString:str];  
imageView.frame = CGRectMake(4.0f, 4.0f, 36.0f, 36.0f);  
[self.view addSubView:imageView];


你可能感兴趣的:(IOS入门之网络请求)