0

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];

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Naresh
  • 363
  • 1
  • 18

2 Answers2

1

You can either offer API to notify that response has been read from the connection, or you can send a notification.

The first can be accomplished by either implementing a delegate protocol and setting the delegate in the using app, or by using block-based API, where the using app would set a block to handle events. You see these two patterns very often in system-provided API, including NSUrlConnection.

Another option is to use notifications. You register for a particular notification name in the using app, and in the lib you post once your connection returns data.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
1

You need to implement a protocol in your static library like:

@protocol DataReciverDelegate <NSObject>

@required
- (void)dataReceived:(NSData *)data;

@end

Also declare a property there like:

@property (nonatomic, weak) id <DataReciverDelegate>delegate;

In your static library implementation, implement the connectionDidFinishLoading like:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [_delegate dataReceived:_dataYouReceived];
}

Now you need to implement the DataReciverDelegate in the class you need to get the data, and When you create the object of your static library class, set the delegate.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Hi Midhun thanks for your reply. You are telling that implementation of DataReciverDelegate in the class you need to get the data means I have to redefined in class how i defined in static library right? And one more thing is I have to implement delegate method in class. Next how can i set delegate for library object in class please let me know. – Naresh Feb 12 '14 at 07:22
  • @Naresh: You need to implement the DataReceiverDelegate in the class you need to get the data. When you create the object of static library class set the delegate to self – Midhun MP Feb 12 '14 at 08:20
  • Hi midhun really thanks a lot for your support. I am little bit confuse in setting delegate can you tell me where can i set delegate. I had edited my question with sample code. Can you please through the code and let me know the place. – Naresh Feb 12 '14 at 09:19
  • Set it after `StaticClass *object1=[[StaticClass alloc]init];` like object1.delegate = self; and implement the dataReceived` method in `Class1` – Midhun MP Feb 12 '14 at 09:23