From 97b46b479e262127261158620274492a04ab977d Mon Sep 17 00:00:00 2001 From: Yueyu Date: Tue, 10 Aug 2021 06:44:03 +0800 Subject: [PATCH] iOS: open external link in browser --- ios/ScratchJr/src/ViewController.m | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ios/ScratchJr/src/ViewController.m b/ios/ScratchJr/src/ViewController.m index a8bc507..1936fe1 100644 --- a/ios/ScratchJr/src/ViewController.m +++ b/ios/ScratchJr/src/ViewController.m @@ -197,6 +197,24 @@ NSDate *startDate; }); } +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { + // All internal urls are started with file:///, if the url scheme is http or https, + // the app is trying to open an external link, we should open it in the browser. + // For now, only the PBS edition opens an itunes link in the lobby. + // If we are going to support more shemes like opening other apps or ftp etc, + // this is the right place to go. + NSURL *url = navigationAction.request.URL; + NSString *scheme = url.scheme; + if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"]) { + if ([UIApplication.sharedApplication canOpenURL:url]) { + [UIApplication.sharedApplication openURL:url options:@{} completionHandler:nil]; + } + decisionHandler(WKNavigationActionPolicyCancel); + return; + } + decisionHandler(WKNavigationActionPolicyAllow); +} + // Sharing controllers - if we later decide to unify, use UIActivityViewController // Email sharing