-2
我有RequestManager類與getContentInBackgroundWithMemberId和postRequest函數。我想從我的視圖控制器調用它們,並使用完成處理程序獲取結果。如何編輯我的功能?客觀c完成處理程序
RequestManager.h
#import <Foundation/Foundation.h>
@interface RequestManager : NSObject
-(void)getContentInBackgroundWithMemberId:(int)memberId;
@end
RequestManager.m
#import "RequestManager.h"
@implementation RequestManager
-(void)postRequestWithParams:(NSDictionary*)params
{
NSString *parameters = @"encrypt=93mrLIMApU1lNM619WzZje4S9EeI4L2L";
for(id key in params)
{
NSString *obj = [NSString stringWithFormat:@"&%@=%@",key,[params objectForKey:key]];
[parameters stringByAppendingString:obj];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://someserver"]];
NSData *postBody = [parameters dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if(!connectionError)
{
NSDictionary *serverData =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *result = [NSArray array];
result = [serverData objectForKey:@"result"];
}
}];
}
-(void)getContentInBackgroundWithMemberId:(int)memberId
{
NSDictionary *params = [NSDictionary dictionary];
params = @{@"member_id":[NSNumber numberWithInt:memberId]};
[self postRequestWithParams:params];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "RequestManager.h"
@interface ViewController : UIViewController
@property (strong,nonatomic) RequestManager *requestManager;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
int memberId = 82;
//here i want to call getContentInBackgroundWithMemberId and get result using completion handler;
_requestManager = [[RequestManager alloc]init];
[_requestManager getContentInBackgroundWithMemberId:memberId];
}
@end
thanx。以及從視圖控制器調用它時的語法? –
@DenisWindover'getContentInBackgroundWithMemberId:(int)memberId completed:^(BOOL status){//在請求完成後調用}' – Teffi