Reputation: 451
I want to make a singleton Reachability observer for each connection delegate to use, however I couldn't build it correctly, here is some snippet of code.
MYReachability.m
static MYReachability *sharedInstance;
+ (MYReachability *)sharedInstance
{
if (sharedInstance == NULL) {
sharedInstance = [[MYReachability alloc] init];
}
return sharedInstance;
}
- (void)addReachabilityObserver
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
Reachability *reach = [Reachability reachabilityWithHostname: @"www.apple.com"];
[reach startNotifier];
}
- (void) reachabilityChanged: (NSNotification *)notification {
NSLog(@"notification: %@", notification);
Reachability *reach = [notification object];
if( [reach isKindOfClass: [Reachability class]]) {
NetworkStatus status = [reach currentReachabilityStatus];
NSLog(@"reachability status: %u", status);
if (status == NotReachable) {
// Insert your code here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Cannot connect to server..."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
}
in MYConnectionDelegate.m
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (error.code) {
// sending notification to viewController
[[MYReachability sharedInstance] addReachabilityObserver];
MYTestClass *myTestClass = [MYTestClass new];
[myTestClass notificationTrigger];
}
}
in MYTestClass.m
- (void)notificationTrigger
{
// All instances of TestClass will be notified
[[NSNotificationCenter defaultCenter]
postNotificationName:kReachabilityChangedNotification
object:self];
}
Rick, Thanks, that works, but here comes another problem, each time it calls will generates one more notifications into stack... I made a dealloc in MYReachability.m
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
but previous notification still exists.
Upvotes: 1
Views: 393
Reputation: 3306
You're calling the method notificationTrigger
but your test class only has the method notificationTrigger:
with a colon, i.e. one parameter.
Change [myTestClass notificationTrigger];
to [myTestClass notificationTrigger:self];
and it should work.
If you only want the notification to show once, remove yourself as an observer once the alert view has been shown, like this:
- (void) reachabilityChanged: (NSNotification *)notification {
NSLog(@"notification: %@", notification);
Reachability *reach = [notification object];
if( [reach isKindOfClass: [Reachability class]]) {
NetworkStatus status = [reach currentReachabilityStatus];
NSLog(@"reachability status: %u", status);
if (status == NotReachable) {
// Insert your code here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot connect to server..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
}
Upvotes: 2
Reputation: 1268
In one class:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyMe" object:self];
In another class:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doSomething)
name:@"notifyMe"
object:nil];
Upvotes: 2