Thursday, July 27, 2017

Webservice with nsurlsession

App Delegate:

#import <UIKit/UIKit.h>
#import "ASIFormDataRequest.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,ASIHTTPRequestDelegate>
{
    ASIFormDataRequest *serverRequest;
    NSString *urlType;
    UIView *spinerView;
    UIActivityIndicatorView *activityView;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong)ASIFormDataRequest *serverRequest;
-(void)PostWebServer:(NSMutableDictionary *)dict type:(NSString *)webServive;
-(void)showView;
-(void)hideView;
@end

AppDelegate.m


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}
-(void)PostWebServer:(NSMutableDictionary *)dict type:(NSString *)webServive
{
    if(serverRequest)
    {
        [self.serverRequest cancel];
        [self.serverRequest clearDelegatesAndCancel];
    }
    urlType=webServive;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *strurl=[NSString stringWithFormat:@"%@",webServive];
    NSURL *url=[NSURL URLWithString:strurl];
    NSLog(@"WEB SERVICE URL------%@",url);
    serverRequest=[ASIFormDataRequest requestWithURL:url];
    [serverRequest setPostBody:[NSMutableData dataWithData:jsonData]];
    [serverRequest setRequestMethod:@"POST"];
    [serverRequest setDelegate:self];
    [serverRequest startAsynchronous];
}
-(void)showView
{
    if ([[UIScreen mainScreen] bounds].size.height==568)
    {
        spinerView=[[UIView alloc] initWithFrame:CGRectMake(110, 240, 100, 100)];
    }
    else if([[UIScreen mainScreen] bounds].size.height==480)
    {
        spinerView=[[UIView alloc] initWithFrame:CGRectMake(110, 200, 100, 100)];
    }
    else
    {
        spinerView=[[UIView alloc] initWithFrame:CGRectMake(135, 270, 100, 100)];
    }
    spinerView.backgroundColor=[UIColor lightGrayColor];
    spinerView.layer.cornerRadius=8;
    self.window.userInteractionEnabled=NO;
    [self.window addSubview:spinerView];
    activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityView.frame=CGRectMake(35, 30, 25, 25);
   
    UILabel *loading=[[UILabel alloc] initWithFrame:CGRectMake(15, 55, 100, 20)];
    loading.backgroundColor=[UIColor clearColor];
    loading.text=@"Loading";
    loading.textColor=[UIColor whiteColor];
    [spinerView addSubview:loading];
    [spinerView addSubview:activityView];
    [activityView startAnimating];
}

-(void)hideView
{
    self.window.userInteractionEnabled=YES;
    [spinerView removeFromSuperview];
}

-(void)requestFinished:(ASIHTTPRequest *)request{
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:[request responseData] options:0 error:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:urlType object:self userInfo:dict];
}

-(void)requestFailed:(ASIHTTPRequest *)request{ 
}

ViewController:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface ViewController : UIViewController<UITextFieldDelegate>
{
    AppDelegate *delObj;

}
@property(nonatomic,retain)IBOutlet UITextField *usernameTextfield;
@property(nonatomic,retain)IBOutlet UITextField *passwordTextfield;
-(IBAction)MatchApiRequest:(id)sender;

@end

viewcontroller.m

#import "ViewController.h"
#import "Reachability.h"
#import "DetailViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    delObj=(AppDelegate *)[[UIApplication sharedApplication] delegate];

}
-(IBAction)MatchApiRequest:(id)sender
{
   
    if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]==NotReachable)
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Please check your internet connection, try again" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        alert.tag=100;
        [alert show];
    }
    else
    {
        //connection available
       
        [delObj showView];
       
        [[NSNotificationCenter defaultCenter]removeObserver:self name:@"http://aiv2.ibbtrade.com/login" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMatchingResponse:) name:@"http://aiv2.ibbtrade.com/login" object:nil];
        NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
//        [dict setValue:self.usernameTextfield.text forKey:@"uName"];
//        [dict setValue:self.passwordTextfield.text forKey:@"pWord"];
        dict=@{@"username":@"valuator1",@"password":@"1234567",@"client_id":@"autoinsp",@"client_secret":@"test@123",@"ip":@"172.16.24.82",@"simno":@"7849ecbba6dde263",@"lat":@"12.975578333333333",@"lng":@"77.71165500000001",@"app_type":@"1"};
        NSLog(@"%@",dict);
        [delObj PostWebServer:dict type:@"http://aiv2.ibbtrade.com/login"];
//        accessToken = Gzzi4FFEQDkd7rx16GbKgpjE0IhdZyJxREwy8PVm;

    }
}
-(void)getMatchingResponse:(NSNotification *)dict{
   
    [delObj hideView];
    NSDictionary *dictValue=[dict userInfo];
   if([[dictValue objectForKey:@"status"] isEqualToString:@"true"])
   {
       DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DashboardView"];
       [dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
       [self presentViewController:dvc animated:YES completion:nil];

   }
    else
    {
        NSLog(@"NO");

    }
//    CategoryListArray=(NSMutableArray *)[NSArray arrayWithArray:[dictValue valueForKey:@"Response"]];
//    NSLog(@"%@",CategoryListArray);
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


DetailView:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController
{
    NSMutableArray *yearArray;
}
@end



#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    yearArray = [[NSMutableArray alloc] init];
   
    for (int i = 1990; i < 2018; i++)
    {
//        NSString *string = i;
        NSLog(@"%d",i);
        [yearArray addObject:[NSString stringWithFormat:@"%d",i]];
        // Do stuff...
    }
}


No comments:

Post a Comment