Dismiss parseUI login view from a SKScene - ios

I am using ParseUI inside a SpriteKit Swift app.
This is my LoginScene
class LoginScene: SKScene, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {
func showLoginView() {
let nativeLogin = NativeLoginViewController()
nativeLogin.fields = PFLogInFields.Default
nativeLogin.delegate = self
self.view?.window?.rootViewController?.presentViewController(nativeLogin, animated: true, completion: nil)
}
func logInViewController(logInController: PFLogInViewController!, didLogInUser user: PFUser!) {
println("login succeeded")
self.view?.window?.rootViewController?.dismissViewControllerAnimated(true, completion: {() in
println("dismissed loginviewcontroller suceeded")
self.view?.presentScene(ModeMenuScene(size: self.view!.bounds.size))
})
}
}
}
It does print login succeeded but not dismissed loginviewcontroller succeeded. How can I dismiss the loginview upon login and go back to my LoginScene ?

This question isn't related to Parse. Instead I'd look into better handling the dismissViewControllerAnimated() method.
Try switching:
self.view?.window?.rootViewController?.dismissViewControllerAnimated...
to
self.dismissViewControllerAnimated...

Found the solution : I need to make use of the first parameter instead of relying on the delegate controller.
func logInViewController(logInController: PFLogInViewController!, didLogInUser user: PFUser!) {
logInController.dismissViewControllerAnimated(true, completion: {() in
self.view?.presentScene(GameScene(size: self.view!.bounds.size)) //Go to Game Scene
return
})
}

Related

In Cognito on iOS, handling new password required doesn't ever reach didCompleteNewPasswordStepWithError

I'm trying to implement functionality to respond to FORCE_CHANGE_PASSWORD on my iOS app that uses AWS Cognito. I used this Stack Overflow question which references this sample code. Right now, my code opens a view controller like it's supposed to; however, once on that view controller, I can't get it to do anything. In the sample code, it seems that when you want to submit the password change request you call .set on an instance of AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>, yet when I do this, the protocol function didCompleteNewPasswordStepWithError is never called. Interestingly, the other protocol function getNewPasswordDetails is called quickly after viewDidLoad and I can't tell why. I believe this shouldn't be called until the user has entered their new password, etc and should be in response to .set but I could be wrong.
My code is pretty identical to the sample code and that SO post, so I'm not sure what's going wrong here.
My relevant AppDelegate code is here:
extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired {
//assume we are presenting from login vc cuz where else would we be presenting that from
DispatchQueue.main.async {
let presentVC = UIApplication.shared.keyWindow?.visibleViewController
TransitionHelperFunctions.presentResetPasswordViewController(viewController: presentVC!)
print(1)
}
var vcToReturn: ResetPasswordViewController?
returnVC { (vc) in
vcToReturn = vc
print(2)
}
print(3)
return vcToReturn!
}
//put this into its own func so we can call it on main thread
func returnVC(completion: #escaping (ResetPasswordViewController) -> () ) {
DispatchQueue.main.sync {
let storyboard = UIStoryboard(name: "ResetPassword", bundle: nil)
let resetVC = storyboard.instantiateViewController(withIdentifier: "ResetPasswordViewController") as? ResetPasswordViewController
completion(resetVC!)
}
}
}
My relevant ResetPasswordViewController code is here:
class ResetPasswordViewController: UIViewController, UITextFieldDelegate {
#IBAction func resetButtonPressed(_ sender: Any) {
var userAttributes: [String:String] = [:]
userAttributes["given_name"] = firstNameField.text!
userAttributes["family_name"] = lastNameField.text!
let details = AWSCognitoIdentityNewPasswordRequiredDetails(proposedPassword: self.passwordTextField.text!, userAttributes: userAttributes)
self.newPasswordCompletion?.set(result: details)
}
}
extension ResetPasswordViewController: AWSCognitoIdentityNewPasswordRequired {
func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>) {
self.newPasswordCompletion = newPasswordRequiredCompletionSource
}
func didCompleteNewPasswordStepWithError(_ error: Error?) {
DispatchQueue.main.async {
if let error = error as? NSError {
print("error")
print(error)
} else {
// Handle success, in my case simply dismiss the view controller
SCLAlertViewHelperFunctions.displaySuccessAlertView(timeoutValue: 5.0, title: "Success", subTitle: "You can now login with your new passowrd", colorStyle: Constants.UIntColors.emeraldColor, colorTextButton: Constants.UIntColors.whiteColor)
self.dismiss(animated: true, completion: nil)
}
}
}
}
Thank you so much for your help in advance and let me know if you need any more information.

