Jak wykryć pierwsze uruchomienie aplikacji na iPhonie
Jak mogę wykryć pierwsze uruchomienie
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// if very first launch than perform actionA
// else perform actionB
}
Metoda?
12 answers
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-27 16:51:38
Napisałem małą bibliotekę w tym właśnie celu. Pozwala mi to wiedzieć, czy jest to pierwsze uruchomienie w historii, czy tylko dla tej wersji i wszystkich poprzednich wersji, które użytkownik zainstalował. Jest dostępny na GitHubie jako cocoapod na licencji Apache 2: GBVersionTracking
Wystarczy zadzwonić application:didFinishLaunching:withOptions:
[GBVersionTracking track];
A następnie, aby sprawdzić, czy jest to pierwsze uruchomienie wystarczy zadzwonić w dowolnym miejscu:
[GBVersionTracking isFirstLaunchEver];
I podobnie:
[GBVersionTracking isFirstLaunchForVersion];
[GBVersionTracking currentVersion];
[GBVersionTracking previousVersion];
[GBVersionTracking versionHistory];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-26 17:43:55
In Swift 3, 4 spróbuj tego:
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = UserDefaults.standard
if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "isAppAlreadyLaunchedOnce"){
print("App already launched : \(isAppAlreadyLaunchedOnce)")
return true
}else{
defaults.set(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
In Swift 2 try this,
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = NSUserDefaults.standardUserDefaults()
if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
print("App already launched : \(isAppAlreadyLaunchedOnce)")
return true
}else{
defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
UPDATE: - For OBJ-C używam tego,
+ (BOOL)isAppAlreadyLaunchedOnce {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
{
return true;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
return false;
}
}
Ref dla OBJ-C: https://stackoverflow.com/a/9964400/3411787
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-04 06:07:27
Innym pomysłem na Xcode 7 i Swift 2.0 jest użycie rozszerzeń
extension NSUserDefaults {
func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
Teraz możesz pisać w dowolnym miejscu w aplikacji
if NSUserDefaults.standardUserDefaults().isFirstLaunch() {
// do something on first launch
}
Osobiście wolę rozszerzenie UIApplication jak to:
extension UIApplication {
class func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
Ponieważ wywołanie funkcji jest bardziej opisowe:
if UIApplication.isFirstLaunch() {
// do something on first launch
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-14 02:47:42
Możesz zaimplementować go za pomocą metody statycznej poniżej:
+ (BOOL)isFirstTime{
static BOOL flag=NO;
static BOOL result;
if(!flag){
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"hasLaunchedOnce"]){
result=NO;
}else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
result=YES;
}
flag=YES;
}
return result;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-07 16:07:43
Dla swift 3.0
Dodaj rozszerzenie
extension UIApplication {
class func isFirstLaunch() -> Bool {
if UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") {
UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag")
UserDefaults.standard.synchronize()
return true
}
return false
}
}
Następnie w kodzie
UIApplication.isFirstLaunch()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-28 13:17:33
Musisz zapisać coś podczas uruchamiania, a następnie sprawdzić, czy istnieje. Jeśli nie, to pierwszy raz. "Coś" może być plikiem, wpisem bazy danych, ustawieniem domyślnym użytkownika....
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-01 12:32:25
Jest to dość proste i wymaga tylko sześciu linijek kodu.
Przydatne będzie dodanie tego kodu w preferencjach uruchamiania aplikacji lub gdziekolwiek indziej, aby sprawdzić, czy jest on uruchamiany po raz pierwszy.
//These next six lines of code are the only ones required! The rest is just running code when it's the first time.
//Declare an integer and a default.
NSUserDefaults *theDefaults;
int launchCount;
//Set up the properties for the integer and default.
theDefaults = [NSUserDefaults standardUserDefaults];
launchCount = [theDefaults integerForKey:@"hasRun"] + 1;
[theDefaults setInteger:launchCount forKey:@"hasRun"];
[theDefaults synchronize];
//Log the amount of times the application has been run
NSLog(@"This application has been run %d amount of times", launchCount);
//Test if application is the first time running
if(launchCount == 1) {
//Run your first launch code (Bring user to info/setup screen, etc.)
NSLog(@"This is the first time this application has been run";
}
//Test if it has been run before
if(launchCount >= 2) {
//Run new code if they have opened the app before (Bring user to home screen etc.
NSLog(@"This application has been run before);
}
P. S. nie używaj boolów w preferencjach po prostu trzymaj się liczb całkowitych. Domyślnie zerują, gdy są niezdefiniowane.
Również linia [theDefaults synchronize];
nie jest wymagana, ale odkryłem, że gdy aplikacja jest uruchamiana setki razy w przypadku setek urządzeń wyniki nie zawsze są wiarygodne, poza tym jest to lepsza praktyka.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-12 09:55:29
Przechowuj klucz bool w NSUserDefaults po raz pierwszy będzie nie zmienisz go na tak i zachowaj go tak, aż aplikacja usunie lub ponownie zainstaluje ponownie po raz pierwszy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-01 12:32:54
Dla Swift 2.0 w Xcode 7. W aplikacji.plik swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool
{
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
didFinishLaunchingOnce()
return true
}
func didFinishLaunchingOnce() -> Bool
{
let defaults = NSUserDefaults.standardUserDefaults()
if let hasBeenLauncherBefore = defaults.stringForKey("hasAppBeenLaunchedBefore")
{
//print(" N-th time app launched ")
return true
}
else
{
//print(" First time app launched ")
defaults.setBool(true, forKey: "hasAppBeenLaunchedBefore")
return false
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-21 09:12:19
Szybka i łatwa Funkcja
- (BOOL) isFirstTimeOpening {
NSUserDefaults *theDefaults = [NSUserDefaults standardUserDefaults];
if([theDefaults integerForKey:@"hasRun"] == 0) {
[theDefaults setInteger:1 forKey:@"hasRun"];
[theDefaults synchronize];
return true;
}
return false;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-19 20:17:42
NSUserDefaults + Macro
Najlepszym rozwiązaniem jest użycie NSUserDefaults
i zapisanie zmiennej BOOL
.
Jak wspomniano powyżej, poniższy kod wystarczy:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithBool:true] forKey:@"~applicationHasLaunchedBefore"];
[userDefaults synchronize];
Możesz również utworzyć makro jak poniżej, aby łatwo sprawdzić, czy jest to pierwsze uruchomienie, czy nie
#define kApplicationHasLaunchedBefore [[NSUserDefaults standardUserDefaults] objectForKey:@"~applicationHasLaunchedBefore"]
Następnie użyj go jako takiego,
if (kApplicationHasLaunchedBefore) {
//App has previously launched
} else {
//App has not previously launched
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-24 21:38:51