webMethod.h
#import <Foundation/Foundation.h>
@protocol ServerReq<NSObject>
-(void)dataResponse:(NSDictionary*)responseDict;
@end
@interface WebMethod : NSObject
-(void)urlElements:(NSString*)url bodyMethod:(NSString *)body;
@property id<ServerReq>delegate;
//propertys
@property NSURLSession *URLSession;
@property NSMutableURLRequest *URLRequest;
@property NSURLSessionDataTask *dataTask;
@end
webmethod.m
-(void)urlElements:(NSString*)url bodyMethod:(NSMutableString *)body{
self.URLSession=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.URLRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
if (body.length>0) {
self.URLRequest.HTTPMethod=@"POST";
NSString *dataToServer=body;
NSData *dataToPassToServer=[dataToServer dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[self.URLRequest setHTTPBody:dataToPassToServer];
}else{
self.URLRequest.HTTPMethod=@"GET";
}
self.dataTask=[self.URLSession dataTaskWithRequest:self.URLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
if (data!=nil)
{
NSMutableDictionary *responseD = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",responseD);
if ([self.delegate respondsToSelector:@selector(dataResponse:)]) {
[self.delegate dataResponse:responseD];
}
}else
{
NSLog(@"data nil");
}
}];
[self.dataTask resume];
}
@end
viewcontroller.h
#import <UIKit/UIKit.h>
#import "WebMethod.h"
@interface ViewController : UIViewController<ServerReq>
@end
viewcontroller.m
#import "ViewController.h"
#import "WebMethod.h"
#import "DebugLogger.h"
@interface ViewController (){
WebMethod *re;
CustomAlert *alert;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
re = [[WebMethod alloc]init];
re.delegate=self;
NSString *str=@"hi";
[DebugLogger logString:str :@"str" :1];
// Alert
[Globals showAlertWithString:@"HELLO" Title:@"ALERT" CancelBtn:@"cn" SucessBtn:@"ok"];
//[self request:2];
}
-(void)request:(int)requestId{
switch (requestId) {
case 1:
[re urlElements:@"https://rss.itunes.apple.com/api/v1/us/ios-apps/new-apps-we-love/all/10/explicit.json" bodyMethod:nil];
break;
case 2:
[re urlElements:@"https:/wens.net/api/Mobile/Login" bodyMethod:[NSMutableString stringWithFormat:@"UserName=%@&Password=%@",@"guest@wens.com",@"guest"]];
break;
default:
break;
}
}
-(void)dataResponse:(NSDictionary *)responseDict{
NSLog(@"%@",responseDict);
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon