Using Xcode 7.3, writing Swift program; searched for this topic but could not find an answer. Apologies, in advance. Last program language was COBOL.
Application background:
It is a single view application, User keys in data and at some point the User 'wins' - at that point, two buttons are available: 'Quit?' and 'Play Again?'
For the 'Quit?' button I have:
// MARK: C.This IBAction used to quit the game
#IBAction func quitIt(sender: AnyObject)
{
exit(0)
}
Hopefully, the above is the most efficient way.
What I need help on is the 'Play Again?' button:
// TODO: B. This IBAction used to play another game
#IBAction func Playagain(sender: AnyObject)
{
}
What code do I put in?
Move the startup code for your game to it's own func(tion).
Call that function both from [wherever you moved it from] and also in PlayAgain().
I hope this is for OS X rather than iOS - Apple will probably reject your iOS app if it exits. They consider that "crashing".
https://developer.apple.com/library/ios/qa/qa1561/_index.html
Related
I am following this Getting Started guide for building an iOS app using the AWS Amplify CLI and the AWS SDK for iOS.
And I had previously followed the steps in this Apple Getting Started guide for simply creating the basic framework for a Single View Application.
Everything works without a hitch: I was able to build my empty project in Xcode, launch the simulator, see my white blank screen, both before and after starting the AWS iOS SDK Swift tutorial.
My problem is that the AWS tutorial presumes more Swift knowledge than I have. So when it says the following toward the end—
Call the runMutation(), runQuery(), and subscribe() methods from your
app code, such as from a button click or when your app starts in
viewDidLoad().
—the guide has essentially skipped some steps.
I've already created the required AWS resources for this tutorial but I don't know how to call the functions and update the DynamoDB table that gets set up.
Assuming I can add two text fields to the UI View (one for the ToDo 'name' and one for the 'description') and tie a button to them, can someone help me go the rest of the way?
UPDATE
Answered below. I received a down-vote for asking this question, but one might argue that a Getting Started guide should be self-contained. No biggie; I worked across the two tutorials and solved my problem and posted an answer for those who are confused as I was.
So, I was able to successfully complete the AWS Amplify / iOS SDK Getting Started guide after leveraging the Apple iOS Swift Getting Started guide to create the necessary view elements required by AWS. What that means is this:
Two text fields: 'name' and 'description'; a label; and a button. Here are my outlet properties:
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var descTextField: UITextField!
#IBOutlet weak var todoItemLabel: UILabel!
My viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
descTextField.delegate = self
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appSyncClient = appDelegate.appSyncClient
}
My button action which calls runMutation():
//MARK: Actions
#IBAction func addToDoItem(_ sender: UIButton) {
runMutation()
}
And changes to runMutation() to update DynamoDB with the entered values:
let mutationInput = CreateTodoInput(name: nameTextField.text ?? "No Entry", description: descTextField.text)
If you have followed Steps 1 - 4 of the AWS Amplify / iOS SDK Getting Started guide and added the necessary UI elements then the code above will seal the deal.
Also note that the API reference pointed at by #dennis-w in the comments above takes care of those deprecated references in AppDelegate from the Getting Started guide.
import XCTest
class UnitTestClass: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
}
I have multiple UIViewController classes in my app. Now, i want to test each class by UIAutomation in Xcode 8 ,swift 3. After Searching a lot, i didn't get any useful tutorial or tool in Xcode or swift's latest version. My App's First screen is Login screen. Can anyone help me to find-out the useful solution
I believe UIAutomation was deprecated in Xcode 8. But you can set up UI tests in Xcode 8 by simply doing the following:
Add an iOS UI Testing Bundle target to your project.
Create a test method under the new test file created as part of that target.
Click inside the curly braces within the method.
Click the "record" icon shown below the source editor to start recording your UI actions in the simulator.
Watch this WWDC Video to learn more about UI testing in Xcode.
Is there any way of testing the UIApplication shortcuts within XCUITests?
I know that in order to test 3d shortcuts in a simulator you need a trackpad with force touch, but I was wondering if I could write tests that test my shortcuts.
Looks like yes! You'll need to expose a private API, though. Exported XCUIElement header from Facebook's WebDriverAgent here.
Or, if that's the only thing you need, just expose it ala:
#interface XCUIElement (Private)
- (void)forcePress;
#end
And then to force press your icon by going to the springboard and getting your icon element see my answer here.
class Springboard {
// this is another private method you'll need to expose (see linked other answer)
static let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
/// Open the 3d touch menu
class func open3dTouchMenu() {
XCUIApplication().terminate() // this may or may not be necessary depending on your desired function / app state
// Resolve the query for the springboard rather than launching it
springboard.resolve()
// Force delete the app from the springboard
let icon = springboard.icons["MyAppName"]
if icon.exists {
icon.forcePress()
// do something with the menu that comes up
}
}
}
* I have not tested this yet.
I have an application written in osx swift, and I need help with a particular behavior/issue. I have the application set to be above all other windows using this code:
self.window!.level = Int(CGWindowLevelForKey(Int32(kCGScreenSaverWindowLevelKey)))
A little issue with it, from what I've seen, is that it does work... but if I go to my launchpad applications menu, the window still shows up. That's exactly how I want the window to behave, but whenever I hit a button within the window, the application resets to the normal desktop to function properly. I have a link below demonstrating this:
https://gyazo.com/f4d05c10ad7b5dbf8b95f3bd2aa23cc4
See how I'm getting taken out of my launchpad and then reset to the normal desktop screen whenever I hit buttons within my application? I just really need to fix this issue.
Is there any info.plist property that I can use to prevent this from happening? Is there any piece of code I could use to make my window use-able everywhere without resetting it to the proper desktop environment? Thanks in advance!
NSFloatingWindowLevel might be a better choice:
import Cocoa
let kCGFloatingWindowLevel: CGWindowLevel =
CGWindowLevelForKey(CGWindowLevelKey.FloatingWindowLevelKey)
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.window!.level = Int(kCGFloatingWindowLevel)
}
}
The window level type you are currently using is primarily meant for screensaver windows. If you want a floating palette or window then using the above should give you the desired behavior.
↳ NSWindow : NSFloatingWindowLevel
I know there is a similar question but the OP seems to think his problem was only on the simulator and so not such an issue... But I think mine is in both.
I was having a problem adapting an app for iCloud storage (when trying to observe for NSUbiquityIdentityDidChangeNotification). I could not see what the problem was with my code and so...
...after hours of banging my head against a wall I have set up a new project in Xcode (v6.1 6A1046a) to try and narrow it down. It seems it must be a very basic problem indeed...
The only things I have done in this new projects are:
Enable iCloud capabilities for 'Key-value storage' and for 'iCloud
Documents'(using the default container) - no error messages, appears to be set up fine.
Add a label and a button to the provided view controller in IB.
Add an IBOutlet for both and hook up an IBAction to change the label text when pressed.
For clarity, the only code I added to an empty 'Single View Application' template is:
#IBOutlet weak var label: UILabel!
#IBOutlet weak var button: UIButton!
#IBAction func buttonPressed(sender: AnyObject) {
self.label.text = "You pressed the button"
}
So if I run the project from Xcode in the simulator or on the device, or if I run the project directly on the device, it launches fine and the button works properly - as you would expect...
The problem is:
If I am running on the device from Xcode & I go to the Settings->iCloud->iCloud Drive on the device and toggle the switch for my app (from/to either state) the app crashes. I'm getting no feedback except this:
If I am running the app directly on the device and try and toggle iCloud Drive setting, when I go back into the app it appears to restart (the view is being reset, along with the label.text - neither of which happen if I just visit settings without touching the switch).
It also has the habit of freezing my device completely, which is mildly irritating.
I'm new to iCloud development so I guess I may be missing something really basic...
As I say it's an empty project with only a couple of lines of additional code so it's probably not something I HAVE done. Which limits it to something I HAVEN'T done. I am trying to follow as many tutorials as I can find and as much apple documentation as well. I just can't see any obvious steps I may have overlooked.
Unless it's the expected behaviour, or an Apple issue I guess.
Thanks for any help - it's driving me nuts!
iOs kill your app when you change iCloudDrive state. This is standard iOs behavior you can check this with build in applications(Safari for example).