2013-02-26 3 views
0

URL에서 JSON 배열을 가져 와서 테이블 뷰를 채우는 데 사용하는 iOS 앱의 일부로 다음 코드를 사용합니다. 어떤 이유로, 요청이 함수에서 수행되지 않고 있으며, 이유를 파악할 수 없습니다. 이 기능은 아래와 같습니다.이 URL 요청이 호출되지 않는 이유는 무엇입니까?

P.S - 나는이 Objective-C 종달새의 초보자입니다. 정말 확실한 실수를했다면 용서해주세요!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

    // Create a URL Request 
    self.responseData = [NSMutableData data]; 
     NSURLRequest *request = [NSURLRequest requestWithURL: 
    [NSURL URLWithString:@"http://mydomain.com/return_json_list"]]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

요청이 비동기이며 대리자 메서드로 다시보고됩니다. 그래서'NSURLConnectionDelegate' 메쏘드 중 일부를 구현할 필요가 있습니다 :'- connection : didReceiveResponse :'. docs –

답변

0

당신은 실제로 요청을하지 ... 당신은 NSURLConnection 또는 + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error (동기)에 start (비동기)를 호출 할 필요가있다. 당신의 UI에 영향을 줄 수 있도록,

self.responseData = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mydomain.com/return_json_list"]]; 

그것은 주 스레드에서 발생주의하십시오 :

당신은 후자를 수행 할 수 있습니다.

+0

에 대한 https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html을 확인하십시오. 특히 JSON 데이터와 같이 동기 요청이있는 메인 스레드를 막히는 것은 좋지 않습니다. 다운로드되는 데는 시간이 걸릴 수 있습니다. 또한'start'를 호출 할 필요가 없습니다 -'initWithRequest : delegate :'호출은'initWithRequest : delegate : startImmediately : YES'를 호출하는 것과 같습니다. –

관련 문제