Xcode에서 Web Page get 하기

2013. 1. 11. 14:43언어/IPhone

- (BOOL)requestUrl:(NSString *)url

{

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];

    [request setHTTPMethod:@"GET"];

    

    // 포스트 방식일때 사용할 코드

    //NSString *bodyString = [NSString stringWithFormat:@"http://www.naver.com/"];

    //[request setHTTPMethod:@"POST"];

    //[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];

    

    NSURLConnection *connection = [[[NSURLConnection alloc]init] initWithRequest:request delegate:self];

    if(connection){

        ReceiveData = [NSMutableData data];

        return YES;

    }else{

        return NO;

    }

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [ReceiveData appendData:data];

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse

{

    [ReceiveData setLength:0];

    mNSURLResponse = aResponse;

}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"Error : %@", [error localizedDescription]);

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSString *mData = [[NSString alloc] initWithData:ReceiveData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", mData);

}


헤더파일

- (BOOL)requestUrl:(NSString *)url;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse;

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;


출처 : http://prideofdark.tistory.com/entry/xcode-%EC%97%90%EC%84%9C-URL-%EC%9D%B8%EC%BD%94%EB%94%A9%EA%B3%BC-%EB%94%94%EC%BD%94%EB%94%A9


http://prideofdark.tistory.com/entry/%EC%9B%B9%EC%84%9C%EB%B2%84%EC%97%90%EC%84%9C-%EC%A0%95%EB%B3%B4-%EA%B0%80%EC%A0%B8-%EC%98%A4%EA%B8%B0

'언어 > IPhone' 카테고리의 다른 글

UITableView 함수 관련 설명  (0) 2013.02.05
xcode textfield next 구현  (0) 2013.01.15
iOS5 에서 OpenCV 사용하기  (0) 2012.03.16
iphone에서 opencv를 사용해보자  (0) 2012.03.08
스티븐 잡스 사망..  (0) 2011.10.06