how to check alert view displayed in xctest case in xcode [duplicate] - ios

This question already has answers here:
Dismissing alert XCUITest
(3 answers)
Closed 6 months ago.
I need to show alert on screen for any validation case and is working fine. But when working with XCTest I am not able to detect if alert is shown. How can I check UIAlertView or UIAlertController in XCTest case in Xcode. please give any suggestions

You can use addUIInterruptionMonitor to handle UIAlertController in XCTest.
let handler = addUIInterruptionMonitor(withDescription: "alert handler") { (alert: XCUIElement) -> Bool in
let ok = alert.buttons["OK"]
XCTAssertTrue(ok.exists, "OK button doesn't exist")
ok.tap()
}

Related

iOS 15 XCUITest addUIInterruptionMonitor

Had a few tests that broke after updating to iOS 15 and Xcode 13 because they are no longer able to dismiss alerts that pop up.
I have been using addUIInterruptionMonitor and had it working from the beginning of testing on my project.
addUIInterruptionMonitor(withDescription: "System Dialog") { alert -> Bool in
let dontAllowButton = alert.buttons.element(boundBy: 0)
if dontAllowButton.exists {
dontAllowButton.tap()
}
return true
}
app.tap()

IOS13 CNMutableContact Bug [duplicate]

This question already has answers here:
Keyboard overlaying action sheet in iOS 13.1 on CNContactViewController
(7 answers)
Closed 3 years ago.
I just try to call function with below code
func bugVersionPressed() {
let contact = CNMutableContact()
contact.familyName = "aaaa"
contact.givenName = "aaaa"
contact.organizationName = "bbbb"
let addContactVC = CNContactViewController(forNewContact: contact)
let navController = UINavigationController(rootViewController: addContactVC)
navController.view.backgroundColor = .red
navController.modalPresentationStyle = .fullScreen
self.present(navController, animated: true, completion: nil)
}
But the apple seems havent handle keyboard dismiss problem, I CAN'T DISMISS THE KEYBOARD AND I CAN'T GO BACK TO THE HOME PAGE BY PRESS "Cancel"
PIC1 : (Before present to CNContactViewController)
PIC2 : (Bug Case, Keyboard Cannot dismiss)
PIC3 : (Wish Case, Keyboard Dismiss while Clicked "Cancel")
In case I found a AppleStore App https://apps.apple.com/cn/app/%E5%BE%AE%E5%95%86%E5%8A%A0%E7%B2%89%E5%AE%9D-%E5%85%8D%E6%B3%A8%E5%86%8C%E6%B7%BB%E5%8A%A0%E6%B4%BB%E7%B2%89-%E8%81%94%E7%B3%BB%E4%BA%BA%E5%A5%BD%E5%8F%8B%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90/id475661774 but same problem. Please tell me what can I do. Many thanks.
The problem doesn't occur in iOS 13, but in iOS 13.1.
I think this is a problem with the CNContactViewController, not CNMutableContact.
I'm sorry, but I don't know how to avoid it because of Apple's framework.
Send a bug report to Apple.

How can I add an alert view [duplicate]

This question already has answers here:
How would I create a UIAlertView in Swift?
(36 answers)
Closed 7 years ago.
I would like to be able to add an alert view to my login screen that says Incorrect Username/ Password when the info submitted is incorrect. I am using Swift 2. I'm still new to learning code and I am unsure how to add this action.
Just use this code to display the alert:
func wrongLogin(input: String) {
var alert = UIAlertController(title: "Incorrect", message: "The \(input) is wrong. Try again.", preferredStyle: UIAlertControllerStyle.Alert)
//add a cancel button
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Cancel, handler: nil))
// Show it
showViewController(alert, sender: self)
}
Then when calling it just pass in if it's the username or the password.
You can do that like this wrongLogin("password") or wrongLogin("username")
For further info see this answer.
Hope that helps, Julian.

Sending sms swift in simulator [duplicate]

This question already has an answer here:
Difficulty obtaining capability to SMS via MFMessageComposeViewController in the iPhone simulator
(1 answer)
Closed 7 years ago.
If it possible to simulate sending sms in iPhone Simulator. I am not expecting to really send sms, just to show sms sending panel.
This is my code:
if MFMessageComposeViewController.canSendText() {
let messageVC = MFMessageComposeViewController()
messageVC.messageComposeDelegate = self
messageVC.recipients = ["1111111111", "2222222222"]
messageVC.body = "hello phone"
self.presentViewController(messageVC, animated: true, completion: nil)
} else {
print("Not using messages")
It always just print "Not using messages". Can someone advice, am I doing something wrong?
Thanks!
No it is not possible to show sms sending panel in iOS simulator. You'll have to register for Apple's developer program and test that on the device.

UIAlertView And UIAlertController

I am using SWIFT in xcode 6 to develope my application AND needs to support the APP for iphone 4 and later versions... So have selected the 'Deploment Target' as 7.1. In simple words needs to support iOS7, iOS8 AND iOS9...
When using Alert View I came across in many places discussing now we have to use newly introduced 'UIAlertController' rather than the old 'UIAlertView'...
By reading got to know UIAlertView is deprecated from ios 8.
But in my case as I have to support for ios 7.1 AND I can NOT only use 'UIAlertController'.
I started using as the following way as many tutorials explains...
if (objc_getClass("UIAlertController") != nil)
{
// Use UIAlertController
}
else
{
// Use UIAlertView
}
But in this way got to write the same code twice and really annoyiong... Either I have to create a custom Alertview combining both or needs to continue coding like this....
but just to test I've used only UIAlertView (ignoring UIAlertController) and the app runs fine even in ios 8 simulators... But the document says UIAlertView is deprecated from iOS 8.0...
So my question is, Like to hear what the best practice to continue my app with these API changes... Is it alright if I ignore 'UIAlertController' and work with old 'UIAlertView' until stop support for iOS7 one day... Will that effect in to my app in any way bad? Thanks
I've written a wrapper around these two classes which uses appropriate classes according to iOS version.
find it here
https://github.com/amosavian/ExtDownloader/blob/master/Utility%2BUI.swift
to show a simple alert use this:
Utility.UI.alertShow("message", withTitle: "title", viewController: self);
to show an action view: use this code:
let buttons = [Utility.UI.ActionButton(title: "Say Hello", buttonType: .Default, buttonHandler: { () -> Void in
print("Hello")
})]
Utility.UI.askAction("message", withTitle: "title", viewController: self, anchor: Utility.UI.AnchorView.BarButtonItem(button: uibarbuttontapped), buttons: buttons)
In case you are presenting action in iPhone, it will add a cancel button automatically. But you can have custom cancel button by defining a button with type of ".Cancel"
If you want show a modal alert view, use this code:
let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
print("OK button pressed")
})
Utility.UI.askAlert("message", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: nil)
As you can see, the text fields is set to nil above. You can set it if you want ask something from user, like this:
let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
print("You entered" + textInputs[0])
}
})
let textFields = [Utility.UI.AlertTextField(placeHolder: "enter secret text here", defaultValue: "", textInputTraits: TextInputTraits.secretInput())]
Utility.UI.askAlert("Enter password", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: textFields)
You can customize almost everything. e.g by defining custom textInputTrait you can customize text inputs easily. also you have not to deal with delegates, instead simple closures are there. Please read code to find out more.

Resources