2016-11-22 13 views
5

मेरा ऐप वर्तमान उपयोगकर्ता लॉग आउट नहीं होगा के बाद एप्लिकेशन को फ़ोन से स्थापना हटाई गई। मैं उपयोगकर्ता एप्लिकेशन को अनइंस्टॉल करना नहीं चाहते हैं, और जब वे इसे पुनः स्थापित वे पहले से लॉग इन कर रहे हैं।लॉग उपयोगकर्ता - Firebase

मुझे लगता है कि वह अपने कीचेन पहुंच शायद के साथ कुछ है? निश्चित नहीं। मैं सोच रहा था कि ऐप हटा दिए जाने के बाद मुझे उपयोगकर्ता को केवल प्रमाणीकृत करने की आवश्यकता थी, लेकिन उस स्थिति की जांच करने का कोई तरीका नहीं है। कि के निकटतम बात applicationWillTerminate समारोह चल रहा हो जाएगा, लेकिन अगर मैं वहाँ में मेरी FIRAuth.auth()?.signOut() शब्दों में कहें, यह मेरे उपयोगकर्ता बाहर हर समय एप्लिकेशन की मौत हो गई पर हस्ताक्षर करेंगे। मुझे वह नहीं चाहिए।

मैं इस काम करने के बारे में कैसे जाना होगा?

उत्तर

12

हालांकि फ़ोन से ऐप को अनइंस्टॉल करने पर जांच करने के लिए कोई फ़ंक्शन या हैंडलर नहीं है, लेकिन हम जांच सकते हैं कि यह ऐप्स पहले लॉन्च है या नहीं। एक ऐप पहली बार लॉन्च होने की संभावना से अधिक, इसका मतलब यह भी है कि इसे अभी इंस्टॉल किया गया है और ऐप के भीतर कुछ भी कॉन्फ़िगर नहीं किया गया है। यह प्रक्रिया 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 
} 
संबंधित मुद्दे