UIBarButtonItem for Safari?

302 Views Asked by At

I’m writing an app that can open webpages in a WKWebView. I currently have a standard Share icon in the navigation bar that I get from the system initWithBarButtonSystemItem:UIBarButtonSystemItemAction, however the only option that is available for the time being is opening the page in Safari.

Share icon

Seems like a waste of a tap to open a UIActionSheet with only one menu option, so I would like the button to open the webpage in Safari directly.

I can't find a system-standard UIBarButtonSystemIcon for "Open in Safari." Is there one? If there isn't, how do people normally go about this? I'm pretty sure I've seen them in the wild, wire-frame Safari icons in the navigation bar. Do I just have to find a third-party one or repurpose one from elsewhere in UIKit? If so, where?

1

There are 1 best solutions below

0
magiclantern On

A couple days later, figured it out.

The correct approach is to defer this decision to the OS, by presenting a UIActivityViewController. The activity view controller will populate the available activities (Mail, Message, Twitter, Facebook, Copy, Add to Reading List, etc.) according to the object you're passing it as the thing you want to "share."

The catch is that the system does not provide a default "Open in Safari" UIActivity—you have to make one yourself. There is third-party open source code available that achieves this.