How to send an email without opening the mail composer iOS swift? - ios

I am working iOS swift project. I want to send a mail with app itself. When the user clicks the submit button inside the app the mail needs to send in background or pop will be present inside the app without navigating into the mail composer.
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients([ApplicationModel.sharedInstance.mailAddress])
mail.setSubject("Feedback: \(UserDefaults.Id)")
mail.setMessageBody(sendBpodyMessage(), isHTML: false)
present(mail, animated: true)
} else {
if let emailUrl = createEmailUrl(to: ApplicationModel.sharedInstance.mailAddress,
subject: "Feedback", body: sendBpodyMessage()) {
UIApplication.shared.open(emailUrl)
}
}
I refer so many answers that all were saying there is no way and apple did not have this feature. Is it possible or any other way Folks?
Most of them said to use API send into the own server. I will try it if I don’t have any option related to the above question.

Apple will not allow you to send email from the user's account without displaying a mail composer window to the user (for security reasons.) You will have to implement your own mail sending mechanism from your server if you want this (and still won't be able to send email from the user's account.)
It's not that "Apple does not have this feature." It's that it would be a huge security hole. Apple explicitly blocks third parties from sending emails from the user's account except using a mail composer, and WILL NEVER ALLOW IT.
Think about the potential for abuse if it was allowed. A spammer could release a "trojan horse" app like "flappy bird" for free: A fun, popular game. Millions of people download it. Unbeknownst to them, it starts sending out emails from their accounts, attempting to defraud their friends, or the online community at large

Related

Does changing the default email app affect MFMailComposeViewController usage?

iOS 14 users can change the default email app. What effect, if any, does this have on MFMailComposeViewController? More specifically, it would be ideal if MFMailComposeViewController "just worked" with whatever the default email client was, for example setting recipients, subject, body, etc.
If that isn't possible, I suppose opening a mailto URL will open whatever the default mail app is. Any way to test this before iOS 14 releases?
iOS 14 and its ability to set a default Mail app did not change anything in regards to MFMailComposeViewController API. It is only capable of displaying Mail's compose sheet, so canSendMail() will still return false when they're not using the Mail app.
To better support users who choose to use a different email app, you could open a mailto URL. This will open the default email app and bring up their compose sheet. If no email app is installed, it will present a system alert asking the user if they want to restore Mail from the App Store (unless running in the Simulator). This API doc explains how you can create the URL, including how to specify a subject, body, and additional recipients.
Note that this will leave your app to open the Mail app or other email app. If you'd like to keep users in your app when they're using Mail, you can continue to use MFMailComposeViewController and fall back to mailto when canSendMail() returns false.
If you would like, you could additionally check if you can open the mailto: URL, and if not, present your own messaging to the user. Note this would require you add mailto to your LSApplicationQueriesSchemes in the Info.plist.
I found this post to be helpful as well.
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients([email])
mail.setSubject(subject)
present(mail, animated: true, completion: nil)
} else {
if let mailURLString = "mailto:\(email)?subject=\(subject)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let mailURL = URL(string: mailURLString) {
if UIApplication.shared.canOpenURL(mailURL) { //check not needed, but if desired add mailto to LSApplicationQueriesSchemes in Info.plist
view.window?.windowScene?.open(mailURL, options: nil, completionHandler: nil)
} else {
//maybe they like web apps? 🤷‍♂️
//maybe let them copy the email address to the clipboard or something
}
}
}

How to implement "Contact Us" view with a default Email destination address

I am trying to build up a Contact Us View in one iOS project, What I am trying to do is:
Put a Text Viewin a view controller, which will capture and save users' writing.
If users press send button, the view controller will send the Email or message to customer service. How to implement this or any SDK recommended?
Thanks in advance.
MFMailComposeViewController is fully dedicated for this. it has built in what you are trying to make. So don't write code to take this hassle. Let Apple do it for you. :)
place the uitextview on the storyboard, create an IBoutlet for your textview, and implement the uitextview delegate methods.
You can then use "shouldChangeTextInRange" to determine if the user has pressed enter.
You are going to need to hit a backend API to then send off the data through your mail server. if you don't have a backend server where you can code up a seamless email sending program, you can open up the mail app by using something like this:
let email = "foo#bar.com"
if let url = URL(string: "mailto:\(email)") {
UIApplication.shared.open(url)
}

SLComposeViewController won't set initialText for SLServiceTypeFacebook

