Get date from when app first opened - objective-c

I have an app that logs the amount of miles you travel, and it presents the data in a UITableView. A helpful thing I thought however, was to add since when this data was formed. Which would be the first time the app has opened right. So, how could I retrieve a date since the first time the app has opened. I thought of an approach that in the App Delegate, I get a string from a file. And if the string then is nil, it means the app has first been opened, then I add a simple NSDate. From there I copy a string into a file, so next time the app has opened the string will be valid and the app recognizes it has not been the first time the app has opened. Unfortunately this did not work.
Are there better solutions?

Use NSUserDefaults for this purpose. On the launch check if #"firstLaunchDate" key is missing. If it is - this is the first launch of your app and you can store the launch date. Other times application will be launched - the condition will evaluate to false and won't overwrite previously set value.

Related

watchOS background refresh not accessing CoreData?

I have a watch companion app that accesses CoreData through CloudKit and then chooses a random string from the data.
It works perfectly, all of that is setup exactly as needed. So I know all of that is working.
The issue now is I am wanting the Watch complication to update sporadically with an updated random string from that data.
Here is what I have confirmed is working:
the complication can update with the assigned variable correctly
this variable can be assigned manually by opening the app and pressing a button
background scheduling and refreshing is confirmed to work, I tested with print statements.
The issue now is that the goal is to make the function run with the background refresh. This function takes that data from CoreData, picks a random string, and assigns it to the variable. That variable is then displayed on the watch face complication and in the app.
The function has an if statement that says
if (the fetched coredata) .isEmpty {
(Variable) = “No Data” }
And the watch complication is showing “No Data.”
This indicates that this function is running with the background refresh but is not accessing the CoreData and is staying empty. This exact same function works fine when manually called inside the app.
TL;DR I have isolated and confirmed that CoreData is not being accessed when running a function in the background schedule on watchOS.
How do I fix this?

How can I automatically run an AppleScript when Safari.app starts?

Basically I want make sure I execute a custom written .applescript every time Safari is opened.
I tried adding the script to the "~/Library/Scripts/Applications/Safari" folder but that doesn't seem to automatically do it.
As a workaround I can probably just write an AppleScript that both starts Safari and runs my custom stuff. I just wanted to check whether there's a cleaner way of doing this.
Putting stuff in the ~/LibraryScripts/Applications folder just makes them available when the particular application is active.
In addition to your alias that runs the script and Safari, you could use some AppleScriptObj-C in a background application (maybe run via login items) that registers for NSWorkspaceDidLaunchApplicationNotification notifications to get the names of applications as they are launched. This would also have the advantage of seeing when Safari gets launched other than from your alias, such as via various document links (Mail, PDFs, etc):
use framework "Foundation"
my addObserver:me selector:"appLaunchNotification:" |name|:(current application's NSWorkspaceDidLaunchApplicationNotification) object:(missing value)
on appLaunchNotification:notification -- an application was launched
# notification's userInfo contains the application that was launched
set applicationInfo to NSWorkspaceApplicationKey of notification's userInfo -- NSRunningApplication
set appName to (localizedName of applicationInfo) as text
-- do your thing depending on appName
end appLaunchNotification:

Where do you find the ApplicationId to pass to TileUpdateManager.CreateTileUpdaterForApplication(string) for a background task

I want to update a live tile from a background task (and the lock screen badge, and show toast notifications). I've got code that works when the app is running, and it runs through to completion as a background task, but nothing happens to the live tile/toast/badge. I'm assuming that's because I need to use the overload of TileUpdateManager.CreateTileUpdaterForApplication() that takes a string, but I don't know where to find the string and/or what the format is.
Any ideas?
Package relative app identifier (PRAID)
Edit per comment: Have you tried the Application ID that you specify in the Manifest or the ID you use to create the Secondary Tile?

Detect First Time User

How can I detect if the user has just downloaded the application and opened it for the first time? IS this a NSUserDefaults? I want to be able to show a welcome screen only the first time my application is run.
Thanks
check for a bool in NSUserDefaults and if it is not set do whatever you want and save a YES-bool back to NSUserDefaults. If you show an alert you probably should put the setBool:forKey: in the delegate method which is called after you have dismissed the alert.
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"wasLaunchedBefore"]) {
NSLog(#"First launch");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"wasLaunchedBefore"];
}
You can use the NSUSerDefaults.By this when the user opened app check whether there is any values in your user defaults for a key.If it is not then that is first time.After this check you have to update the value for the key which you have checked previously.
You can check if flag is set in NSPreferences or check if file exists in app's file (file that you create after the first launch).
You need to set in your application if opened first time then show that stuff which you want to show first time. There is no any other way to find out that user downloaded your application and he run it or not. On run process you need to set it inside your application.
If you use NSUserDefaults then user can reinstall your application. And application will think thank user use it for first time again. But after updating application remembers that the user has already launched it.
I can't understand from your question if it is appropriate for you but the most of applications work this way

iPhone User Defaults starting values

I've set up my app's user defaults to contain two toggle switches - one for help, one for sound. They are working however the starting values are false even though I've set them to be YES:
I use BOOL variables to track these values in the app, so I'd like to store YES/NO values. What else do I need to do to get the correct starting values?
I found the answer to this. Apparently there is an Application Domain AND a Registration Domain that aren't in sync the first time the app is run. The solution that worked for me is here:
http://excitabyte.wordpress.com/2009/08/12/keeping-user-defaults-synchronized-with-settings-bundle/
Not good when you want to have help and sound enabled the first time your app is run! Come on Apple.
Uhm... Aren't toggle switches setup this way:
http://screencast.com/t/bGCGIZara