messageComposeViewController 解除崩溃 swift 3
messageComposeViewController dismissing crash swift 3
在我的应用程序上,我通过短信发送警报。我尝试打开一个新的视图控制器作为模态,并在其中发送了警报。
但是,当发送短信或用户单击取消按钮时,messageComposeViewController 不会关闭,并会崩溃。
xcode 日志上的错误是:
(lldb)
这是我用来发送警报的代码:
import UIKit
import CoreLocation
import Social
import MessageUI
import BRYXBanner
class AlertInProgressViewController: UIViewController, MFMessageComposeViewControllerDelegate, CLLocationManagerDelegate {
[... Code here ...]
func sms()
{
//Send sms
if(sms_exist()==true)
{
if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways)
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
let mlocation = self.locationManager.location
if mlocation != nil {
let latitude: Double = mlocation!.coordinate.latitude
let longitude: Double = mlocation!.coordinate.longitude
let latitude_convert:String = String(format:"%f", latitude)
let longitude_convert:String = String(format:"%f", longitude)
let location = number_street + " " + ville + "\nLatitude " + latitude_convert + " - Longitude : " + longitude_convert
let geoCoder = CLGeocoder()
let location_details = CLLocation(latitude: mlocation!.coordinate.latitude, longitude: mlocation!.coordinate.longitude)
geoCoder.reverseGeocodeLocation(location_details)
{
(placemarks, error) -> Void in
let placeArray = placemarks as [CLPlacemark]!
// Place details
var placeMark: CLPlacemark!
placeMark = placeArray?[0]
// Address dictionary
print(placeMark.addressDictionary)
// Location name
if let locationName = placeMark.addressDictionary?["Name"] as? NSString
{
print(locationName)
self.details_location = locationName as String
}
if let city = placeMark.addressDictionary?["City"] as? NSString
{
self.city = city as String
}
self.message = NSLocalizedString("IN_DANGER_TEXT_SHARE",comment:"I'm in danger, I'm currently at ") + location + "\n - " + self.details_location + " - " + self.city
let defaults = UserDefaults.standard
let sms_perso = defaults.object(forKey: "sms") as? String
if(MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = self.message
controller.recipients = [sms_perso!]
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)
}
else
{
print("Can't send sms")
}
}
}
}
}
}
[...]
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
//Original code but doesn't work too
//self.dismiss(animated: true, completion: nil)
OperationQueue.main.addOperation {
self.dismiss(animated: true, completion: {});
}
print("I want to dismiss here")
}
}
在 xcode 日志中,我可以看到:我想在此处关闭,因此调用了 messageComposeViewController 但在它崩溃之后。
为了显示 AlertInProgressViewController,我使用了故事板 segue。
我通过改变这个解决了我的问题:
self.dismiss(animated: true, completion: {});
来自
controller.dismiss(animated: true, completion: nil)
是的,在 Swift 4.x 中你必须使用
controller.dismiss(animated: true, completion: nil)
这是 MFMailComposeViewControllerDelegate
的有用委托函数
// MAIL COMPOSER DELEGATE
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
var resultMess = ""
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue:
resultMess = "Mail cancelled"
case MFMailComposeResult.saved.rawValue:
resultMess = "Mail saved"
case MFMailComposeResult.sent.rawValue:
resultMess = "Thanks for contacting us!\nWe'll get back to you asap."
case MFMailComposeResult.failed.rawValue:
resultMess = "Something went wrong with sending Mail, try again later."
default:break
}
// Show email result alert
let alert = UIAlertController(title: APP_NAME, message: resultMess, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
})
alert.addAction(ok)
present(alert, animated: true, completion: nil)
// Dismiss the controller
controller.dismiss(animated: true, completion: nil)
}
在我的应用程序上,我通过短信发送警报。我尝试打开一个新的视图控制器作为模态,并在其中发送了警报。 但是,当发送短信或用户单击取消按钮时,messageComposeViewController 不会关闭,并会崩溃。
xcode 日志上的错误是:
(lldb)
这是我用来发送警报的代码:
import UIKit
import CoreLocation
import Social
import MessageUI
import BRYXBanner
class AlertInProgressViewController: UIViewController, MFMessageComposeViewControllerDelegate, CLLocationManagerDelegate {
[... Code here ...]
func sms()
{
//Send sms
if(sms_exist()==true)
{
if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways)
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
let mlocation = self.locationManager.location
if mlocation != nil {
let latitude: Double = mlocation!.coordinate.latitude
let longitude: Double = mlocation!.coordinate.longitude
let latitude_convert:String = String(format:"%f", latitude)
let longitude_convert:String = String(format:"%f", longitude)
let location = number_street + " " + ville + "\nLatitude " + latitude_convert + " - Longitude : " + longitude_convert
let geoCoder = CLGeocoder()
let location_details = CLLocation(latitude: mlocation!.coordinate.latitude, longitude: mlocation!.coordinate.longitude)
geoCoder.reverseGeocodeLocation(location_details)
{
(placemarks, error) -> Void in
let placeArray = placemarks as [CLPlacemark]!
// Place details
var placeMark: CLPlacemark!
placeMark = placeArray?[0]
// Address dictionary
print(placeMark.addressDictionary)
// Location name
if let locationName = placeMark.addressDictionary?["Name"] as? NSString
{
print(locationName)
self.details_location = locationName as String
}
if let city = placeMark.addressDictionary?["City"] as? NSString
{
self.city = city as String
}
self.message = NSLocalizedString("IN_DANGER_TEXT_SHARE",comment:"I'm in danger, I'm currently at ") + location + "\n - " + self.details_location + " - " + self.city
let defaults = UserDefaults.standard
let sms_perso = defaults.object(forKey: "sms") as? String
if(MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = self.message
controller.recipients = [sms_perso!]
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)
}
else
{
print("Can't send sms")
}
}
}
}
}
}
[...]
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
//Original code but doesn't work too
//self.dismiss(animated: true, completion: nil)
OperationQueue.main.addOperation {
self.dismiss(animated: true, completion: {});
}
print("I want to dismiss here")
}
}
在 xcode 日志中,我可以看到:我想在此处关闭,因此调用了 messageComposeViewController 但在它崩溃之后。
为了显示 AlertInProgressViewController,我使用了故事板 segue。
我通过改变这个解决了我的问题:
self.dismiss(animated: true, completion: {});
来自
controller.dismiss(animated: true, completion: nil)
是的,在 Swift 4.x 中你必须使用
controller.dismiss(animated: true, completion: nil)
这是 MFMailComposeViewControllerDelegate
// MAIL COMPOSER DELEGATE
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
var resultMess = ""
switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue:
resultMess = "Mail cancelled"
case MFMailComposeResult.saved.rawValue:
resultMess = "Mail saved"
case MFMailComposeResult.sent.rawValue:
resultMess = "Thanks for contacting us!\nWe'll get back to you asap."
case MFMailComposeResult.failed.rawValue:
resultMess = "Something went wrong with sending Mail, try again later."
default:break
}
// Show email result alert
let alert = UIAlertController(title: APP_NAME, message: resultMess, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
})
alert.addAction(ok)
present(alert, animated: true, completion: nil)
// Dismiss the controller
controller.dismiss(animated: true, completion: nil)
}