It seems as a bug in Social framework, it won't set the initial text for Facebook service type. This is my implementation using Swift:
let view = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
if view.setInitialText("Some text") {
NSLog("Success")
} else {
NSLog("Failure")
}
view.addURL(NSURL(string: "https://google.ro"))
self.presentViewController(view, animated: true, completion: nil)
What it works:
The URL shows up for both, Facebook, and Twitter
The initialText works correctly just for Twitter
What is doesn't work:
The initialText won't show up for Facebook, even though the code above outputs "Success"
Update 2
It seems like Facebook really doesn't want developers to set the initial text of a message when Sharing as that it's impossible even with their SDK. In the app I was implementing I've chosen screenshots from the app (uploaded as images) for letting the user share their progress on Facebook.
Update
The solution is to use the Facebook SDK for iOS, that feature of Social framework is not supported by Facebook anymore.
Not a bug, Facebook doesn't allow it anymore.
Use of the iOS share sheet is subject to Facebook Platform Policy, including section 2.3 which states that apps may not pre-fill. In the context of the share sheet, this means apps may not pre-fill the share sheet's initialText field with content that wasn't entered by people earlier in their use of the app.
Facebook dose't allow to pre fill message anymore.
For more info here
Saying that, if you use native Facebook sdk it won't solve your problem, because Facebook will not publish your app.
So as written on Facebook guidelines, the only solutions that you have are:
Populate the user message parameter with content that the user manually entered earlier in your app's flow.
Your app’s composer can include a call-to-action that disappears when people start to write a post. For example, Facebook's composer uses grey scale text to ask “What's on your mind?” that disappears when people start to write.
I think it is an iOs 8.3 bug.
Before I updated to that version it was working correctly. :-(

How to pass text to 3rd party app in iOS

My app generates some sort of text information.
User presses button like "Share" in my app and after that pops up a windows with a list of installed applications or only apps which can recieve string parameter. After that, user selects, for example, "Mail" app and then it is opened with the new email message and with a given text from my app. Or user selects Skype app and then it is opened with a given text.
How could those scenarios be implemented in iOS?
PS: I already saw similar behavior in Android app (via Intent extras).
UPDATE: I posted the answer below that works for me (via UIActivityViewController) exactly how I need.
There is no single answer that will work for all target apps. You need to research each app and see if it has a facility for receiving info from other apps.
A simple way to do this is to invoke an URL that targets the other app.
For mail, you could invoke a mailto:// URL that composes an email with the text in the desired field(s) (to, cc, bcc, subject, or body.)
If the app supports the iOS document model you may be able to pass it a document to open.
If the target app has a server then you may also be able to connect to the server and send data to it that way. Again, this is not a question you can ask in general. The answer will be different for each target app, and for some apps the answer will be "you can't, because it doesn't have any mechanism to receive data from an outside app."
Android is a different beast with different abilities than iOS. iOS is more of a "walled garden", with very limited access outside of your app.
I found the best solution for me is
- (IBAction)onShare:(id)sender {
NSString *textStr = self.textToShare.text;
NSArray *items = #[textStr];
UIActivityViewController *activity = [[UIActivityViewController alloc]
initWithActivityItems:items
applicationActivities:nil];
[self presentViewController:activity animated:YES completion:nil];
}
It does exactly what I need. It shows popup view with the list of apps which are able to receive text string. Then user can select any of them and controller sends text to it.

How to send background email after button press?

I'm building an internal app. I want to be able to press a button, and have the app automatically send a predefined email message to a specified email address, without the user knowing. I have access to a web-server, but I'm just not quite sure on what the best way to go about doing this is.
I'm using storyboard in xcode, this is a singleview application for the ipad.
Any suggestions are greatly appreciated.
You can create a PHP script that can do this (code below). Use a library such as ASIHTTPRequest to post the user's email address to the script and then the script will automatically send the message.
<?php
$to = $_POST["email"]; //this is the user's address; you can replace $_POST["email"] with "user#example.com" to try it out
$subject = "Subject";
$body = "Message";
$headers = "From: Name <noreply#example.com>\r\n" . "X-Mailer: php";
if (mail($to, $subject, $body, $headers)) {
//sent
}
?>
If you want to use the user's mail account that they have set up in the mail.app - you can't do that without the MFMailComposeViewController.
Solutions are:
Use some Framework or roll your own mail solution that you or the user fills with their respective mail acc data and then send mails.
or
Write a little PHP/Ruby/Java/... script that sends a mail which you can trigger via web request (i.e. REST).
SKPSMTPMessage works well for sending emails without the need for a UI.
(Make sure you add a reference to the CFNetwork.framework in your project.)

Resources