Hi I am writing the following code to refer a friend through SMS.
When I click on cell, the sms app opens with text but when again I tried for second time, it shows white color screen.
Here is my code
var controller1 = MFMessageComposeViewController()
extension ReferaFriendController:UICollectionViewDelegate,UICollectionViewDataSource,MFMessageComposeViewControllerDelegate
{
if indexPath.item == 0
{
if MFMessageComposeViewController.canSendText() {
let urlToShare = self.referalmodeldata[0].referralCodeOnly
controller1.body = "Hey I just gave an Awesome Assessment on App you can also try it. I scored , Try to beat my score \(String(describing: urlToShare))"
controller1.messageComposeDelegate = self
self.present(controller1, animated: true, completion: nil)
}
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
self.dismiss(animated: true, completion: nil)
}
}
As far as I can see, there's no need to keep a reference to the MFMessageComposeViewController. Just move it to be created at the point you need it, inside your if closure:
if MFMessageComposeViewController.canSendText() {
let controller = MFMessageComposeViewController()
// ...
}
Related
I have set up UIVideoEditorController as followed
if UIVideoEditorController.canEditVideo(atPath: video.path) {
let editController = UIVideoEditorController()
editController.videoPath = video.path
editController.delegate = self
present(editController, animated:true)
}
It will end up in the cancel delegate call and then dismisses the editor
func videoEditorControllerDidCancel(_ editor: UIVideoEditorController) {
print("in here")
editor.dismiss(animated: true, completion: nil)
}
Does anyone know why this is or if I need additional configurations to get it to work?
whenever the user presses on a logo, a counter will count from 0 to 3. after these 3 seconds, I am presenting the message controller for the user:
if MFMessageComposeViewController.canSendText() == true {
print(self.urgentNumber)
let recipients:[String] = ["\(self.urgentNumber as! String)"]
self.messageController.messageComposeDelegate = self as?
MFMessageComposeViewControllerDelegate
self.messageController.recipients = recipients
self.messageController.body = "Hey,\nmy longitude: \
(self.userLocation.coordinate.longitude) \nmy latitude: \
(self.userLocation.coordinate.latitude)"
self.present(self.messageController, animated: true,
completion: nil)
} else {
//handle text messaging not available
}
when the user presses cancel or send the message, I am detecting this in :
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
messageController.dismiss(animated: true, completion: nil)
}
here the user should wait 20 sec to send another message:
DispatchQueue.main.asyncAfter(deadline: .now() + 20, execute: {
self.reSetAnimations()
})
The problem is, When the user presses on the logo the second time( After waiting the 20 sec to give him the ability to press again), the message controller is not opening again!
any idea why?
Try to create a new instance of the MFMessageComposeViewController instead of reusing the old one.
I'm trying to develop an app, that take a number from a variable then open in "Messages" app with (1500) in the field To: and the variable value in Text Messages field like this
I tried this answer how to open an URL in Swift3 and Swift: How to open a new app when uibutton is tapped but i didn't figure out the URL for Messages app
what should I use? Big thanks.
use this
if MFMessageComposeViewController.canSendText() == true{
let recipients:[String] = ["1500"]
var messageController = MFMessageComposeViewController()
//messageController.messageComposeDelegate = self // implement delegate if you want
messageController.recipients = recipients
messageController.body = "Your_text"
self.present(messageController, animated: true, completion: nil)
}
You need to import "MessageUI" to your class and use the below code.
func sendMessages() {
if MFMessageComposeViewController.canSendText() == true {
let recipients:[String] = ["9895249619"]
let messageController = MFMessageComposeViewController()
messageController.messageComposeDelegate = self
messageController.recipients = recipients
messageController.body = "Your_message_text"
self.present(messageController, animated: true, completion: nil)
} else {
//handle text messaging not available
}
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}
With manipulation of this tutorial http://www.raywenderlich.com/113772/uisearchcontroller-tutorial
I have a table view displaying people, when the cell is clicked the user is redirected to another view showing their picture and their email. I want to be able to have the user click on the email address and email them. I have researched and found similar tutorials
https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/
The problem with the tutorial above is when test running the new code the ios simulator pops up an error and wont show the composed email (Maybe a Glitch?)and If the simulator did not give an error I dont know how to display multiple emails based on which person the user selected. Any help on solutions to this problem or any alternative will be great Thanks!!
The simulator will crash when you try to open the mail. Try it on an actual device instead.
To compose a mail do the following
Add these to your class
MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate
In your didSelectRowAtIndexPath, this is the function called when you presses a row in your tableView. Do the following:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let candy = candies[indexPath.row]
var mail: MFMailComposeViewController!
// yourArray is the array that you use to populate the tableView
// .mail is the variable in the object (I´m assuming you´re using objects in your array)
let toRecipients = [candy[indexPath.row].email]
let subject = "Feedback"
let body = "<br><br><p>I have a \(UIDevice.currentDevice().modelName).<br> And iOS version \(UIDevice.currentDevice().systemVersion).<br</p>"
mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(toRecipients)
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: true)
presentViewController(mail, animated: true, completion: nil)
}
The delegate methods below if you need to use them
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
dismissViewControllerAnimated(true, completion: nil)
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
switch (result.rawValue) {
case MessageComposeResultCancelled.rawValue:
self.dismissViewControllerAnimated(true, completion: nil)
case MessageComposeResultFailed.rawValue:
self.dismissViewControllerAnimated(true, completion: nil)
case MessageComposeResultSent.rawValue:
self.dismissViewControllerAnimated(true, completion: nil)
default:
break;
}
}
I submitted my app to the app store, but they rejected it saying that the app crashed when they clicked the feedback button which is a button that opens up a MFMailComposeViewController. The problem I am having is I have run it on many devices between the simulator and actual devices, yet I have never had this problem. I will post my functions for the feedback button below which I have called and are all connected to the button (Like I said it works completely fine every time I have tested it), and my question is: Am i doing something wrong in the code to where only they get the crash?
func giveFeedback()
{
let email = ["info#website.com"]
var fvc = view?.window?.rootViewController
var cev = MFMailComposeViewController()
cev.mailComposeDelegate = self
cev.setToRecipients(email)
cev.setSubject("MyApp")
fvc?.presentViewController(cev, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)
{
controller.dismissViewControllerAnimated(true, completion: nil)
}
Also, I have imported MessageUI, and in the class I have my MFMailComposeViewControllerDelegate
One thing is that you don't call canSendMail. I believe that if you try to show the MFMailComposeViewController when mails are disabled, your app would crash.
In your function you would use it for example like this:
func giveFeedback(contextViewController: UIViewController) {
if MFMailComposeViewController.canSendMail() {
let email = ["info#website.com"]
var cev = MFMailComposeViewController()
cev.mailComposeDelegate = self
cev.setToRecipients(email)
cev.setSubject("MyApp")
contextViewController.presentViewController(cev, animated: true, completion: nil)
}
}
But it would be best to check the status earlier and display the button only if email is enabled on the device...
Well I will have to guess too.... There are two things that look suspicious to me... the first thing is the line
var fvc = view?.window?.rootViewController
could you change it this way?
func giveFeedback(contextViewController: UIViewController) {
let email = ["info#website.com"]
var cev = MFMailComposeViewController()
cev.mailComposeDelegate = self
cev.setToRecipients(email)
cev.setSubject("MyApp")
contextViewController.presentViewController(cev, animated: true, completion: nil)
}