Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Gmail有没有办法从UIapplication.shared.open url的文本中识别换行符_Ios_Swift - Fatal编程技术网

Ios Gmail有没有办法从UIapplication.shared.open url的文本中识别换行符

Ios Gmail有没有办法从UIapplication.shared.open url的文本中识别换行符,ios,swift,Ios,Swift,Gmail客户端无法识别UIApplication.shared.openURL(url)文本中的换行符 我有一个函数,它返回可用电子邮件客户端的元组(由UIApplication.shared.canOpen验证)和关联的URL。这是一个错误报告功能,因此arguments数组包含自动填充电子邮件字段的文本 启动这三个电子邮件客户端中的任何一个都没有问题,但gmail是唯一一个不处理断线的。gmail使用不同的方法吗 enum EmailClient { case gmail

Gmail客户端无法识别UIApplication.shared.openURL(url)文本中的换行符

我有一个函数,它返回可用电子邮件客户端的元组(由UIApplication.shared.canOpen验证)和关联的URL。这是一个错误报告功能,因此arguments数组包含自动填充电子邮件字段的文本

启动这三个电子邮件客户端中的任何一个都没有问题,但gmail是唯一一个不处理断线的。gmail使用不同的方法吗

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")
        ]))
    }
}
1)将方案添加到您的信息中。plist

我们可以通过Info.plist文件来实现这一点。将名为LSApplicationQueriesSchemes的新密钥添加为数组。然后,您可以在阵列中输入应用程序。邮件应用不需要放在这里,大概是因为它是一个苹果应用。您的条目应如下所示


我们需要做的最后一件事是在将主题行传递到URL之前对其进行URL编码。我们可以通过调用subjectString?来实现这一点。在字符串上添加PercentEncoding(使用AllowedCharacterSet.urlQueryLowed)。

似乎使用“\r\n”而不是“\n”可以解决问题。

大小写。gmail:urlFormat=“go”oglegmail://co?to=%@&subject=%@&body=%@“用2/而不是3试试,直到没有变化,不幸的是他们!谢谢您的回答,但我仍然不确定为什么需要在plist中声明数组?它看起来不像是在任何地方使用的lsApplicationQueriesSchemes(Array-iOS)指定了您希望应用程序能够与UIApplication类的canOpenURL:方法一起使用的URL方案。对于您希望应用程序与canOpenURL:方法一起使用的每个URL方案,将其作为字符串添加到此数组中。阅读canOpenURL:method description了解有关声明支持的方案和使用该方法的重要信息。我目前已在最后一个字符串上使用canOpenURL来验证设备上有哪些电子邮件客户端可用。我的主要问题纯粹是gmail客户端无法识别嵌入在正文中的“%0A”。我正在尝试实现您所做的一些操作,但是xcode无法识别.urlQueryLowed,因此我不确定该怎么做好的,我通过键入来实现它。我有必要拥有openGmail openOutlook等功能吗?另外,你的答案是我们需要在主题行调用addingPercentEncoding方法,但我的主要问题是主体。主题行不需要任何换行符,而我的身体包含许多换行符。我是否需要对正文和主题分别应用内容编码,然后将整个内容围绕URL(字符串(格式:urlFormat,参数:[EMAIL\u RECIPIENT,EMAIL\u subject.applyPercentEncoding….,EMAIL\u body.applyPercentEncoding])进行包装,或者我可以只对其应用内容编码吗?这并没有回答问题“Gmail有没有办法识别换行符?”
func openGmail(withFrom: String?, withSubject: String?) {

    var gmailUrlString = "googlegmail:///"

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

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

}