Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
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 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda