Untitled

 avatar
unknown
plain_text
3 months ago
6.3 kB
2
Indexable
-(void)CheckUserIsAuthorized
{
	
	NSString * siteid=[FDKeychain itemForKey:SiteId forService:InspectorService error:nil];
	if(siteid.length==0)
    {
		siteid =@"0";
	}
	
	//[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
	_activityView.hidden=NO;
    _activityindicator.hidden=NO;
    [_activityindicator startAnimating];
    _listButton.userInteractionEnabled = NO;
    _qcretakebtn.userInteractionEnabled = NO;
    NSString * url=[NSString stringWithFormat:@"%@%@",serverAI,TokenVarification];
    
    NSString * username=[self GetUsername];
    NSString * accesstoken=[self GetAccessTokenForAI];
	NSString * siteID = [FDKeychain itemForKey:SiteId forService:InspectorService error:nil];
    
    NSDictionary* parametersDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          username, @"username",
                                          accesstoken, @"token",
										  siteID,@"siteid",
                                          nil ];
    
    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    NSDate *startTime = [NSDate date]; // Added on 19 Nov'24
    
    dispatch_async(dispatch_get_main_queue(),^{
        [manager POST:url parameters:parametersDictionary headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
         {
            
            //Added on 19 Nov'24
            NSDate *endTime = [NSDate date];
            NSTimeInterval timeTaken = [endTime timeIntervalSinceDate:startTime];
            NSString *timeTakenStr = [NSString stringWithFormat:@"%.3f Sec", timeTaken];
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
                
            if ([httpResponse isKindOfClass:[NSHTTPURLResponse class]]) {
                NSInteger statusCode = [httpResponse statusCode];
                NSString *statusCodeStr = [NSString stringWithFormat:@"%ld", (long)statusCode];
                [APIStatusLogger logAPIStatus:TokenVarification userName:username timeTaken:timeTakenStr withMessage:statusCodeStr];
                NSLog(@"response status code: %@", statusCodeStr);
            } else {
                NSLog(@"Error: Response is not an NSHTTPURLResponse.");
            }
            // End on 19 Nov'24
           
            
             self->_activityView.hidden=YES;
             self->_qcretakebtn.userInteractionEnabled = YES;
             self->_listButton.userInteractionEnabled = YES;
             NSDictionary * json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                                   options:kNilOptions error:nil];
             NSString * okstr=[json objectForKey:Result];
             if([okstr isEqualToString:OK])
             {
                 if(self->listClick==YES)
                 {
                     [self ShowDashBoard];
                 }else if(self->StartQCRetake==YES)
                 {
                     [self startQCRetake];
                 }else
                 {
                     [self StartInspection];
                 }
             }
             else
             {
                 
                 dispatch_async(dispatch_get_main_queue(), ^{
                     UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"" message:ALREADYLOGGEDIN  preferredStyle:UIAlertControllerStyleAlert];
                     
                     UIAlertAction * action =[UIAlertAction actionWithTitle:OKaction style:UIAlertActionStyleCancel handler:^(UIAlertAction * action)
                                              {
                         
                         LoginViewController * login=[[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
                         
                         UINavigationController * nav =[[UINavigationController alloc]initWithRootViewController:login];
                         nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
                         [self SetTrasitionEffetct:nav];
                     }];
                     
                     [alert addAction:action];
                     [self presentViewController:alert animated:YES completion:nil];
                 });
             }
             
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
         {
             NSLog(@"%@",error);
            
            //Added on 19 Nov'24
            NSDate *endTime = [NSDate date];
            NSTimeInterval timeTaken = [endTime timeIntervalSinceDate:startTime];
            NSString *timeTakenString = [NSString stringWithFormat:@"%.3f Sec", timeTaken];
            
            NSInteger number = error.code;
            NSString *numberString = [NSString stringWithFormat:@"%ld", (long)number];
            [APIStatusLogger logAPIStatus:TokenVarification userName:username timeTaken:timeTakenString withMessage:numberString];
            //End on 19 Nov'24
             
             if([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]==NotReachable)
             {
                 [self ShowAlert:NoInternetConnection];
             }
             else if( error.code == -1001 || error.code == -1009 )//-1001 --> request timed out
             {
                 [self ShowAlert:slowInternet];
             }
             else
             {
                 [self ShowAlert:PleaseTryAgain];
             }
            
            self->_activityView.hidden=YES;
            [self->_activityindicator stopAnimating];
            self->_listButton.userInteractionEnabled = YES;
            self->_qcretakebtn.userInteractionEnabled = YES;
         }];
        
    });
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    
}
Editor is loading...
Leave a Comment