Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement local notification in iOS SDK

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 195
    Comment on it

    Hi Readers,

    In iOS development of today scenario we may pass the data in different part of code either to selected class or broadcast. One way of broadcasting objects is local notification. Following is the way to implement it:

    To broadcast object to all classes we need to make that class observer for notification like:

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(yourMethodName:) name:@"yourNotificationName" object:nil];
    

    After It we can post notification to like:

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter postNotificationName:@"yourNotificationName" object:yourObject];
    

    Now in that method definition you can get passed object as follows:

    -(void)yourMethodName::(NSNotification *)notification{
        id passedObject = notification.object;
    }
    

    Now you can get this passedObject to all classes working as an observer to yourNotificationName notification. It can also possible to pass dictionary as userInfo by notification. It can be passed like:

     NSDictionary *userInfo = @{
                                   @"yourStringKey1":@"yourStringValue1",
                                   @"yourStringKey2":@"yourStringValue2",
                                   };
    [[NSNotificationCenter defaultCenter] postNotificationName:@"yourNotificationName" object:yourObject  userInfo:userInfo];
    

    So it can also get by observer selector as :

      -(void)yourMethodName::(NSNotification *)notification{
                NSDictionary *dict = notification.userInfo;
        }
    

    NOTE: You need to remover observer for notification as well while unloading or dealloc class as follows:

     [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    Hope it helps. Thanks for reading

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: