From 23ca901f148730b49c06543efe844868afeeff8b Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 4 May 2023 05:26:28 -0700 Subject: [PATCH] ios: mailto url open --- .../Onboarding/CreateSimpleXAddress.swift | 22 +++++++-- .../Views/UserSettings/UserAddressView.swift | 46 ++++++++++++++++++- apps/ios/SimpleX--iOS--Info.plist | 7 +++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/apps/ios/Shared/Views/Onboarding/CreateSimpleXAddress.swift b/apps/ios/Shared/Views/Onboarding/CreateSimpleXAddress.swift index 0962813d6..2dfeeab71 100644 --- a/apps/ios/Shared/Views/Onboarding/CreateSimpleXAddress.swift +++ b/apps/ios/Shared/Views/Onboarding/CreateSimpleXAddress.swift @@ -36,11 +36,14 @@ struct CreateSimpleXAddress: View { shareQRCodeButton(userAddress) .frame(maxWidth: .infinity) + Spacer() + if MFMailComposeViewController.canSendMail() { - Spacer() - shareViaEmailButton(userAddress) .frame(maxWidth: .infinity) + } else { + shareViaEmailToButton(userAddress) + .frame(maxWidth: .infinity) } Spacer() @@ -170,6 +173,15 @@ struct CreateSimpleXAddress: View { } } + private func shareViaEmailToButton(_ userAddress: UserContactLink) -> some View { + Button { + UserAddressView.shareUserAddressViaMailTo(userAddress) + } label: { + Label("Invite friends", systemImage: "envelope") + .font(.title2) + } + } + private func continueButton() -> some View { Button { withAnimation { @@ -190,14 +202,14 @@ struct SendAddressMailView: View { var userAddress: UserContactLink var body: some View { - let messageBody = """ + let messageBody = NSLocalizedString("""

Hi!

Connect to me via SimpleX Chat

- """ + """, comment: "") MailView( isShowing: self.$showMailView, result: $mailViewResult, - subject: "Let's talk in SimpleX Chat", + subject: NSLocalizedString("Let's talk in SimpleX Chat", comment: ""), messageBody: messageBody ) } diff --git a/apps/ios/Shared/Views/UserSettings/UserAddressView.swift b/apps/ios/Shared/Views/UserSettings/UserAddressView.swift index 134eeb969..2fc9b89b0 100644 --- a/apps/ios/Shared/Views/UserSettings/UserAddressView.swift +++ b/apps/ios/Shared/Views/UserSettings/UserAddressView.swift @@ -40,7 +40,7 @@ struct UserAddressView: View { } } } - + var body: some View { ZStack { if viaCreateLinkView { @@ -192,9 +192,13 @@ struct UserAddressView: View { Section { QRCode(uri: userAddress.connReqContact) shareQRCodeButton(userAddress) + if MFMailComposeViewController.canSendMail() { shareViaEmailButton(userAddress) + } else { + shareViaEmailToButton(userAddress) } + shareWithContactsButton() autoAcceptToggle() learnMoreButton() @@ -286,6 +290,16 @@ struct UserAddressView: View { } } + private func shareViaEmailToButton(_ userAddress: UserContactLink) -> some View { + Button { + UserAddressView.shareUserAddressViaMailTo(userAddress) + } label: { + settingsRow("envelope") { + Text("Invite friends") + } + } + } + private func autoAcceptToggle() -> some View { settingsRow("checkmark") { Toggle("Auto-accept", isOn: $aas.enable) @@ -434,6 +448,36 @@ struct UserAddressView: View { } } } + + static func shareUserAddressViaMailTo(_ userAddress: UserContactLink) { + guard + let subject = String(format: NSLocalizedString("Let's talk in SimpleX Chat", comment: "")).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), + let addressStr = userAddress.connReqContact.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed.subtracting(CharacterSet(charactersIn: "&"))), + let bodyWithoutAddress = (String(format: NSLocalizedString("Hi!\nConnect to me via SimpleX Chat:", comment: "")) + " ").addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) + else { return } + + let body = bodyWithoutAddress + addressStr + + let gmailUrl = URL(string: "googlegmail://co?subject=\(subject)&body=\(body)") + let outlookUrl = URL(string: "ms-outlook://compose?subject=\(subject)&body=\(body)") + let yahooUrl = URL(string: "ymail://mail/compose?subject=\(subject)&body=\(body)") + let sparkUrl = URL(string: "readdle-spark://compose?subject=\(subject)&body=\(body)") + var selectedUrl: URL + + if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) { + selectedUrl = gmailUrl + } else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) { + selectedUrl = outlookUrl + } else if let yahooUrl = yahooUrl, UIApplication.shared.canOpenURL(yahooUrl) { + selectedUrl = yahooUrl + } else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) { + selectedUrl = sparkUrl + } else { + return + } + + UIApplication.shared.open(selectedUrl, options: [:]) + } } struct UserAddressView_Previews: PreviewProvider { diff --git a/apps/ios/SimpleX--iOS--Info.plist b/apps/ios/SimpleX--iOS--Info.plist index 6f7d6c239..2a944f850 100644 --- a/apps/ios/SimpleX--iOS--Info.plist +++ b/apps/ios/SimpleX--iOS--Info.plist @@ -2,6 +2,13 @@ + LSApplicationQueriesSchemes + + googlegmail + ms-outlook + readdle-spark + ymail + BGTaskSchedulerPermittedIdentifiers chat.simplex.app.receive