Skip to content

Commit

Permalink
fix(Browser): avoid crash when using invalid urls (#2056)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Oct 15, 2019
1 parent 14cc338 commit 2e4d8b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ public void open(PluginCall call) {
CustomTabsIntent tabsIntent = builder.build();
tabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,
Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + getContext().getPackageName()));
tabsIntent.launchUrl(getContext(), Uri.parse(url));
call.success();
try {
tabsIntent.launchUrl(getContext(), Uri.parse(url));
call.success();
} catch (Exception ex) {
call.error(ex.getLocalizedMessage());
}
}

@PluginMethod()
Expand Down
37 changes: 20 additions & 17 deletions ios/Capacitor/Capacitor/Plugins/Browser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ public class CAPBrowserPlugin : CAPPlugin, SFSafariViewControllerDelegate {

let toolbarColor = call.getString("toolbarColor")
let url = URL(string: urlString)

DispatchQueue.main.async {
self.vc = SFSafariViewController.init(url: url!)
self.vc!.delegate = self
let presentationStyle = call.getString("presentationStyle")
if presentationStyle != nil && presentationStyle == "popover" {
self.vc!.modalPresentationStyle = .popover
self.setCenteredPopover(self.vc)
} else {
self.vc!.modalPresentationStyle = .fullScreen
}
if let scheme = url?.scheme, ["http", "https"].contains(scheme.lowercased()) {
DispatchQueue.main.async {
self.vc = SFSafariViewController.init(url: url!)
self.vc!.delegate = self
let presentationStyle = call.getString("presentationStyle")
if presentationStyle != nil && presentationStyle == "popover" {
self.vc!.modalPresentationStyle = .popover
self.setCenteredPopover(self.vc)
} else {
self.vc!.modalPresentationStyle = .fullScreen
}

if toolbarColor != nil {
self.vc!.preferredBarTintColor = UIColor(fromHex: toolbarColor!)
}
if toolbarColor != nil {
self.vc!.preferredBarTintColor = UIColor(fromHex: toolbarColor!)
}

self.bridge.viewController.present(self.vc!, animated: true, completion: {
call.success()
})
self.bridge.viewController.present(self.vc!, animated: true, completion: {
call.success()
})
}
} else {
call.error("Invalid URL")
}
}

Expand Down

0 comments on commit 2e4d8b1

Please sign in to comment.