Adding UIPreviewActionItem
to SFSafariViewController
might be a tedious task. This extansion should help.
let vc = SFSafariViewController(initialURL: url, entersReaderIfAvailable: true)
vc.previewActionItemsDelegate = self
When presenting SFSafariViewController
use convenience initializer that will store original URL for later, it'll make custom action requiring original URL easier. But it's not mandatory.
Here is the delegate implementation, this is where we might want to use initialURL
func safariViewControllerPreviewActionItems(controller: SFSafariViewController) -> [UIPreviewActionItem] {
var actions: [UIPreviewActionItem] = []
if let url = controller.initialURL {
// Only if we know initialURL
let action1 = UIPreviewAction(title: "Action 1", style: .Default, handler: { action, controller in
print("action1 for initial URL \(url)")
})
actions.append(action1)
}
let action2 = UIPreviewAction(title: "Action 2", style: .Default, handler: { action, controller in
print("action1")
})
actions.append(action2)
return actions
}
All the API I used is objective c API. You can literally rewrite it in objective c as is and it should work