在我的应用程序中,我使用的是一个静态库。在那个库中,我实现了与服务器建立连接的代码。对于我使用的服务器交互,NSURLSession
但它延迟了 UI 响应以避免它我已经开始使用NSURLConnection
委托方法现在我从服务器获取响应但在这里我不知道如何将响应从完成加载方法发送回实际代码。
在我的团队中,我想将此库分发给 iphone 和 ipad 开发工程师。他们对我在静态库中实现的所有服务器相关代码没有任何控制。因此,请提前向我展示我的问题的解决方案,谢谢。
以下是我在一类静态库中使用的代码:
静态类:
.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
导入“静态类.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];
}