nickoneill / PermissionScope

Intelligent iOS permissions UI and unified API
MIT License
4.85k stars 507 forks source link

OnAuthChange block not called #242

Open SandeepAggarwal opened 7 years ago

SandeepAggarwal commented 7 years ago

OnAuthChange block is not getting called in case of notification permission

    ` PermissionScope* permissionScope = [PermissionScope new];
     switch ([permissionScope statusNotifications])
     {
             case PermissionStatusAuthorized:
               if (authorizedBlock)
              {
                    authorizedBlock();
              }
              return;

             case PermissionStatusUnknown:
               [permissionScope requestNotifications];
                break;

              case PermissionStatusUnauthorized:
             case PermissionStatusDisabled:
             if (deniedOrDisabledBlock)
            {
                deniedOrDisabledBlock();
            }
            return;
   }

@weakify(permissionScope);
[permissionScope setOnAuthChange:^(BOOL authorized, NSArray<PermissionResult *> * _Nonnull results)
 {
     @strongify(permissionScope);
     NSLog(@"status: %ld",[permissionScope statusNotifications]);

     if ([permissionScope statusNotifications] == PermissionStatusUnauthorized)
     {
         if (deniedOrDisabledBlock)
         {
             deniedOrDisabledBlock();
         }
     }
     else if ([permissionScope statusNotifications] == PermissionStatusAuthorized)
     {
         if (authorizedBlock)
         {
             authorizedBlock();
         }
     }
 }];`
royherma commented 6 years ago

same for me, were you able to resolve?

SandeepAggarwal commented 6 years ago

@royherma Yes, I was able to use a workaround to solve this issue:

//setOnAuthChange: is not being called (bug) so this workaround

@weakify(permissionScope);
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note)
    {
             @strongify(permissionScope);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), 
            dispatch_get_main_queue(), ^
           {
                  if ([permissionScope statusNotifications] == PermissionStatusUnauthorized || 
                  [permissionScope statusNotifications] == PermissionStatusDisabled)
                {
                        if (deniedOrDisabledBlock)
                       {
                             deniedOrDisabledBlock();
                       }
                }
                else
                {
                       if (authorizedBlock)
                       {
                            authorizedBlock();
                       }
                 }
           });
             [[NSNotificationCenter defaultCenter] removeObserver:self];
    }];