I\'m using uiactivityviewcontroller for showing sharing option. Everything working except when user selects the Gmail sharing option. If user chooses email then it shows the
It's a bit tricky in Swift 3 and iOS 9+.
First, you need to add googlegmail to LSApplicationQueriesSchemes in Info.plist
Second, here's how my UIActivity subclass looks like:
It's a bit tricky in Swift 3 and iOS 9+.
First, you need to add googlegmail to LSApplicationQueriesSchemes in Info.plist
Second, here's how my UIActivity subclass looks like:
final class GmailNewEmailShareActivity: UIActivity {
private let urlScheme = "googlegmail:"
private var activityItems: [Any]? = nil
override class var activityCategory: UIActivityCategory {
return .share
}
override var activityType: UIActivityType? {
return UIActivityType(rawValue: "googlemail")
}
override var activityTitle: String? {
return "Gmail"
}
override var activityViewController: UIViewController? {
return nil
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
guard let url = URL(string: urlScheme) else {
return false
}
return UIApplication.shared.canOpenURL(url)
}
override func prepare(withActivityItems activityItems: [Any]) {
// At this point I store incoming activity items to use them later in `perform()`
self.activityItems = activityItems
}
override var activityImage: UIImage? {
// 60x60, 120x120 and 180x180
return nil
}
override func perform() {
guard let body = activityItems?.first as? String, let encodedBody = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
activityDidFinish(false)
return
}
let urlString = "\(urlScheme)///co?body=\(encodedBody)"
guard let url = URL(string: urlString) else {
activityDidFinish(false)
return
}
UIApplication.shared.openURL(url)
activityDidFinish(true)
}
}