Launch Apple Mail App from within my own App?

后端 未结 15 2179
心在旅途
心在旅途 2020-11-29 01:59

What I already found is

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"mailto:\"]];

But I just want to open the Mail ap

相关标签:
15条回答
  • 2020-11-29 02:24

    Swift version of the original Amit's answer:

    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)
        }
    }
    

    Swift 3.0:

    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)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:26
    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]];
    
    0 讨论(0)
  • 2020-11-29 02:27

    Swift 4 / 5 to open default Mail App without compose view. If Mail app is removed, it automatically shows UIAlert with options to redownload app :)

    UIApplication.shared.open(URL(string: "message:")!, options: [:], completionHandler: nil)
    
    0 讨论(0)
  • 2020-11-29 02:28

    In Swift:

    let recipients = "someone@gmail.com"
    let url = NSURL(string: "mailto:\(recipients)")
    UIApplication.sharedApplication().openURL(url!)
    
    0 讨论(0)
  • 2020-11-29 02:29

    If you are using Xamarin to developer an iOS application, here is the C# equivalent to open the mail application composer view:

    string email = "yourname@companyname.com";
    NSUrl url = new NSUrl(string.Format(@"mailto:{0}", email));
    UIApplication.SharedApplication.OpenUrl(url);
    
    0 讨论(0)
  • 2020-11-29 02:32

    on swift 2.3: open mailbox

    UIApplication.sharedApplication().openURL(NSURL(string: "message:")!)
    
    0 讨论(0)
提交回复
热议问题