हालांकि फ़ोन से ऐप को अनइंस्टॉल करने पर जांच करने के लिए कोई फ़ंक्शन या हैंडलर नहीं है, लेकिन हम जांच सकते हैं कि यह ऐप्स पहले लॉन्च है या नहीं। एक ऐप पहली बार लॉन्च होने की संभावना से अधिक, इसका मतलब यह भी है कि इसे अभी इंस्टॉल किया गया है और ऐप के भीतर कुछ भी कॉन्फ़िगर नहीं किया गया है। यह प्रक्रिया return true
लाइन से ऊपर didfinishLaunchingWithOptions
में निष्पादित की जाएगी।
सबसे पहले, हम उपयोगकर्ता चूक स्थापित करने के लिए है:
if userDefaults.bool(forKey: "hasRunBefore") == false {
print("The app is launching for the first time. Setting UserDefaults...")
// Update the flag indicator
userDefaults.set(true, forkey: "hasRunBefore")
userDefaults.synchronize() // This forces the app to update userDefaults
// Run code here for the first launch
} else {
print("The app has been launched before. Loading UserDefaults...")
// Run code here for every other launch but the first
}
हम:
let userDefaults = UserDefaults.standard
इस के बाद, हम अगर एप्लिकेशन से पहले शुरू किया है या पहले चलाया जा रहा है जांच करने की आवश्यकता अब जांच की गई है कि क्या यह ऐप्स पहले लॉन्च है या नहीं। अब हम अपने उपयोगकर्ता को लॉग आउट करने का प्रयास कर सकते हैं। यहाँ अद्यतन सशर्त देखो कैसे चाहिए:।
if userDefaults.bool(forKey: "hasRunBefore") == false {
print("The app is launching for the first time. Setting UserDefaults...")
do {
try FIRAuth.auth()?.signOut()
} catch {
}
// Update the flag indicator
userDefaults.set(true, forkey: "hasRunBefore")
userDefaults.synchronize() // This forces the app to update userDefaults
// Run code here for the first launch
} else {
print("The app has been launched before. Loading UserDefaults...")
// Run code here for every other launch but the first
}
अब हम जाँच कर ली है उपयोगकर्ता पहली बार इस एप्लिकेशन लॉन्च कर रहा है अगर, और यदि हां, एक उपयोगकर्ता को लॉग आउट करता है, तो एक पिछली बार प्रवेश किया गया था सभी कोड एक साथ रखा निम्नलिखित तरह दिखना चाहिए:
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "hasRunBefore") == false {
print("The app is launching for the first time. Setting UserDefaults...")
do {
try FIRAuth.auth()?.signOut()
} catch {
}
// Update the flag indicator
userDefaults.set(true, forkey: "hasRunBefore")
userDefaults.synchronize() // This forces the app to update userDefaults
// Run code here for the first launch
} else {
print("The app has been launched before. Loading UserDefaults...")
// Run code here for every other launch but the first
}
स्रोत
2016-11-22 03:21:50