Skip to content

Instantly share code, notes, and snippets.

@soffes
Created October 26, 2020 22:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soffes/2c932aac6f6acc2e8022628f70d6b664 to your computer and use it in GitHub Desktop.
Save soffes/2c932aac6f6acc2e8022628f70d6b664 to your computer and use it in GitHub Desktop.
Open in Safari UIActivity
import UIKit
final class SafariActivity: UIActivity {
// MARK: - Properties
var url: URL?
// MARK: - UIActivity
override var activityType: UIActivity.ActivityType? {
UIActivity.ActivityType(rawValue: String(describing: type(of: self)))
}
override var activityTitle: String? {
"Open in Safari"
}
override var activityImage: UIImage? {
UIImage(systemName: "safari")?.applyingSymbolConfiguration(.init(scale: .large))
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
for item in activityItems {
guard let url = item as? URL, UIApplication.shared.canOpenURL(url) else {
continue
}
return true
}
return false
}
override func prepare(withActivityItems activityItems: [Any]) {
for item in activityItems {
guard let url = item as? URL, UIApplication.shared.canOpenURL(url) else {
continue
}
self.url = url
return
}
}
override func perform() {
guard let url = url else {
activityDidFinish(false)
return
}
UIApplication.shared.open(url) { [weak self] completed in
self?.activityDidFinish(completed)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment