Dismiss a ShortcutViewController after setting it - ios

I Implements SiriShortcut with Intent in my App.
I set up a button "Add to Siri" with the code that Apple give here : https://developer.apple.com/documentation/sirikit/inuiaddvoiceshortcutviewcontroller
func addSiriButton(to view: UIView) {
let button = INUIAddVoiceShortcutButton(style: .blackOutline)
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
button.addTarget(self, action: #selector(addToSiri(_:)), for: .touchUpInside)
}
// Present the Add Shortcut view controller after the
// user taps the "Add to Siri" button.
#objc
func addToSiri(_ sender: Any) {
if let shortcut = INShortcut(intent: ProjectorOnIntent()) {
let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
viewController.modalPresentationStyle = .formSheet
viewController.delegate = self as? INUIAddVoiceShortcutViewControllerDelegate // Object conforming to `INUIAddVoiceShortcutViewControllerDelegate`.
present(viewController, animated: true, completion: nil)
}
}
But once I record a phrase when I click on the "Ok" button the view won't dismiss.
The error I got in the console :
2018-10-12 10:16:51.985156+0200 AppName[1029:172350] [default] No results found for query: {(
<_LSApplicationIsInstalledQuery: 0x28226e560>
)}
2018-10-12 10:16:51.989467+0200 AppName[1029:172263] [strings] ERROR: Add to Siri not found in
table Localizable of bundle CFBundle 0x111a01d00 </var/containers/Bundle/Application/DDADF244-FBCE-47C0-90F8-E8C8ADA6962E/AppName.app> (executable, loaded)

hope it help you,
class ViewController: UIViewController, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate {
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
controller.dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
Use INUIAddVoiceShortcutViewControllerDelegate and INUIEditVoiceShortcutViewControllerDelegate
Thank you.

Related

MFMessageViewController turns to white screen when presented

I have presented MFMessageViewController with following code.
extension TransactionResultViewController {
func showMessageComposeController() {
if MFMessageComposeViewController.canSendText() {
let messageComposeVc = MFMessageComposeViewController()
messageComposeVc.body = viewModel.offlinePayload
messageComposeVc.recipients = ["37245"]
messageComposeVc.messageComposeDelegate = self
self.present(messageComposeVc, animated: true, completion: nil)
}
}
}
and dismissed with following code.
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}
Sometimes the MFMessageviewcontroller is not presented and sometimes it shows blank white/black screen. Please help. Thanks

Can't get app to dismiss the mail compose view controller & return to first screen

I'm using the Swift book to try to learn to code. I added the delegate method to dismiss the view, but it's not working. What am I missing here?
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}
}
You just need to declare the delegate function outside of the IBAction:
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}

Implementing MailComposeController in tableView

I have created the setting page in TableView. When I tap row 0 UIActivityViewController is called. When I tap row2 MailComposeController is called.
MailComposeController is called, but when I tap cancel or send button on mail screen it does not work.
Here is the code。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let message = "Hey download my app [LINK]"
let shareView = UIActivityViewController(activityItems: [message], applicationActivities: nil)
self.present(shareView, animated: true, completion: nil)
} else if indexPath.row == 1 {
let mailCompose = MFMailComposeViewController()
mailCompose.mailComposeDelegate = self
mailCompose.setToRecipients(["address#gmail.com"])
mailCompose.setSubject("feedback")
mailCompose.setMessageBody("text", isHTML: false)
if MFMailComposeViewController.canSendMail()
{
self.present(mailCompose, animated: true, completion: nil)
}
else{
print("error...!")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
You have to put the delegate method in the ViewController but not in the tableView:didSelectRowAt method:
extension ViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
Implement the delegate methods of Mail composer on click of send and cancel button:
Here are the Delegates methods which you need to implement :
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: {
switch (result)
{
case .cancelled:
break
case .saved:
break;
case .sent:
AppUtility.showAlert(message: kEmailSentSuccessMessage, isSuccess: true)
break;
case .failed:
AppUtility.showAlert(message: kEmailSentFailureMessage, isSuccess: true)
break;
}
})
}
Use breakpoint to see what exactly going on the click of send or cancel. and make sure that you have created the MailComposer object locally . since this object is going to send and cancel the mail each time .

There's a bug in CNContactPickerController

After spending all day (>12 hours) trying to isolate a bug in 13 lines of mind-bogglingly generic code, I have come to the dubious conclusion that there must be a bug in the current iteration of CNContactPickerViewController, in iOS 9.2.
Simply copy+paste this ViewController and link the invite action to a button.
The bug is that MFMessageComposeViewController dismisses itself immediately.
If anybody knows what to do with this, do share?
import UIKit
import MessageUI
import ContactsUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate, CNContactPickerDelegate {
let contactPickerVC = CNContactPickerViewController()
let messageVC = MFMessageComposeViewController()
override func viewDidLoad() {
super.viewDidLoad()
contactPickerVC.delegate = self
}
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
if let phoneNumberValue = contact.phoneNumbers.first?.value as? CNPhoneNumber {
if let phoneNumber = phoneNumberValue.valueForKey("digits") as? String {
// Configure message ViewController
messageVC.messageComposeDelegate = self
messageVC.recipients = [phoneNumber]
messageVC.body = "Yoyoyo"
picker.presentViewController(messageVC, animated: true, completion: nil)
}
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func invite(sender: AnyObject) {
presentViewController(contactPickerVC, animated: true, completion: nil)
}
}
I got it working by dismissing the pickerVC and changing the controller which presents the messageVC!
Insert (before the messageVC config lines):
picker.dismissViewControllerAnimated(true, completion: nil)
Replace
picker.presentViewController(messageVC, animated: true, completion: nil)
with
presentViewController(messageVC, animated: true, completion: nil)

show UIAlertController outside of ViewController

I have trouble to display my UIAlertController because I'm trying to show it in a Class which is not an ViewController.
I already tried adding it:
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Which is not working...
I didn't find any solution that worked for me yet.
I wrote this extension over UIAlertController to bring back show().
It uses recursion to find the current top view controller:
extension UIAlertController {
func show() {
present(animated: true, completion: nil)
}
func present(animated: Bool, completion: (() -> Void)?) {
if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
presentFromController(controller: rootVC, animated: animated, completion: completion)
}
}
private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
if
let navVC = controller as? UINavigationController,
let visibleVC = navVC.visibleViewController
{
presentFromController(controller: visibleVC, animated: animated, completion: completion)
} else if
let tabVC = controller as? UITabBarController,
let selectedVC = tabVC.selectedViewController
{
presentFromController(controller: selectedVC, animated: animated, completion: completion)
} else if let presented = controller.presentedViewController {
presentFromController(controller: presented, animated: animated, completion: completion)
} else {
controller.present(self, animated: animated, completion: completion);
}
}
}
Now it's as easy as:
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
alertController.show()
This should work.
UIApplication.sharedApplication().windows[0].rootViewController?.presentViewController(...)
Create a helper function that you call from the current view controller and pass the current view controller as a parameter:
func showAlertInVC(
viewController: UIViewController,
title: String,
message: String)
{
//Code to create an alert controller and display it in viewController
}
If you solution is not working it probably because of there is no window at that moment. I had the same problem when I was trying to show alert view in application:DidFinishLoadingWithOptions method. In this case my solution was to check if root view controller is available, and if it's not, then add notification for UIApplicationDidBecomeActiveNotification
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification,
object: nil,
queue: NSOperationQueue.mainQueue()) {
(_) in
//show your alert by using root view controller
//remove self from observing
}
}

Resources