Skip to content

Commit

Permalink
Merge pull request #13 from angeria/no-compose-mode
Browse files Browse the repository at this point in the history
Add ability to open mail apps without compose mode
  • Loading branch information
vtourraine authored Oct 16, 2019
2 parents 957a195 + fceb248 commit 745e787
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ThirdPartyMailer changelog

## 1.6 (work in progress)

- Add open client method


## 1.5

- Add Swift Package Manager support
Expand Down
12 changes: 11 additions & 1 deletion Sources/ThirdPartyMailer/ThirdPartyMailClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,15 @@ public struct ThirdPartyMailClient {
return URLComponents().url!
}
}
}

/**
Returns the open URL for the mail client, based on its custom URL scheme.
- Returns: A `URL` opening the mail client.
*/
public func openURL() -> URL {
var components = URLComponents()
components.scheme = URLScheme
return components.url!
}
}
13 changes: 13 additions & 0 deletions Sources/ThirdPartyMailer/ThirdPartyMailer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ open class ThirdPartyMailer {
return application.canOpenURL(URL)
}


/**
Opens a third-party mail client.
- Parameters application: The main application (usually `UIApplication.sharedApplication()`).
- Parameters client: The third-party client to test.
- Returns: `true` if the application opens the client; otherwise, `false`.
*/
open class func application(_ application: UIApplicationOpenURLProtocol, openMailClient client: ThirdPartyMailClient) -> Bool {
return application.openURL(client.openURL())
}

/**
Opens a third-party mail client in compose mode.
Expand Down
21 changes: 21 additions & 0 deletions Tests/ThirdPartyMailerTests/ThirdPartyMailClientsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let sparrow = clientWithURLScheme("sparrow")
XCTAssertNotNil(sparrow)

_ = ThirdPartyMailer.application(application, openMailClient: sparrow!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "sparrow:")

_ = ThirdPartyMailer.application(application, openMailClient: sparrow!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "sparrow:")

Expand All @@ -51,6 +54,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let gmail = clientWithURLScheme("googlegmail")
XCTAssertNotNil(gmail)

_ = ThirdPartyMailer.application(application, openMailClient: gmail!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "googlegmail:")

_ = ThirdPartyMailer.application(application, openMailClient: gmail!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "googlegmail:///co")

Expand All @@ -65,6 +71,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let dispatch = clientWithURLScheme("x-dispatch")
XCTAssertNotNil(dispatch)

_ = ThirdPartyMailer.application(application, openMailClient: dispatch!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "x-dispatch:")

_ = ThirdPartyMailer.application(application, openMailClient: dispatch!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "x-dispatch:///compose")

Expand All @@ -79,6 +88,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let spark = clientWithURLScheme("readdle-spark")
XCTAssertNotNil(spark)

_ = ThirdPartyMailer.application(application, openMailClient: spark!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "readdle-spark:")

_ = ThirdPartyMailer.application(application, openMailClient: spark!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "readdle-spark://compose")

Expand All @@ -93,6 +105,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let airmail = clientWithURLScheme("airmail")
XCTAssertNotNil(airmail)

_ = ThirdPartyMailer.application(application, openMailClient: airmail!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "airmail:")

_ = ThirdPartyMailer.application(application, openMailClient: airmail!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "airmail://compose")

Expand All @@ -107,6 +122,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let outlook = clientWithURLScheme("ms-outlook")
XCTAssertNotNil(outlook)

_ = ThirdPartyMailer.application(application, openMailClient: outlook!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "ms-outlook:")

_ = ThirdPartyMailer.application(application, openMailClient: outlook!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "ms-outlook://compose")

Expand All @@ -121,6 +139,9 @@ class ThirdPartyMailClientsTests: XCTestCase {
let yahoo = clientWithURLScheme("ymail")
XCTAssertNotNil(yahoo)

_ = ThirdPartyMailer.application(application, openMailClient: yahoo!)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "ymail:")

_ = ThirdPartyMailer.application(application, openMailClient: yahoo!, recipient: nil, subject: nil, body: nil)
XCTAssertEqual(application.lastOpenedURL?.absoluteString, "ymail://mail/compose")

Expand Down

0 comments on commit 745e787

Please sign in to comment.