2012-04-25 13 views
25

मेरे पास कई आईओएस ऐप्स हैं जो सभी एक ही पोर्ट का उपयोग नेटवर्क बीकन सुनने के लिए करते हैं। मुख्य दृश्य पर जब मैं एक और दृश्य खोला जाता है, तो पोर्ट बंद करने के लिए ViewWillDisappear का उपयोग करता है, जो बहुत अच्छा काम कर रहा था। तब मैंने देखा कि अगर मैंने बंदरगाह को बंद करने के लिए एक और दृश्य खोलने के बिना मुख्य दृश्य नियंत्रक से होम बटन दबाया, तो बंदरगाह खुला रहता है और मेरे अन्य ऐप्स में से कोई भी उस बंदरगाह पर और नहीं सुन सकता है। मैंने फिर viewWillUnload का उपयोग करने का प्रयास किया, लेकिन जब मैं होम बटन दबाता हूं तो ऐसा नहीं लगता है।पता लगाएं कि घर बटन दबाए जाने पर पता लगाएं आईओएस

-(void)viewWillUnload 
{ 
    //[super viewWillUnload]; 
    NSLog(@"View will unload"); 
    [udpSocket close]; 
    udpSocket = nil; 
} 

देखें अनलोड को कंसोल में कभी प्रदर्शित नहीं किया जाएगा, जो मुझे विश्वास दिलाता है कि विधि कभी नहीं कह रही है।

क्या घर बटन दबाए जाने का पता लगाने का कोई तरीका है ताकि मैं अपना बंदरगाह बंद कर सकूं?

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 
+3

कोशिश

