如何从ios中的静态库得到响应
问题描述:
在我的应用程序中,我使用了一个静态库。在那个库中,我实现了与服务器建立连接的代码。对于服务器交互,我使用了NSURLSession
,但它延迟了UI响应以避免它我已经开始使用NSURLConnection
委托方法现在我得到了服务器的响应,但在这里我不知道如何将响应发回到实际的代码完成加载方法。如何从ios中的静态库得到响应
在我的团队中,我想将这个库分发给iphone和ipad开发工程师。他们没有任何我在静态库中实现的与服务器相关的代码的任何控制。所以请提前向我展示我的问题的解决方案。
下面是我在一个类的静态库中使用的代码:
StaticClass:
.h文件中
@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
进口 “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
我想使用的响应在class1中:
这里请让我知道我可以包括委托我在静态库类中创建
@interface Class1 : NSObject<NSURLConnectionDelegate,NSURLSessionDelegate>
{
}
@end
@implementation Class1
-(void)login
{
StaticClass *object1=[[StaticClass alloc]init];
[object loginWithUsername:@“AAA” password:@“BBB” serviceUrl:url domainName:dname];
}
答
您可以提供API来通知响应已经从连接读取,或者您也可以发送通知。
第一个可以通过实现委托协议并在使用应用程序中设置委托或使用基于块的API来完成,其中使用应用程序将设置块来处理事件。您经常在系统提供的API中看到这两种模式,其中包括NSUrlConnection
。
另一种选择是使用通知。您在使用应用程序中注册特定的通知名称,并在您连接返回数据后发布的库中。
答
你需要在你喜欢的静态库来实现一个协议:
@protocol DataReciverDelegate <NSObject>
@required
- (void)dataReceived:(NSData *)data;
@end
还声明属性存在,如:
@property (nonatomic, weak) id <DataReciverDelegate>delegate;
在静态库实现,实现connectionDidFinishLoading
,如:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[_delegate dataReceived:_dataYouReceived];
}
现在您需要实施DataReciverDelegate
中需要获取数据的类,并且当您创建静态库类的对象时,请设置委托。
嗨Midhun感谢您的回复。你说的是在你需要获得数据的类中实现DataReciverDelegate意味着我必须在类中重新定义我如何在静态库中定义的权利?还有一件事是我必须在类中实现委托方法。接下来我如何在类中设置库对象的委托,请让我知道。 – Naresh
@Naresh:您需要在需要获取数据的类中实现DataReceiverDelegate。当你创建静态库类的对象时,将代理设置为自己 –
嗨中部真的非常感谢你的支持。我有点混淆在设置委托你能告诉我在哪里可以设置委托。我用示例代码编辑了我的问题。你可以通过代码请让我知道这个地方。 – Naresh