I'm trying to create UI tests for my app using EarlGrey 2.0 framework while using Swift language for those tests. However, I can't find a solution for tapping on a system alert, although EG 2.0 should support them. To be more specific, it is the native location iOS permission dialog when the app launches. Has anyone done this already? Any ideas? Thank you.
Here's the full code of a test accepting native system alert in Swift
func testExample() {
let app = XCUIApplication()
app.launch()
XCTAssertTrue(grey_wait(forAlertVisibility: true, withTimeout: 2))
XCTAssertTrue(grey_acceptSystemDialogWithError(nil))
XCTAssertTrue(grey_wait(forAlertVisibility: false, withTimeout: 1))
}
Related
I try to use KIF in swift project. I run test case on iPhone simulator. Probably I did not set up correctly KIF because use it first time. I used this manual
Test fails in this simple code
func testSelectingOrganizerRole() {
tester().tapView(withAccessibilityLabel: "ORGANIZE")
}
with reason:
A button with Accessibility label "ORGANIZE" exists on initial ViewConroller of storyboard.
Why don't you switch to the UI tests framework available since Xcode 7? A quick intro:
UI testing gives you the ability to find and interact with the UI of your app in order to validate the properties and state of the UI elements.
UI testing includes UI recording, which gives you the ability to generate code that exercises your app's UI the same way you do, and which you can expand upon to implement UI tests. This is a great way to quickly get started writing UI tests.
Using this framework, your simple test would look like this:
let app = XCUIApplication()
app.launch()
app["ORGANIZE"].tap()
Is it possible to enable Bluetooth (with Discoverability on), using a button on macOS? Or enable it when an application opens? I've looked around online for a solution but everything directs towards iPhone and iOS Development rather than anything on the Mac side. Everything that is geared towards iOS also states that it is not possible on the mobile devices, but it is possible to display an alert notifying the user to turn on their Bluetooth to use their accessories. Is any alert type possible?
Edit: The closest I could get to this was opening the Bluetooth pane of the System Preferences programmatically.
It's a private API.
Add the following to your bridging header:
void IOBluetoothPreferenceSetControllerPowerState(int);
And call it with 1 to enable or 0 to disable:
func setBluetooth(on: Bool) {
IOBluetoothPreferenceSetControllerPowerState(on ? 1 : 0)
}
setBluetooth(on: true)
Don't forget to import IOBluetooth in your Swift file.
To get the current status use int IOBluetoothPreferenceGetControllerPowerState();
I haven't been able to find anything about this in Swift. Is there a way to programmatically make my application minimize all other windows open in the background, or even just minimize Safari? I basically want my application to run against the desktop, without any clutter in the background. Is there a way to programmatically do this for a Cocoa app? I'm pretty new to swift, so any help would be appreciated.
You can use api on NSWorkspace which allows you to hide all visible app in the background.
You can find more about NSWorkspace here: link
Hides all applications other than the sender. This method must be called from your app’s main thread.
NSWorkspace.shared().hideOtherApplications()
If you only want to hide Safari,
let appPath = NSWorkspace.shared().fullPath(forApplication: "Safari")
let identifier = Bundle.init(path: appPath!)?.bundleIdentifier
if let bundleID = identifier {
let runningApps = NSRunningApplication.runningApplications(withBundleIdentifier:bundleID )
if !runningApps.isEmpty {
runningApps.first?.hide()
}
}
**I have read the RevMob instructions for Swift and I have read an answer to an Swift-RevMob question on here, but it didn't resolve my particular issue. **
I am currently trying to get a fullscreen ad to show.
This is my GameViewController.swift:
verride func viewDidLoad() {
super.viewDidLoad()
//Start RevMob code
let completionBlock: () -> Void = {
// do something when it successfully starts the session
RevMobAds.session().showFullscreen();
}
let errorBlock: (NSError!) -> Void = {error in
// check the error
println(error);
}
RevMobAds.startSessionWithAppID("55770fcc17dd7840727aa5e8",
withSuccessHandler: completionBlock, andFailHandler: errorBlock);
//End of RevMob Code
I have defined the modules in my package as "Yes":
This is my Bridge-Header:
// Use this file to import your target's public headers that you would like to expose to Swift.
#import <RevMobAds/RevMobAds.h>
Lastly, this is the error that I am currently getting when I try to build my code:
Thanks if you can help!
CLBeaconRegion is part of Core Location from iOS 7 on, so you need to make sure the framework is linked in your project.
Note: in my experience, CLBeaconRegion is picked up by Apple as part of app review and they often want to know how you're using beacons – even if the code is just sitting there and you're not planning to use it.
Twice now I have been asked by app review to provide a video of my app detecting a beacon so they can see how it works, so either be prepared to explain your usage or see if there's a library version without it built in.
In addition to the required frameworks, you have to add CoreLocation.framework
I had the same issue. With CoreLocation.framework, also add AVFoundation.framework. Hope that helps
I'm trying to programmatically return to the home screen in an iOS8 App using Swift. I want to continue the application running in the background though. Any ideas on how to do this?
Thanks in advance for the help.
When an app is launched, the system calls the UIApplicationMain function; among its other tasks, this function creates a singleton UIApplication object. Thereafter you access the object by calling the sharedApplication class method.
To exit gracefully (the iOS developer library explicitly warns you not to use exit(0) because this is logged as a crash ) you can use:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
For example, my app exits when the user shakes the device. So, in ViewController.swift:
override func motionEnded(motion: UIEventSubtype,
withEvent event: UIEvent?) {
if motion == .MotionShake{
//Comment: to terminate app, do not use exit(0) bc that is logged as a crash.
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
}}
Swift 4:
UIControl().sendAction(#selector(NSXPCConnection.suspend),
to: UIApplication.shared, for: nil)
Edit: It's worth mentioning that in iOS 12 there's a bug that will prevent network connectivity if the app is brought back from background after sending the suspend action.
For that you should use following code
import Darwin
exit(0)
To force your app into the background, you can legally launch another app, such as Safari, via a URL, into the foreground.
See: how to open an URL in Swift3
UIApplication.shared.open() (and the older openURL) are a documented public APIs.
If you set the exits-on-suspend plist key, opening another app via URL will also kill your app. The use of this key is a documented legal use of app plist keys, available for any app to "legally" use.
Also, if your app, for some impolite reason, continues to allocate and dirty large amounts of memory in the background, without responding to memory warnings, the OS will very likely kill it.
How about setting of info.plist?
You can set "Application does not run in background" true in info.plist with editor.
Or Add this lines with code editor.
<key>UIApplicationExitsOnSuspend</key>
<true/>
There is no way to "programmatically return to the home screen" except for crashing, exiting your program or calling unofficial API. Neither is welcome by Apple. Plus the Human Interface Guidelines are also covering this.