iOS URL Scheme Microsoft Outlook App

落花浮王杯 提交于 2019-12-29 07:42:11

问题


This seems impossible to find, unless perhaps there isn't one for it. But anyone know (if there is one) the iOS URL Scheme for opening the Microsoft Outlook Mobile App right to the Compose Screen with pre-defined TO_EMAIL, SUBJECT and BODY?


回答1:


Here is a link I found that helped me out with the IOS Outlook URL Scheme.

From that I was able to come up with this code:

// Create an array of recipients for the email.
NSArray* emailRecipients = @[@"example@email.com", @"example2@email.com"];

// Create a mutable string to hold all of the recipient email addresses and add the first one.
NSMutableString* emailTo = [[NSMutableString alloc] initWithString:emailRecipients[0]];
// Loop through all of the email recipients except for the first one.
for (int index = 1; index < emailRecipients.count; index++)
{
    // Add a semicolon and then the email address at the current index.
    [emailTo appendFormat:@";%@", emailRecipients[index]];
}

// Get the email subject from the subject text field.
NSString* emailSubject = fieldSubject.text;
// Encode the string for URL.
NSString* encodedSubject = [emailSubject stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
// Get the email body from the body text field.
NSString* emailBody = fieldBody.text;
// Encode the string for URL.
NSString* encodedBody = [emailBody stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

// See if the subject or body are empty.
if (![emailSubject length] || ![emailBody length])
{
    // Exit.
    return;
}

// Create a string with the URL scheme and email properties.
NSString *stringURL = [NSString stringWithFormat:@"ms-outlook://compose?to=%@&subject=%@&body=%@", emailTo, encodedSubject, encodedBody];
// Convert the string to a URL.
NSURL *url = [NSURL URLWithString:stringURL];
// Open the app that responds to the URL scheme (should be Outlook).
[[UIApplication sharedApplication] openURL:url];

The URL scheme for Outlook is: ms-outlook://compose?to=example@email.com&subject=Subject&body=Message

Hope this helps!




回答2:


Swift

func outlookDeepLink(subject: String, body: String, recipients: [String]) throws -> URL {

    enum MailComposeError: Error {
        case emptySubject
        case emptyBody
        case unexpectedError
    }

    guard !subject.isEmpty else { throw MailComposeError.emptySubject }
    guard !body.isEmpty else { throw MailComposeError.emptyBody }

    let emailTo = recipients.joined(separator: ";")

    var components = URLComponents()
    components.scheme = "ms-outlook"
    components.host = "compose"
    components.queryItems = [
        URLQueryItem(name: "to", value: emailTo),
        URLQueryItem(name: "subject", value: subject),
        URLQueryItem(name: "body", value: body),
    ]

    guard let deepURL = components.url else { throw MailComposeError.unexpectedError }
    return deepURL
}

Usage

try! UIApplication.shared.open(
    outlookDeepLink(
        subject: "subject",
        body: "body",
        recipients: ["example@email.com", "example2@email.com"]
    )
)

Note that:

  1. Don't forget to tell iOS you are going to call ms-outlook. (Add it to LSApplicationQueriesSchemes in info.plist, Otherwise, You will get an clear error message in console if you forget it)

  2. Also don't forget to check if the app actually exists before trying to open the url. (canOpenURL is here to help)



来源:https://stackoverflow.com/questions/33190891/ios-url-scheme-microsoft-outlook-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!