[iOS] Background → Foreground 진입 시점에 WebView alert 호출 시 CPU 100% 이슈

1. 개요

앱을 백그라운드 상태로 두고 스키마를 통해 앱으로 진입될 때 AppDelegate에서 WebView에 callback을 줌

  • callback에 alert가 있는 경우 CPU 사용량 100%로 치솟음과 동시에 앱 터치 먹통

 

2. 재현

NSString* str = [[NSString alloc] initWithFormat:@"alert('Test')"];
dispatch_async(dispatch_get_main_queue(), ^{
	[topView stringByEvaluatingJavaScriptFromString:str];
});

topView는 WebView 인스턴스라고 가정

AppDeleage에서 alert('Test')를 호출함

 

이 때 화면에 alert가 나타나며 현상 재현

 

3. 해결 방법

NSString* str = [[NSString alloc] initWithFormat:@"setTimeout(function(){alert('Test');},1);"];

dispatch_async(dispatch_get_main_queue(), ^{
	[topView stringByEvaluatingJavaScriptFromString:str];
});

callback 호출 시점에 setTimeout 함수를 호출하여 1ms 간격을 줌

원인은 파악하지 못 했지만 앱이 백그라운드에서 포그라운드 상태로 전환될 때의 thread 문제로 추측하고 있습니다

정확히 원인을 아시는 분이 계시면 댓글로 남겨주시면 감사하겠습니다~!