我的iOS应用中的WebService响应问题

时间:2015-01-27 06:57:54

标签: ios objective-c iphone xcode asiformdatarequest

我正在使用Web服务句柄类来实现我的app中使用的所有Web服务(这个单例类名'ServiceHandle')。从我的viewController调用服务及其响应通过delegate从我的viewController返回。这是我的过程当我从不同的viewcontroller连续调用webservice,然后冲突响应时。(无法获得我正在调用的服务的服务响应。一次服务响应发生了变化)。如何解决这个问题?。请查看我的代码

ServiceHandle.h

@protocol serviceDelegate <NSObject>

-(void)successFullResponse:(ASIHTTPRequest *)req;
-(void)failedResponse:(ASIHTTPRequest *)req;

@end

@interface ServiceHandle : NSObject

@property(nonatomic,strong)NSOperationQueue *queue;
@property (nonatomic,weak)id<serviceDelegate>delegate;

+ (id)sharedInstance;
-(void)loginWithUserName:(NSString *)userName;
-(void)userReg:(NSDictionary*)userDict;

@end

。      ServiceHandle.m

 static ServiceHandle *sharedInstance = nil;

 @implementation ServiceHandle
 @synthesize delegate,queue;

+(ServiceHandle *)sharedInstance{

if (sharedInstance == nil) {

    sharedInstance = [[super allocWithZone:NULL]init];
}
return sharedInstance;
}
 - (id)init
{
self = [super init];

if (self) {
    // Work your initialising magic here as you normally would


}
return self;}


-(void)loginWithUserName:(NSString *)userName{

if (![self queue]) {
    [self setQueue:[[NSOperationQueue alloc] init]];
}

NSString *path=[NSString stringWithFormat:@"%@userLogin",webserviceURL];

NSString *lattitude=[[NSUserDefaults standardUserDefaults] objectForKey:@"mylatitude"];
NSString *longitude=[[NSUserDefaults standardUserDefaults] objectForKey:@"mylongitude"];


ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:path]];
 [request setTimeOutSeconds:30];

[request setPostValue:userName forKey:@"userId"];
[request setPostValue:lattitude forKey:@"latitude"];
[request setPostValue:longitude forKey:@"longitude"];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setDidFinishSelector:@selector(requestSuccess:)];
[request setDidFailSelector:@selector(requestFail:)];
request.queuePriority = NSOperationQueuePriorityHigh;
request.delegate = self;

[[self queue]addOperation:request];

}

-(void)requestSuccess:(ASIHTTPRequest *)request{

if ([self.delegate respondsToSelector:@selector(successFullResponse:)]) {

    [self.delegate successFullResponse:request];
}
}
 -(void)requestFail:(ASIHTTPRequest *)request{

if ([self.delegate respondsToSelector:@selector(failedResponse:)]) {

    [self.delegate failedResponse:request];
}
}

ViewController.m

-(IBAction)clickLogin:(id)sender{

ServiceHandle *sharedObj = [ServiceHandle sharedInstance];
sharedObj.delegate = self;
[sharedObj loginWithUserName:@“usrName”];
 }

-(void)successFullResponse:(ASIHTTPRequest *)req{

    NSString *responseString = [req responseString];
    NSLog(@"responseString is%@",responseString);
    NSDictionary *responseDict = [responseString JSONValue];
    NSLog(@"dict is :%@",responseDict);
  }
-(void)failedResponse:(ASIHTTPRequest *)req{


  }

1 个答案:

答案 0 :(得分:0)

您可以根据网址中的唯一性来执行此操作... 要么 您可以添加参数

.h 文件

中的

@property(nonatomic) int callingClassValue; //0 for VC1, 1 for VC2, and so on 
.m 文件

中的

@synthesize callingClassValue;

并在您创建ServiceHandle对象的位置,为callingClassValue分配值。

然后根据它的值,你可以调用相对委托方法。