11
-(void)viewDidAppear:(BOOL)animated {
            NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
                [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
                    NSLog(@"SShot");
            }];
        }

- (void)viewWillDisappear:(BOOL)animated{
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    NSLog(@"VWD");
        }

 -(void)viewDidDisappear:(BOOL)animated {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
        NSLog(@"VDD");
    }

I am getting SShot logged in console even after I have removed the observer.

Is there any other way to remove UIApplicationUserDidTakeScreenshotNotification observer.

S.J
  • 3,063
  • 3
  • 33
  • 66

4 Answers4

21

Here is how to do it in Swift 4...

    private var observer: Any!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        observer = NotificationCenter.default.addObserver(forName: NSNotification.Name("SomeNotification"), object: nil, queue: nil) { notification in
            //do something
        }
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        NotificationCenter.default.removeObserver(observer)
    }
Tiago Mendes
  • 4,572
  • 1
  • 29
  • 35
Elijah
  • 8,381
  • 2
  • 55
  • 49
20

From Apple Doc:

To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified by addObserverForName:object:queue:usingBlock: is deallocated.

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];

You're trying to remove the worng observer, self is not the observer here, the observer is the object returned by the add method

Leonardo
  • 1,740
  • 18
  • 27
3

Harris code is correct except a small detail that for Swift 4 it is now

private var observer: Any! and not private var observer: NSObjectProtocol! So code should be:

private var observer: Any!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    observer = NotificationCenter.default.addObserver(forName: NSNotification.Name("SomeNotification"), object: nil, queue: nil) { notification in
        //do something
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(observer)
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Zebra
  • 103
  • 1
  • 7
0

Try using this code

To Add Observer

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

- (void)userDidTakeScreenshot {
    // Screenshot taken, act accordingly.
}

And to Remove a Particular Observer

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

to Remove All Observer

- (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }

Let me know if it worked for you !!!!

Piyush Sharma
  • 1,891
  • 16
  • 23