Created
October 26, 2020 22:19
-
-
Save soffes/2c932aac6f6acc2e8022628f70d6b664 to your computer and use it in GitHub Desktop.
Open in Safari UIActivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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