How do I prevent a navigationController from returning to root when dismissing MFMailComposeViewController

When I dismiss an instance of MFMailComposeViewController or MFMessageComposeViewController that is presented modally from the third viewController in a navigation stack, the navigation stack is reset, and the root VC is reloaded. How can I prevent this behavior and remain on the original presenting viewController (third VC in the stack)? I get the same behavior whether I call dismiss from the presenting VC, the presented VC, or the navigationController.
This has been asked before, but I have not seen a solution.
App Structure looks like this:
TabBarController
Tab 1 - TripsNavController
-> Trips IntroductionVC (root VC) segue to:
-> TripsTableViewController segue to:
-> TripEditorContainerVC
- TripEditorVC (child of ContainerVC)
- HelpVC (child of ContainerVC)
Tab 2...
Tab 3...
Tab 4...
In the TripEditorVC I present the MFMailComposeViewController. The functions below are declared in an extension to UIViewController that adopts the MFMailComposeViewControllerDelegate protocol
func shareWithEmail(message: NSAttributedString) {
guard MFMailComposeViewController.canSendMail() else {
showServiceError(message: "Email Services are not available")
return
}
let composeVC = MFMailComposeViewController()
composeVC.setSubject("My Trip Plan")
composeVC.setMessageBody(getHTMLforAttributedString(attrStr: message), isHTML: true)
composeVC.mailComposeDelegate = self
present(composeVC, animated: true, completion: nil)
}
Then in the delegate method I dismiss the MFMailComposeVC:
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .sent:
print("Mail sent")
case .saved:
print("Mail saved")
case .cancelled:
print("Mail cancelled")
case .failed:
print("Send mail failed")
}
if error != nil {
showServiceError(message: "Error: \(error!.localizedDescription)")
}
dismiss(animated: true, completion: nil)
}
I have tried the following to present and dismiss and get the same behavior, i.e.: the TripsNavController clears the nav stack and reloads the TripsIntroductionVC as its root VC:
self.present(composeVC, animated: true, completion: nil)
self.parent?.present(composeVC, animated: true, completion: nil)
self.parent?.navigationController?.present(composeVC, animated: true, completion: nil)
self.navigationController?.present(composeVC, animated: true, completion: nil)
You can also check with presentingViewController?.dismiss method to get the solution.
I have tried with following navigation stack.
I am able to send email successfully from Container VC's Send Email button using your code only.
Can you please check and verify navigation flow?
Please let me know if you still face any issue.
dismiss(animated: true, completion: nil)
to
self.dismiss(animated: true, completion: nil)
Try this
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("secondViewControllerId") as! SecondViewController
self.presentViewController(secondViewController, animated: true, completion: nil)
or
https://stackoverflow.com/a/37740006/8196100
Just Use Unwind Segue its Pretty simple and perfect in your case
I have used many times..
just look at this Unwind segue's example
. If u still don't able to find answer or not able to understand the Unwind segue then just reply to me.
Hope this will solve your problem.
I found the problem with my navigation stack today. I built a simple
project that duplicated the tabBarController/NavigationControler architecture of my problem project, component by component, until dismissing a MFMailComposeViewController caused my navigation stack to reset as described in my original post.
That immediately pointed to the source of the bug. In my subclassed UINavigationCotroller I was instantiating the root viewController in code so that I could skip an introductory view if the user had set a switch in the apps settings. In order to pick up changes in that switch setting I was calling my instantiation code in viewDidAppear of the navigationController. That worked fine EXCEPT when dismissing the mailComposeVC. The fix was to add a guard statement in viewDidAppear to return if the navControllers viewController collection was not empty, and send and respond to an NSNotification when the switch was changed.
class TopNavigationController: UINavigationController {
var sectionType: SectionType?
var defaults = UserDefaults.standard
var showIntroFlag: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
// Handle initial load of the tab bar controller where we are not sent a sectionType
if sectionType == nil {
sectionType = .groups
}
setShowIntroFlag()
NotificationCenter.default.addObserver(self, selector: #selector(resetControllers), name: NSNotification.Name(rawValue: "kUserDidChangeShowIntros"), object: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
guard self.viewControllers.isEmpty else {
return
}
loadControllers()
}
func setShowIntroFlag() {
showIntroFlag = true
// Check NSUserDefaults to see if we should hide the Intro Views for all sections
if defaults.bool(forKey: "SHOW_SECTION_INTROS") == false {
showIntroFlag = false
}
}
func loadControllers() {
if showIntroFlag == true {
showIntro()
} else {
skipIntro()
}
}
func resetControllers() {
setShowIntroFlag()
loadControllers()
}

Navigate to a UIViewController from a UIAlertView in Swift 2.3

I am new to Swift and iOS development. I have been trying to create an app-wide check for internet connectivity, that allows the user to retry (reload the current view). The checkInternetConnection() function below is called during viewWillAppear() in my BaseUIViewController class.
The Reachability.isConnectedToNetwork() connectivity-check works fine. But so far, I have not been able to figure out a way to reload the current view after the user presses the 'Retry' button in the alert. in fact, I have not been able to figure out a way to reload the current view in any scenario.
One thing I know I am doing wrong in the following attempt (since there is a compile error telling me so), is that I am passing a UIViewController object as a parameter instead of a string on this line: self.performSegueWithIdentifier(activeViewController, sender:self), but I suspect that this is not my only mistake.
func checkInternetConnection() {
if (Reachability.isConnectedToNetwork()) {
print("Internet connection OK")
} else {
print("Internet connection FAILED")
let alert = UIAlertController(title: NSLocalizedString("Error!", comment: "Connect: error"), message: NSLocalizedString("Could not connect", comment: "Connect: error message"), preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Retry", comment: "Connect: retry"), style: .Default, handler: { action in
print("Connection: Retrying")
let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
let activeViewController: UIViewController = navigationController.visibleViewController!
self.performSegueWithIdentifier(activeViewController, sender:self)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
BaseUIViewController can perform app-wide check for connectivity, so all your ViewControllers will be inherited from BaseUIViewController.
And each ViewController will have different behaviour after connectivity check.
One thing you can do is, in your BaseUIViewController you can define a block that performs action after connectivity check failed.
Here's the example:
class BaseViewUIController: UIViewController {
var connectivityBlock: (() -> Void)?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
checkInternetConnection()
}
func checkInternetConnection() {
if Reachability.isConnectedToNetwork() {
print("Internet connection OK")
} else {
if let b = connectivityBlock {
b()
}
}
}
}
And in your inherited ViewControllers:
class MyViewController: BaseUIViewController {
override func viewDidLoad() {
super.viewDidLoad()
connectivityBlock = {
print("Do View Refresh Here.")
}
}
}
Then After the check in BaseUIViewController, the code in connectivityBlock will be executed so that each VC can deal with it differently.
You can also define a function in BaseUIViewController and each subclass will override that function, after connectivity check failed, call the function.
Try navigate using application's windows
let destinationVC = UIApplication.shared.delegate?.window
destinationVC .present(activeViewController, animated: true, completion: nil)

CNContactViewController Cancel Button Not Working

I'm trying to use the built-in new contact UI and am getting unexpected behavior with the cancel button. The code below works and calls up the new contact screen but the cancel button will only clear the screen entries not cancel out of the new contact screen. In the built in contacts app hitting cancel returns to the contact list screen. I would like the cancel button to close out the window.
#IBAction func newTwo(sender: AnyObject) {
AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
if accessGranted {
let npvc = CNContactViewController(forNewContact: nil)
npvc.delegate = self
self.navigationController?.pushViewController(npvc, animated: true)
}
}
}
did you implement CNContactViewControllerDelegate methods?
Here's a link to documentation
for example:
func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
It worked for me using the following code:
Swift 3
func contactViewController(_ vc: CNContactViewController, didCompleteWith con: CNContact?) {
vc.dismiss(animated: true)
}
Also I changed the way I was calling the controller:
Instead of:
self.navigationController?.pushViewController(contactViewController, animated: true)
the solution was:
self.present(UINavigationController(rootViewController: contactViewController), animated:true)
I found the solution using the example code Programming-iOS-Book-Examples written by Matt Neuburg:
Better way to do the dismissing would be to check if the contact is nil and then dismiss it. The dismiss doesn't work if you've pushed the view controller from a navigation controller. You might have to do the following:
func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
if let contactCreated = contact
{
}
else
{
_ = self.navigationController?.popViewController(animated: true)
}
}

