Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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")
        ]))
    }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!