I'm using Fastlane's snapshot to create screenshots for an app I'm about to submit to the App Store.
It works "as advertised" for the most part, but it doesn't seem to like the way I access the UserDefaults within my app. On one test, it generates an Exit status: 65 error.
UI Testing Failure - com.me.MyApp crashed in (extension in MyApp):__ObjC.NSObject.defaultTime () -> Swift.Float
I find UserDefaults.standard.value(forKey: "defaultTime") to an invitation for a syntax error, so I created an extension to access UserDefaults. Here's what the extension looks like:
class CustomExtensions: NSObject {
/*
This is blank. Nothing else in here. No really...nothing else
*/
}
extension NSObject {
// User Defaults
func defaultTime() -> Float {
return UserDefaults.standard.value(forKey: "defaultTime") as! Float
}
// a bunch of other UserDefaults
}
Wihin the app, whenever I need defaultTime, I just type defaultTime(). Using this method to access UserDefaults values works fine in the Simulator and on the devices I've tested. I only encounter a problem with snapshot.
I've tried adding in sleep(1) within the test, but that doesn't seem to do anything. I welcome suggestions re: alternative means of accessing UserDefaults that enable me to access them easily throughout MyApp.
What's probably happening is that, in your simulator and on device, you're writing a value to user defaults for the key defaultTime before it is ever read. value(forKey: returns an optional, and if you force-unwrap it (or force down-cast as your are doing here), you will crash if the value is nil. Try either returning an optional:
func defaultTime() -> Float? {
return UserDefaults.standard.value(forKey: "defaultTime") as? Float
}
or using a default value:
func defaultTime() -> Float {
return UserDefaults.standard.value(forKey: "defaultTime") as? Float ?? 0.0
}
Related
I'm new to both Xcode and Swift (...and serious Swift programming) and am hoping someone can help me figure out how to view / access the values of this class object.
I have this code in my ViewController.swift for invoking my REST API (via AWS API Gateway) and am attempting to print result to the console. Clearly, all I'm doing here is printing the address of the class object:
#IBAction func userInvokeApi(_ sender: UIButton) {
print("You clicked invoke api...")
let client = SVTLambdaGateClient.default()
client.calcGet(operand2: "3", _operator: "+", operand1: "5").continueWith{ (task: AWSTask?) -> AnyObject? in
if let error = task?.error {
print("Error occurred: \(error)")
return nil
}
if let result = task?.result {
// Do something with result
print("The result is... \(result)")
}
return nil
}
}
Here's what prints:
You clicked invoke api...
The result is... <AmplifyRestApiTest.Empty: 0x600002020770> {
}
(where AmplifyRestApiTest is the name of my Xcode project. Though I'm NOT using AWS Amplify to build this project; mainly because I've run into problems using it.)
I do have this Empty class in Empty.swift that is part of the API Gateway generated iOS Swift SDK:
import Foundation
import AWSCore
#objcMembers
public class Empty : AWSModel {
public override static func jsonKeyPathsByPropertyKey() -> [AnyHashable : Any]!{
var params:[AnyHashable : Any] = [:]`
return params
}
}
Now, when I set a breakpoint on the print statement this is what I see:
Can someone please tell me why I don't see the values relating to this object? What's the strategy for unpacking this API response??
I know that I'm invoking the REST API successfully because I can see (via Cloudwatch logs) that it's returning the result to the Client. So this post is just my attempt to access the corresponding Object.
Another detail: I'm using an API Gateway generated iOS Swift SDK and I followed all of the tutorial instruction for using the SDK in my project.
use lldb command po to print the object.
(lldb) po #"lunar"
lunar
(lldb) p #"lunar"
(NSString *) $13 = 0x00007fdb9d0003b0 #"lunar"
I would suggest going over the docs here...
https://cocoapods.org/pods/AWSCore#getting-started-with-swift
Did you import appropriate headers?
Hope this points you in the right direction.
I "inherited" an iOS Xcode Project in Swift. I've never programmed in Swift or used Xcode before. I copied the project from one user account to another and checked it into a git repo.
Now I'm araid this (the copying, permissions ?) might be the cause for the App not being able to read its settings properly. Because UserDefaults.standard.string(forKey: "pref_Foo") returns nil although in the Settings.Bundle's Root.plist there's clearly a pref_Foo identifier.
The App has worked before, so I don't see where this suddenly comes from.
Since I'm not too familiar with XCode all I did up til now was debug into the Application to see that UserDefaults.standard.string(forKey: "pref_Foo") is nil.
How could I approach this problem?
Thank you!
EDIT: This is part of my code in ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
loadSettings()
...
}
func loadSettings()
{
if(UserDefaults.standard.object(forKey: "pref_Foo") == nil)
{
printf("Error")
}
}
You should use below code with validation, first delete app and install again. then use, you will get success.
var UserStatus:String {
get{
return UserDefaults.standard.object(forKey: "pref_Foo") as? String ?? "0"
}
set(status){
UserDefaults.standard.set(status, forKey: "pref_Foo")
UserDefaults.standard.synchronize()
}
}
Use of code is as below.
if (UserStatus == "pref_Foo"){
self.logoutSuccess()
} else {
self.loginSuccess()
}
Update or set value is so simple in one line code as given below.
UserStatus = "pref_Foo"
A newbie to native swift development!
Opened the below issue in https://github.com/forcedotcom/SalesforceMobileSDK-iOS/issues/2072
Version of Mobile SDK Used: 5.1.0
Issue found in Native App or Hybrid App: Native App
OS Version: 10.12.5
Device: iPhone 6
Steps to reproduce:
forceios create
Provided application type as native_swift and add other requested details
Open the *.xcworkspace file in Xcode
Build the project
Error: Value id optional type '[SFUserAccount]?' not unwrapped;
func handleSdkManagerLogout()
{
self.log(.debug, msg: "SFAuthenticationManager logged out. Resetting app.")
self.resetViewState { () -> () in
self.initializeAppViewState()
// Multi-user pattern:
// - If there are two or more existing accounts after logout, let the user choose the account
// to switch to.
// - If there is one existing account, automatically switch to that account.
// - If there are no further authenticated accounts, present the login screen.
//
// Alternatively, you could just go straight to re-initializing your app state, if you know
// your app does not support multiple accounts. The logic below will work either way.
var numberOfAccounts : Int;
let allAccounts = SFUserAccountManager.sharedInstance().allUserAccounts()
numberOfAccounts = (allAccounts!.count);
if numberOfAccounts > 1 {
let userSwitchVc = SFDefaultUserManagementViewController(completionBlock: {
action in
self.window!.rootViewController!.dismiss(animated:true, completion: nil)
})
if let actualRootViewController = self.window!.rootViewController {
actualRootViewController.present(userSwitchVc!, animated: true, completion: nil)
}
} else {
if (numberOfAccounts == 1) {
SFUserAccountManager.sharedInstance().currentUser = allAccounts[0]
// ERROR: Value id optional type '[SFUserAccount]?' not unwrapped;
}
SalesforceSDKManager.shared().launch()
}
}
}
The allUserAccounts property of SFUserAccountManager is nullable.
- (nullable NSArray <SFUserAccount *> *) allUserAccounts;
https://github.com/forcedotcom/SalesforceMobileSDK-iOS/blob/master/libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Security/SFUserAccountManager.h#L188
If you know for a fact that it will exist at the time you are trying to use it, you can perform a force unwrap by typing allAccounts![0]. If you need to handle the case where it may be nil, you need to check for that by doing something like:
if let accounts = allAccounts
{
currentUser = accounts[0]
}
else
{
// does not exist
}
What I can't tell you is whether it being nil is actually a valid case that you need to handle, as I am not familiar with the library. You'll need to do the research or ask them yourself.
You need to unwrap allAccounts because it's an optional array. And since force unwrapping has been used above to get numberOfAccounts, it's probably safe to use it.
Try this:
SFUserAccountManager.sharedInstance().currentuser = allAccounts![0]
Under the iOS simulator, I'm having trouble getting a consistent value back from NSUserDefault on app startup. I've set the value in a previous run of the app (and I put some prints in there to make sure there was no accidental changes), so I don't think there's a problem with the way I'm calling synchronize(). The reads are happening on the main thread also, and after I've set the default values. After a couple of seconds, if I retry the fetch from NSUserDefaults, I get the correct value (I put a timer in there to verify that the value gets corrected and seems to stay the same after startup).
The value represents where the user will store the data (on iCloud or just on the local device) and is represented by an integer in the user defaults. Here's the enum representing the values:
class MovingDocument: UIDocument {
enum StorageSettingsState: Int {
case NoChoice = 0
case Local = 1
case ICloud = 2
}
}
Here's an excerpt of the class used to access NSUserDefaults:
class Settings {
static let global = Settings()
private enum LocalStoreKeys : String {
case IsHorizontal = "IsHorizontal"
case ItemInPortrait = "ItemInPortrait"
case RotateTitle = "RotateTitle"
case StorageLocation = "StorageLocation"
}
// Other computed variables here for the other settings.
// Computed variable for the storage setting.
var storageLocation: MovingDocument.StorageSettingsState {
get {
print("Storage state fetch: \(NSUserDefaults.standardUserDefaults().integerForKey(LocalStoreKeys.StorageLocation.rawValue))")
return MovingDocument.StorageSettingsState(rawValue: NSUserDefaults.standardUserDefaults().integerForKey(LocalStoreKeys.StorageLocation.rawValue)) ?? .NoChoice
}
set {
print("Setting storage location to \(newValue) (\(newValue.rawValue))")
NSUserDefaults.standardUserDefaults().setInteger(newValue.rawValue, forKey: LocalStoreKeys.StorageLocation.rawValue)
synchronize()
CollectionDocument.settingsChanged()
}
}
func prepDefaultValues() {
NSUserDefaults.standardUserDefaults().registerDefaults([
LocalStoreKeys.IsHorizontal.rawValue: true,
LocalStoreKeys.ItemInPortrait.rawValue: true,
LocalStoreKeys.RotateTitle.rawValue: true,
LocalStoreKeys.StorageLocation.rawValue: MovingDocument.StorageSettingsState.NoChoice.rawValue
])
}
func synchronize() {
NSUserDefaults.standardUserDefaults().synchronize()
}
}
The value that I should be reading (and which I always seem to get after startup) is the value '1' or .Local. The value that I am sometimes getting instead is '0' or .NoChoice (which just happens to be the default value). So, it looks like it's using the default value until some later point in the application startup.
For completeness sake, here's the relevant app delegate code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
Settings.global.prepDefaultValues() // Must be called before doing CollectionDocument.start() because we need the default value for storage location.
CollectionDocument.start()
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
browserController = ViewController()
navController = UINavigationController(rootViewController: browserController!)
navController!.navigationBar.translucent = false
self.window!.rootViewController = navController
self.window!.makeKeyAndVisible()
checkStorageLocation()
return true
}
The getter is called within CollectionDocument.start() (CollectionDocument is a subclass of MovingDocument, where the location enum is defined). The call at the end to checkStorageLocation() is my periodic debugging check for the value of the storage location (it calls itself back via dispatch_after()).
As a side note, I also tried putting most of the code in the application(didFinishLaunchingWithOptions...) into a dispatch_async call (on the main thread) to see if that made a difference (just the call to set up the default values and the return statement were done immediately, the rest of the calls were put into the asynchronous call block), and it still got the wrong value (sometimes). I guess I could try using dispatch_after instead (and wait a second or two), but I can't really start the app until this is complete (because I can't read the user data from the doc until I know where it is) and a loading screen for just this one problem seems ridiculous.
Any ideas about what might be going wrong? I'm unaware of any limitations on NSUserDefaults as far as when you are allowed to read from it...
Rebooting my mac fixed the problem (whatever it was). iOS 10, NSUserDefaults Does Not Work has a similar problem and the same solution (though the circumstances are quite different, since I have tried neither Xcode 8 nor iOS 10).
So I had set up a settings bundle to just do one thing. Allow the Users to choose between the The TouchUI and GestureUI in my app and for some odd reason, I am unable to get the Settings Bundle to control it. It stays with one and doesn't switch even when I have the If else Statement. originally I had var touchCheck = userDefaults.boolForKey("myGestureEnabledDisabled") but it didn't change boolean at all when i keep closing app(Multitask > SwipeUp app) and re running the app via springboard. The Settings App could have the bundle at NO but log says gestures are on. After watching a Tutorial, i changed boolForKey to valueForKey which causes the build to fail and there is no error in the code the way i have it below.
override func viewDidLoad() {
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.synchronize()
var touchCheck = Bool(userDefaults.valueForKey("myGestureEnabledDisabled"))
if touchCheck {
whenGuestureIsEnabled()
} else {
whenGestureIsDisabled()
self.navBar.hidden = false
}
}
func whenGuestureIsEnabled() {
NSLog("Gesture is suppose to be on")
}
func whenGestureIsDisabled() {
NSLog("Gesture is OFF")
}
Maybe from what I was thinking, I shouldn't do this in UIViewController but I had seen this in action in a youtube tutorial and it was in OBJ-C.
You should cast to bool, not to create a new one:
//notice (Bool) cast in the beginning
var touchCheck = (Bool)userDefaults.valueForKey("myGestureEnabledDisabled")