UIWebView에서 앱 URL을 처리하는 방법은 무엇입니까?
최근에 내 UIWebView가 ITMS 링크에서 질식하는 것을 발견했습니다. 특히, 나는 같은 사이트로 이동 내 응용 프로그램에있는 UIWebView에서, 경우이 하나의 링크 "앱 스토어에서 사용할 수있는"를 클릭하는 UIWebView는 URL은 오류 도메인 = WebKitErrorDomain 코드 = 101 "로 할 수없는 오류가 있습니다. 것 표시됩니다. "
인터넷 검색을 한 후 앱 링크 요청을하고 iOS에서 처리해야한다는 것을 깨달았습니다. 나는 스키마가에서 "itms"로 시작하는지 확인하는 것을 시작 -webView:shouldStartLoadWithRequest:navigationType:
했지만 시스템이 처리 할 수있는 다른 종류의 앱 링크가있을 수 있습니다. 그래서 대신에 말입니다.
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
// Give iOS a chance to open it.
NSURL *url = [NSURL URLWithString:[error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]];
if ([error.domain isEqual:@"WebKitErrorDomain"]
&& error.code == 101
&& [[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication]openURL:url];
return;
}
// Normal error handling…
}
이에 대해 두 가지 질문이 있습니다.
- 이건 정상인가요? 특히 오류 도메인과 오류 코드를 확인하고 userInfo에서 URL을 가져옵니다. 그게 남을 것 같나요?
- 이 위에 링크 된 앱 스토어 링크에서 작동하지만 내 앱으로 다시 전환하면 "프레임로드 중단"으로 실패한 실패 요청이있는 광고 시청. 어떻게 제거 할 수 있습니까? OS가에서 요청을 처리하면
-webView:shouldStartLoadWithRequest:navigationType:
약간의 성가신 일입니다.
어떻게 당신은 그런 요청을 처리합니까?
여기에 내가 생각 해낸 것이 있습니다. 에서는 webView:shouldStartLoadWithRequest:navigationType:
OS에 다음과 같이 http 및 https가 아닌 요청을 처리하도록 요청합니다.
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Determine if we want the system to handle it.
NSURL *url = request.URL;
if (![url.scheme isEqual:@"http"] && ![url.scheme isEqual:@"https"]) {
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
return NO;
}
}
return YES;
}
이 "프레임로드 중단"오류를 제외하고 매우 잘 작동합니다. 나는 webView:shouldStartLoadWithRequest:navigationType:
웹 뷰가 요청을로드하지 않고 처리 할 오류가 없을 점에서 거짓을 반환 생각했습니다 . 그러나 NO
위에서 돌아 왔지만 여전히 "Frame Load Interrupted"오류가 발생합니다. 왜 그런 겁니까?
어쨌든 나는 그것을 무시할 수 있다고 가정합니다 -webView:didFailLoadWithError:
:
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
// Ignore NSURLErrorDomain error -999.
if (error.code == NSURLErrorCancelled) return;
// Ignore "Fame Load Interrupted" errors. Seen after app store links.
if (error.code == 102 && [error.domain isEqual:@"WebKitErrorDomain"]) return;
// Normal error handling…
}
이제 iTunes URL이 mailto:
s 및 앱 링크 처럼 작동 합니다.
이론의 코드로 시작하여 "itms"체계에 대한 URL을 검사합니다 (이 메서드는 리디렉션으로 인해 여러 번 호출 될 수 있음). "itms"체계가 표시되면 webView로드를 중지하고 Safari URL을 중지합니다. 내 WebView는 NavigationController이므로 Safari를 연 후 (덜 깜박임) 튀어 나옵니다.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:@"itms-apps"]) {
[webView stopLoading];
[[UIApplication sharedApplication] openURL:[request URL]];
[self.navigationController popViewControllerAnimated:YES];
return NO;
} else {
return YES;
}
}
itms : links를 처리하기 위해 앱을 등록하면 도움이됩니까?
예 : http://inchoo.net/iphone-development/launching-application-via-url-scheme/
스킴으로 시작한 http
다음 itms
리디렉션 을 얻을 수 있습니다. 앱이 해당 스킴을 처리하도록 등록되지 않은 경우 실패 할 수 있습니다.
참고 URL : https://stackoverflow.com/questions/4299403/how-to-handle-app-urls-in-a-uiwebview
'ProgramingTip' 카테고리의 다른 글
그래프에서 "좋은"격자 선 간격을위한 알고리즘 (0) | 2020.12.01 |
---|---|
Scala에서 특성 및 추상 메서드 재정의 (0) | 2020.12.01 |
PDF를 병합하는 Ghostscript는 결과를 압축합니다. (0) | 2020.12.01 |
Python mock의 모의 속성? (0) | 2020.12.01 |
SHA512에서 해시 된 길이는 얼마입니까? (0) | 2020.12.01 |