2012-11-25 3 views
0

응용 프로그램의 웹보기가 홈 화면을 통해 시작하거나 홈 버튼을 두 번 탭하여 응용 프로그램이 활성화 될 때마다 새로 고침하고 싶습니다.UIWebView가 새로 고침되지 않습니다.

내 ViewController.m은 다음과 같습니다

- (void)viewDidLoad 
{ 
NSURL *url = [NSURL URLWithString:@"http://cargo.bplaced.net/cargo/apptelefo/telefonecke.html"]; 
NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
[_webView loadRequest:req]; 

[super viewDidLoad]; 

} 

- (void)viewDidAppear:(BOOL)animated{ 
[super viewDidAppear:animated]; 
[_webView reload]; 
} 

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType { 
if (inType == UIWebViewNavigationTypeLinkClicked) { 
    [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
    return NO; 
} 

return YES; 
} 

무슨 잘못이 코드? 미리 감사드립니다!

답변

3

나는 앱이 전경을 얻었을 때 viewDidAppear:이 실행되지 않을 것이라고 생각하지 않습니다. 이러한 viewWill * 및 viewDid * 메소드는 뷰 전환 (모달, 푸시)을위한 것이지 앱 라이프 사이클 이벤트와 관련이 없습니다.

포어 그라운드 이벤트에 특별히 등록하고 알림을받을 때 웹보기를 새로 고치는 것이 좋습니다. viewDidAppear: 방법으로 알림에 등록하고 viewDidDisappear: 방법으로 등록을 취소합니다. 컨트롤러가 사라지면 컨트롤러가 사용자에게 아무 것도 표시하지 않을 때 webview를 다시로드하지 않거나 좀비 인스턴스를 다시로드하고 충돌을 시도하지 않도록 컨트롤러를 변경합니다. 다음과 같은 것이 작동해야합니다.

- (void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 

    [_webView reload]; // still want this so the webview reloads on any navigation changes 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil]; 
} 

- (void)viewDidDisappear:(BOOL)animated{ 
    [super viewDidDisappear:animated]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; 
} 

- (void)willEnterForeground { 
    [_webView reload]; 
} 
+0

고마워요. 그건 완벽하게 작동합니다. –

관련 문제