웹 서비스 API에서 데이터를 다운로드하고 있습니다. 많은 프로젝트에서 다른 API를 사용하여 데이터를 다운로드해야합니다. 따라서 Iam이 웹 서비스를 호출하는 곳마다 복잡성이 있습니다. 코드iOS에서 NSMutableURLRequest 및 NSURLConnection 관리하기
- (void)downloadInvitedVcards
{
AppManager *oAppManager = [AppManager getSharedInstance];
[oAppManager hasreachabilityChanged];
if (oAppManager.currentConnectivity) {
//NSString *deviceUDID = oAppManager.deviceID;
NSArray *keys = [NSArray arrayWithObjects:@"UserName",@"PMobileNo",nil];
NSArray *objects = [NSArray arrayWithObjects:oAppManager.userName,oAppManager.mobileno,nil];
NSData *_jsonData = nil;
NSString *_jsonString = nil;
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
if ([NSJSONSerialization isValidJSONObject:jsonDictionary]) {
_jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
_jsonString = [[NSString alloc] initWithData:_jsonData encoding:NSUTF8StringEncoding];
}
NSString *newUrl = oAppManager.loginURL;
NSString *appendUrl = [newUrl stringByAppendingString:@"/DownloadInvitedVcards"];
NSURL *aUrl = [NSURL URLWithString:appendUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:_jsonData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[_jsonData length]] forHTTPHeaderField:@"Content-Length"];
NSError *errorReturned = nil;
NSURLResponse *theResponse = [[NSURLResponse alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
NSLog(@"Error %@",errorReturned);
}
else {
NSString *responseString = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",responseString);
NSArray *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSDictionary *resultData in responseData) {
NSLog(@"Value :%@",resultData);
}
}
}
}
언제 어디서나 동일한 프로세스를 수행하지만 API 메소드는 변경해야합니다. 그것은 잘 작동하지만 더 많은 코드와 복잡성을 증가시킵니다. 이것을 달성하기위한 더 좋은 방법이 있습니까?
그래서 문제가 무엇입니까? public 메서드 (매개 변수)를 사용하여 클래스 파일을 만들고 원하는 곳 어디에서나 호출하십시오. –