class VBase: UIViewController { fileprivate var listenersActivated = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) onStart() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) onStop() removeListeners() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. onStop() removeListeners() } internal func iniListeners() { if (!listenersActivated) { NotificationCenter.default.addObserver(self, selector: #selector(onStop), name: NSNotification.Name.UIApplicationWillResignActive, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(onStart), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) listenersActivated = true } else { } } internal func removeListeners() { NotificationCenter.default.removeObserver(self) listenersActivated = false } internal func onStop() { } internal func onStart() { iniListeners() } } 

अवहेलना onStop() और onStart() अंदर बच्चे "applicationWillTerminate" विधि ।:-) –

+1

"applicationWillTerminate" विधि मौजूद नहीं है। हालांकि, उप-वर्ग UIAplplicationWillTerminateNotification के लिए पंजीकरण कर सकते हैं और फिर अपना स्वयं का सफाई या बंद कर सकते हैं। –

उत्तर

39

ये अपने विकल्पों

अपने अनुप्रयोग प्रतिनिधि में कर रहे हैं। आप application delegate methodsapplicationDidEnterBackground: या applicationWillTerminate: को कार्यान्वित करने और वहां पर कार्य करने या अपने एप्लिकेशन के हिस्से में एक अधिसूचना भेजकर बेहतर हैं जो सफाई प्रक्रिया को संभालने के बारे में जानता है।

+0

चूंकि मैं स्पष्ट रूप से सहमत हूं, +1 – DGund

+2

मेरे पास पोर्ट को बंद करने के लिए कोड था, विल्लडिस्प्लेर और ऐसा नहीं लगता था। बंदरगाह खुला रहेगा और बंदरगाह का उपयोग करने वाले सभी अन्य ऐप्स असफल हो जाएंगे। मैंने बंदरगाह को बंद करने के लिए एक क्लास विधि स्थापित की है और इसे एप्लिकेशनडिड एंटरबैकग्राउंड से कॉल किया है और यह बहुत अच्छा काम करता है – nick

+7

'व्यू विल्ल डिस्प्लेयर' या 'व्यूडिड डिस्प्लेयर' को होम बटन दबाए जाने पर या ऐप समाप्त होने पर कॉल नहीं किया जाता है। सबसे अच्छा समाधान 'UIAplicationWillResignActiveNotification' अधिसूचना – Sam

5

viewWillUnload अक्सर कम स्मृति के मामले को छोड़कर नहीं बुलाया जाता है:

5

viewWillUnload आमतौर पर कम स्मृति के मामले को छोड़कर नहीं कहा जाता है। इन बजाय उपयोग:

अपने अनुप्रयोग प्रतिनिधि में:

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

या फिर आप अपने दृश्य नियंत्रक में कोड का उपयोग करना चाहते हैं:

- (void)viewDidDisappear:(BOOL)animated 
{ 
//Put code here 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
//Put code here 
} 
+0

व्यूविल्ल/डीड डिस्प्लेयर का उपयोग कर रहा है जब ऐप बंद हो रहा है। जब घर बटन ने केंद्र को नियंत्रित करने के लिए अनुप्रयोगों को कम करने के लिए दबाया नहीं। – khunshan

31

इस संभाल करने के लिए सबसे आसान तरीका करने के लिए रजिस्टर करने के लिए है अपने व्यू कंट्रोलर में UIApplicationWillResignActiveNotification अधिसूचना प्राप्त करें।

घटना एक घर बटन दबाएँ, ताला पर और एक फोन कॉल

- (void) applicationWillResign{ 
    NSLog(@"About to lose focus"); 
} 

- (void) myVcInitMethod { 
    [[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(applicationWillResign) 
     name:UIApplicationWillResignActiveNotification 
     object:nil]; 
} 
+3

शून्य के बजाय न्यूल पास क्यों करें? – mszaro

+1

मैंने शून्य का उपयोग करने के लिए कोड संपादित किया। – PeterPurple

+8

असल में 'applicationWillResignActive' अधिसूचना _not_ हमेशा ऐसा करने का सबसे अच्छा तरीका है, क्योंकि सक्रिय इस्तीफा में भी (आकस्मिक) स्लाइड डाउन मेनू, या आईओएस 7 में नई स्लाइड अप डाउन मेनू शामिल है। 'ApplicationDidEnterBackground' का अर्थ है कि आपका ऐप" छोटा " , और आईओएस 4 से उपलब्ध है। – bobobobo

10

स्विफ्ट उपयोगकर्ता के मामले में पर जारी किया जाता है

आप इस

override func viewDidLoad() { 
    super.viewDidLoad() 

    // code here... 

    NSNotificationCenter.defaultCenter().addObserver(
     self, 
     selector: "applicationWillResignActive:", 
     name: UIApplicationWillResignActiveNotification, 
     object: nil) 
} 

func applicationWillResignActive(notification: NSNotification) { 
    print("I'm out of focus!") 
} 

भी है जैसे कि यह लिख सकते हैं , जब आपका ऐप समाप्त हो जाता है तो इसे बंद करना न भूलें

deinit { 

    // code here... 

    NSNotificationCenter.defaultCenter().removeObserver(self) 
} 
+0

यदि आप आईओएस 9 का उपयोग कर रहे हैं या आगे आप Deinit विधि में पर्यवेक्षकों को हटाने के बारे में भूल सकते हैं। लेकिन, केवल अगर आप आईओएस 8 या उससे पहले का समर्थन करने की योजना नहीं बनाते हैं। और, जैसा कि @ बॉबोबोबो ने कहा था, आपको एप्लिकेशनडिड एंटरबैकग्राउंड का उपयोग करना चाहिए –

2

UIApplicationWillResignActive और का उपयोग करने के लिए बेहतर है क्योंकि वे 'शीर्ष आयताकार पकड़ और रिलीज ईवेंट' पकड़ते हैं। मैं इस जड़ वर्ग का उपयोग करने का सुझाव देते हैं: उपयोग करने के लिए सभी दृश्य उपस्थिति/लापता होने

है यही कारण है, को पकड़ने के लिए

class SomeViewController: VBase { 

... 
    override func onStart() { 
     super.onStart() 
     someFunctionToInitialize() 
    } 
    override func onStop() { 
     super.onStop() 
     stopTimer() 
     someFunctionToDesctruction() 
    } 
} 
संबंधित मुद्दे