Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 从我自己的应用程序中启动Apple Mail应用程序?_Ios_Email - Fatal编程技术网

Ios 从我自己的应用程序中启动Apple Mail应用程序?

Ios 从我自己的应用程序中启动Apple Mail应用程序?,ios,email,Ios,Email,我已经找到的是 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]]; 但我只想打开邮件应用程序,而不仅仅是composer视图。仅邮件应用程序处于正常或上次状态 有什么想法吗?因为启动其他应用程序的唯一方法是使用它们的URL方案,所以打开邮件的唯一方法是使用mailto:方案。不幸的是,在您的情况下,它总是会打开compose视图。如果您使用Xamarin开发iOS应用程序,下面是打开mail

我已经找到的是

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
但我只想打开邮件应用程序,而不仅仅是composer视图。仅邮件应用程序处于正常或上次状态


有什么想法吗?

因为启动其他应用程序的唯一方法是使用它们的URL方案,所以打开邮件的唯一方法是使用mailto:方案。不幸的是,在您的情况下,它总是会打开compose视图。

如果您使用Xamarin开发iOS应用程序,下面是打开mail application composer视图的C#等价物:

NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";

NSString *body = @"&body=It is raining in sunny California!";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];

email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
NSURL* mailURL = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

在真实设备上运行应用程序并拨打

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your@email.com"]];
请注意,该行对模拟器没有影响。

在Swift中:

let recipients = "someone@gmail.com"
let url = NSURL(string: "mailto:\(recipients)")
UIApplication.sharedApplication().openURL(url!)

扩展Amit的答案: 这将启动邮件应用程序,并启动新的电子邮件。只需编辑字符串即可更改新电子邮件的开始方式

//put email info here:
NSString *toEmail=@"supp0rt.fl0ppyw0rm@gmail.com";
NSString *subject=@"The subject!";
NSString *body = @"It is raining in sunny California!";

//opens mail app with new email started
NSString *email = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@", toEmail,subject,body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

显然,邮件应用程序支持第二个url方案-
message://
,如果应用程序获取了特定的邮件,则允许打开该邮件。如果不提供邮件url,则只会打开邮件应用程序:

NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

你可以在iOS上启动任何应用程序,只要你知道它的URL方案。不知道邮件应用程序方案是公开的,但您可以偷偷尝试以下方法:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"message:message-id"]];

法哈德·努尔扎伊把我卷入这件事的道具。这是邮件应用程序API的反向工程。更多信息请点击此处:

Amit原始答案的Swift版本:

Swift 2:

func openMailApp() {

    let toEmail = "stavik@outlook.com"
    let subject = "Test email".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()
    let body = "Just testing ...".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()

    if let
        urlString = ("mailto:\(toEmail)?subject=\(subject)&body=\(body)")),
        url = NSURL(string:urlString) {
        UIApplication.sharedApplication().openURL(url)
    }
}
func openMailApp() {

    let toEmail = "stavik@outlook.com"
    let subject = "Test email".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

    if let
        urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)",
        url = URL(string:urlString) {
        UIApplication.shared().openURL(url)
    }
}
Swift 3.0:

func openMailApp() {

    let toEmail = "stavik@outlook.com"
    let subject = "Test email".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()
    let body = "Just testing ...".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()

    if let
        urlString = ("mailto:\(toEmail)?subject=\(subject)&body=\(body)")),
        url = NSURL(string:urlString) {
        UIApplication.sharedApplication().openURL(url)
    }
}
func openMailApp() {

    let toEmail = "stavik@outlook.com"
    let subject = "Test email".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let body = "Just testing ...".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

    if let
        urlString = "mailto:\(toEmail)?subject=\(subject)&body=\(body)",
        url = URL(string:urlString) {
        UIApplication.shared().openURL(url)
    }
}

在swift 2.3上:打开邮箱

UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)

您可以使用url方案打开邮件应用程序,而无需打开compose视图。您可能需要使用脚本桥。我在我的应用程序中使用此方法,直接向用户提供使用内置的
mail.App
发送电子邮件通知的选项。我还构建了一个选项,作为备用选项直接通过SMTP执行此操作

但是,由于您想使用
Mail.app
方法,您可以通过以下方式找到有关如何执行该解决方案的更多信息:


它将使用composer视图打开默认邮件应用程序:

string email = "yourname@companyname.com";
NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
UIApplication.SharedApplication.OpenUrl(url);
NSURL* mailURL = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

它将打开默认邮件应用程序:

NSURL* mailURL = [NSURL URLWithString:@"message://"];
if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
    [[UIApplication sharedApplication] openURL:mailURL];
}

Swift 4/5打开默认邮件应用程序,无需撰写视图。 如果删除邮件应用程序,它会自动显示UIAlert,并提供重新下载应用程序的选项:)

Swift 5版本:

if let mailURL = URL(string: "message:") {
    if UIApplication.shared.canOpenURL(mailURL) {
        UIApplication.shared.open(mailURL, options: [:], completionHandler: nil)
    }
}

您尝试过imap或pop而不是mailto吗?这将在撰写视图中打开。在iOS 11中不起作用。有人能帮我吗?@HardikAmal iOS 11:问题是
stringbyaddingpercentescapesusingencode
正在编码所有特殊字符(包括
mailto
后面的冒号)。解决方案是找到一个更好的编码函数,或者去掉编码行,用%20替换
主题
正文
@cschuff中的空格来手动编码消息,即使现在看来是这样。我刚刚又浏览了一次URL启动机制和我能找到的任何相关信息。仍然限于在打开撰写视图的情况下启动。除非未配置邮件帐户,否则在这种情况下,它将以默认的“欢迎使用邮件”屏幕打开邮件,显示iCloud、Exchange、Google、Yahoo!,Aol、Outlook.com和其他请查看成功回答问题的备选答案:+1,注意这在模拟器上不起作用,或者想知道Slack应用程序是否使用此技术。他们在没有邮件组成视图的情况下启动这应该是可以接受的答案。目前公认的答案是,这是不可能的,但使用“message//”确实可以在没有撰写屏幕的情况下打开电子邮件应用程序。很高兴找到这个!您知道我们是否可以使用该URL方案,以便邮件应用程序打开一个电子邮件文件(.msg from Outlook或类似文件),提供文件所在的URL?比如说
message://http://domain/file.msg
@AlexMM,我还没有试过,但若你们有邮件url+附件url,那个么它可能会根据邮件应用程序工作,只会打开已经在邮件应用程序中下载的邮件。但问题是,是否有其他方法可以打开某个域中托管的邮件。谢谢这个版本实际上不适合我。这里似乎只有主题和正文应该根据苹果的文档进行编码:因此答案中正确的
urlString
应该是
let urlString=“mailto:\(toEmail)?subject=\(subject.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())&body=\(body.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)
。但此代码可以重构为更好的格式和更安全的代码。这在iOS 11中仍然有效吗?这只是为我打开邮件,而不是特定的消息。欢迎提供指向解决方案的链接,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户将了解它是什么以及为什么存在,然后引用最相关的在目标页面不可用的情况下链接到的页面的vant部分。