In my application I am using one static library. In that library I implemented code for establish a connection with the server. For server interaction I used NSURLSession
but it's delaying the UI response to avoid it I have started using NSURLConnection
delegate methods now I am getting the response from server but here I don't know how to send the response back to actual code from did finish loading method.
In my team I want to distribute this library to both iphone and ipad development engineers. They don't have any control on server related code everything I implemented in static library. So please show me the solution for my problem thanks in advance.
The below is the code which I used in one class of static library:
StaticClass:
.h File
@interface StaticClass : NSObject<NSURLConnectionDelegate,NSURLSessionDelegate>
{
NSMutableDictionary *responseDictionary;
NSUserDefaults *serviceURlInUserDefaults;
NSData *responseData;
}
@property (nonatomic, weak) id <DataReciverDelegate>delegate;
@property(strong,nonatomic)NSData *responseData;
-(void)loginWithUsername:(NSString *)name password:(NSString*)password serviceUrl:(NSString*)serviceUrl domainName:(NSString*)domainName ;
@end
import "StaticClass.h"
@protocol DataReciverDelegate <NSObject>
@required
- (void)responseDictionary:(NSDictionary *)response;
@end
@implementation StaticClass
@synthesize responseData;
-(void)loginWithUsername:(NSString *)name password:(NSString*)password serviceUrl:(NSString*)serviceUrl domainName:(NSString*)domainName
{
NSString *ApiStr=[NSString stringWithFormat:@“http://login.com”];
NSURL *Url=[[NSURL alloc]initWithString:[loginApiStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *ApiRequest=[NSURLRequest requestWithURL:loginUrl];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:ApiRequest delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
self.responseData=data;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
responseDictionary=[NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:nil];
[_delegate responseDictionary:responseDictionary];
}
@end
Response where I want to use is in class1 :
Here please let me know how can i include that delegate which i created in static library class
@interface Class1 : NSObject<NSURLConnectionDelegate,NSURLSessionDelegate>
{
}
@end
@implementation Class1
-(void)login
{
StaticClass *object1=[[StaticClass alloc]init];
[object loginWithUsername:@“AAA” password:@“BBB” serviceUrl:url domainName:dname];
}