2013-01-14 2 views
0

저는 wcf 서비스에서 요청을 코딩하는 방법에 대한 온라인 기사/자습서를 찾으려고했습니다.objective c calling wcf rest service request

[ServiceContract] 
    public interface IUserAccountService 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "UserLogIn?id={email}&password={password}")] 
     AuthenticationToken UserLogIn(string email, string password); 
    } 

내가 찾아 봤는데 SO 기사 또는 관련된 질문을 정말 혼란 받고 있어요 : 나는 다음과 같은 웹 서비스를 내 서버에 업로드 한

예 :

  • -http : //stackoverflow.com/questions/1557040/objective-c-best-way-to-access-rest-api-on-your-iphone

  • -http : // 유래 .com/questions/8650296/nsjsonserialization 파싱 응답 데이터

마지막이 우연히 :

http://iam.fahrni.ws/2011/10/16/objective-c-rest-and- json/

내 질문에, 정말 API를 호출 할 수있는 편안한 프레임 워크를 사용해야합니까? 그렇다면 어떤 것이 더 권장되는지 - ASIHttpRequest 또는 RestKit 또는 AFNetworking? 또는 내가 언급 한 마지막 링크를 사용하여 간단하게 할 수 있습니까? 나는 어디서부터 시작해야할지 모르겠습니다.

감사합니다.

+0

확인이 - http://stackoverflow.com/questions/8922296/is-restkit-a-good-replacement-for-asihttprequest – rishi

답변

1

NSURLConnection 및 NSJSONSerialization이 정상적으로 작동합니다.

편집 : 간략하게 편집 된 나의 프로젝트 중 하나의 예제 코드.
fstr (...)은 [NSString stringWithFormat : ...]의 래퍼입니다.
GCD가있는 백그라운드 스레드에서이 코드를 호출합니다. 스레드로부터 안전하지 않습니다.

- (NSMutableURLRequest *)buildGetRequestHeaderWithMethod:(NSString *)method 
{ 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
    initWithURL:[NSURL URLWithString:fstr(@"%@%@", self.url, method)]]; 
    [request setTimeoutInterval:10.0]; 
    [request setHTTPMethod:@"GET"]; 
    [request setValue:self.key forHTTPHeaderField:@"Authentication"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    return request; 
} 

- (id)callMethod:(NSString *)method 
{ 
    NSMutableURLRequest *request = [self buildGetRequestHeaderWithMethod:method]; 
    return [self sendRequest:request withMethod:method]; 
} 

- (id)sendRequest:(NSMutableURLRequest *)request withMethod:(NSString *)method 
{ 
    NSHTTPURLResponse *response = nil; 
    NSError *error = nil; 
    [state() pushNetworkActivity]; 
    NSData *result = [NSURLConnection sendSynchronousRequest:request 
    returningResponse:&response error:&error]; 
    [state() popNetworkActivity]; 
    self.lastStatusCode = response.statusCode; 
    // Bug in Cocoa. 401 status results in 0 status and NSError code -1012. 
    if(error && [error code] == NSURLErrorUserCancelledAuthentication) 
    { 
    [self interpretHTTPError:401 URLError:error forMethod:method]; 
    self.lastStatusCode = 401; 
    return nil; 
    } 
    if(response.statusCode != 200) 
    { 
    [self interpretHTTPError:response.statusCode URLError:error forMethod:method]; 
    return nil; 
    } 
    id jsonResult = [self parseJsonResult:result]; 
    debug(@"%@", jsonResult); 
    return jsonResult; 
} 


- (void)interpretHTTPError:(int)statusCode URLError:(NSError *)urlError 
    forMethod:(NSString *)method 
{ 
    NSString *message = fstr(@"HTTP status: %d", statusCode); 
    if(statusCode == 0) 
    message = [urlError localizedDescription]; 

#ifdef DEBUG 
    message = fstr(@"%@ (%@)", message, method); 
#endif 

    if(self.alertUserOfErrors) 
    { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     errorMessage (message); 
    }); 
    } 
    else 
    debug(@"%@", message); 
    self.lastErrorMessage = message; 
} 

- (id)parseJsonResult:(NSData *)result 
{ 
    if(! result) 
    return nil; 
    NSError *error = nil; 
    id jsonResponse = [NSJSONSerialization JSONObjectWithData:result 
    options:NSJSONReadingMutableContainers error:&error]; 
    if(error) 
    { 
    NSLog(@"JSONObjectWithData failed with error: %@\n", error); 
    return nil; 
    } 
    return jsonResponse; 
} 
+0

당신은 내가 그렇게 볼 수있는 예제 소스 코드의 알고 난 시퀀스가 어떻게 작동하는지 이해할 수 있습니까? – gdubs

+0

@gdubs 내 프로젝트 중 하나에서 코드를 추가했습니다. – Minthos

+0

끔찍하고 아프다. 고맙습니다! 나는 약간 질문을 역시 가지고 있을지도 모르다. – gdubs