how to wait for ui delegate in swift

I want to create simple object helper to sending sms in many places on my Swift 2.0 app, and in next step another helpers (email, pdf opener etc)
I create simple class:
import Foundation
import MessageUI
class SmsHelper: MFMessageComposeViewControllerDelegate {
func sendSMS(body: String){
if (MFMessageComposeViewController.canSendText()){
let messageVC = MFMessageComposeViewController()
messageVC.body = body
//messageVC.recipients = ["Enter tel-nr"]
messageVC.messageComposeDelegate = self;
AppDelegate().sharedInstance().getTopController().presentViewController(messageVC, animated: false, completion: nil)
}
else{
//do some alert etc.
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult){
sendSMSRsp(result, errorMsg: nil)
controller.dismissViewControllerAnimated(true, completion: nil)
print("sms didFinishWithResult")
}
}
in anywhere in code I want to do that:
class someAnotherClass{
func someFunction(){
let smsHelper = SmsHelper()
smsHelper.sendSms("some text")
}
}
so sms ios editor is opened, but when I want to close it or sending, it don't dismiss, function messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) i never called and app crashes with memory leak, I know reason: is because SmsHelper object in 'someFunction' is deleting after end scope of this function, and this object is nil, and system try to call didFinishWithResult at nil object. I confirm it: when I add smsHelper object as member of 'SomeClass' it worked - delegate is called.
Question is: What is the best practice to do that, adding a member is not an option for me, because many classes can use that, also creating a singleton, appDelegate member is I think quite stupid. How to force not deleting a object at end of scope function?
Try to use singleton way to do it:
class SmsHelper: MFMessageComposeViewControllerDelegate {
static let sharedInstance = SmsHelper()
private init() {}
}
And usage:
SmsHelper.sharedInstance().sendSms('some text')
EDIT:
If you add different init method from abstract class.
private override init(message: String){
super.init()
}
then change definition to static let sharedInstance = SmsHelper(message:"smsHelper") won't complicated.
For your delegate, just assign it out of the init method, in the UIViewController viewDidLoad,
SmsHelper.sharedInstance().delegate = self
For me, the way to do this is to create a type alis of a closure, then storing an optional variable of this type alias. When you call sendSMS, you pass in a closure and set it to your variable. Then that can be called in the delegate, which will then pass it through the closure of sendSMS:
import Foundation
import MessageUI
class SmsHelper: MFMessageComposeViewControllerDelegate {
let messageVC: MFMessageComposeViewController!
typealias SMSCompletion = (result: MessageComposeResult, errorMsg: NSError?) -> ()
var sendSMSRsp: SMSCompletion?
func sendSMS(body: String, completion: SMSCompletion){
sendSMSRsp = completion
if (MFMessageComposeViewController.canSendText()){
messageVC = MFMessageComposeViewController()
messageVC.body = body
//messageVC.recipients = ["Enter tel-nr"]
messageVC.messageComposeDelegate = self;
AppDelegate().sharedInstance().getTopController().presentViewController(messageVC, animated: false, completion: nil)
} else {
//do some alert etc.
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult){
sendSMSRsp?(result: result, errorMsg: nil)
controller.dismissViewControllerAnimated(true, completion: nil)
print("sms didFinishWithResult")
}
}
class SomeOtherClass {
func someFunction() {
let smsHelper = SmsHelper()
smsHelper.sendSMS("some text") { (result, errorMsg) -> () in
}
}
}

Resources