Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for Gmail to recognize line breaks from text from UIapplication.shared.open url

Tags:

ios

swift

Gmail client does not recognize line breaks within text from UIApplication.shared.openURL(url)

I have a function that returns a tuple of available email clients (validated by UIApplication.shared.canOpen) and the associated URL. It's for an error reporting feature, so the arguments array contains the text that will autopopulate email fields.

There are no issues with launching any of the three email clients, but gmail is the only one that doesn't process the line breaks. Does gmail use a different method?

enum EmailClient {
    case gmail
    case outlook
    case mail

    var title: String {
        switch self {
        case .gmail: return "Gmail"
        case .outlook: return "Outlook"
        case .mail: return "Mail"
        }
    }

    //Creates url used by UIapplciation.shared to launch the client and autopopulate the email
    func url(error: CustomError?) -> URL? {

        guard let username = CredentialManager.username,
            let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
                return nil
        }

        let arguments = [
            username,
            UIDevice().modelName,
            UIDevice.current.systemVersion,
            appVersion,
            error?.description ?? "N/A" //When called from the settings page, no error is passed in
        ]

        var urlFormat: String

        switch self {
        case .gmail: urlFormat = "googlegmail:///co?to=%@&subject=%@&body=%@"
        case .outlook: urlFormat = "ms-outlook://compose?to=%@&subject=%@&body=%@"
        case .mail: urlFormat = "mailto:%@?subject=%@&body=%@"
        }

        return URL(string: String(format: urlFormat, arguments: [
            EMAIL_RECIPIENT,
            EMAIL_SUBJECT.replacingOccurrences(of: " ", with: "%20"),
            String(format: EMAIL_BODY_FORMAT, arguments: arguments).replacingOccurrences(of: " ", with: "%20").replacingOccurrences(of: "\n", with: "%0A")
        ]))
    }
}
like image 240
teachMeSenpai Avatar asked Oct 16 '22 07:10

teachMeSenpai


2 Answers

It seems that using "\r\n" instead of "\n" fixes the problem

like image 143
Andrew Avatar answered Nov 15 '22 11:11

Andrew


1) Add the the scheme to your info.plist

We can do this through the beautiful thing that is the Info.plist file. Add a new key called LSApplicationQueriesSchemes as an array. Then you can enter your apps within the array. The Mail app doesn’t need to go in here, presumably because it is an Apple app. Your entry should look like the below

image

func openGmail(withFrom: String?, withSubject: String?) {

    var gmailUrlString = "googlegmail:///"

    if let from = withFrom {
            gmailUrlString += "co?to=\(from)"
    }

    if let subject = withSubject {
        gmailUrlString += "&subject=\(subject)"
    }

}

One last thing we will need to do is URL encode the subject line before we pass it into the URL. We can do this by calling subjectString?.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed) on the string.

like image 35
Enea Dume Avatar answered Nov 15 '22 13:11

Enea Dume