从另一个类

时间:2015-07-14 07:47:40

标签: ios objective-c nsurlconnection nsurlconnectiondelegate

我是目标c的初学者。我有两个类Connectivity和SignInViewController,我想要的是在另一个类的单个调用中调用类中定义的方法。我开始知道,我可以通过protocoldelegation来实现它,但仍然想知道是否有任何简单的方法可以做到这一点。当我调试代码时,我可以看到控件在执行+ SigninViewController方法后转到(void)connectWithURL:(NSURL *)URL withData:(NSString *)postedData。但我想要的是首先执行连接类的所有方法(下面定义的委托方法),然后控制应该返回到SigninViewController。希望,我清楚地问了。

`// connectivity.h

 #import<Foundation/Foundation.h>
@interface Connectivity : NSObject<NSURLConnectionDelegate, NSURLConnectionDelegate>
+(void)connectWithURL:(NSURL *)URL withData:(NSString *)postedData;
@end 

// connectivity.m

#import "Connectivity.h"
NSMutableData *_receivedData;
@implementation Connectivity
+(void)connectWithURL:(NSURL *)URL withData:(NSString *)postedData
{
//Create the Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
NSLog(@"posted url %@", URL);
//Create The Method "POST"
[request setHTTPMethod:@"POST"];
//header
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded"   forHTTPHeaderField:@"Content-Type"];

//Pass the String to the Server
NSString *postString = [NSString stringWithString:postedData];

//Check the Passed Value
NSLog(@"post string =%@", postedData);
//Convert the String to Data
NSData *data1 = [postedData dataUsingEncoding:NSUTF8StringEncoding];
//Apply the Data to the Body
[request setHTTPBody:data1];
//connection to the webserver

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];
[connection start];
}
#pragma mark NSURLConnection Delegate Methods

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

[_receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.

[_receivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data

NSLog(@"Succeeded! Received %lul bytes of data",[_receivedData length]);
NSString *responeString = [[NSString alloc] initWithData:_receivedData
                                                encoding:NSUTF8StringEncoding];

connection = nil;
_receivedData = nil;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

connection = nil;
_receivedData = nil;

// inform the user

NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
@end

//我正在调用方法     的(SignInViewController.m)

#import "SignInViewController.h"
#import "AlertMessageViewController.h"
#import "Connectivity.h"

@interface SignInViewController ()

@end

@implementation SignInViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)loginCustomUser
{

if([[self.emailTextField text] isEqualToString:@""] || [[self.passwordTextField text] isEqualToString:@""])
{
  [self presentViewController:[AlertMessageViewController alertWithTitle:@"Error" withMessage:@"Enter email and Password" preferredStyle:UIAlertControllerStyleAlert] animated:YES completion:nil];
}
else
{
NSString *data = [NSString stringWithFormat:@"{\"rqBody\":{\"emailId\":\"%@\",\"password\":\"%@\",\"userId\":\"%@\",\"idType\":\"%@\",\"firstName\":\"%@\",\"lastName\":\"%@\",\"contactNumber\":\"%@\",\"firstLineOfAddress\":\"%@\",\"localityName\":\"%@\",\"city\":\"%@\",\"state\":\"%@\",\"country\":\"%@\",\"roleType\":\"%@\",\"paymentProfile\":\"%@\",\"paymentDate\":\"%@\",\"registerationStatus\":\"%@\",\"loggedStatus\":\"%@\",\"lastLoggedInDate\":\"%@\",\"registerationDate\":\"%@\",\"profileStatus\":\"%@\"}}",[self.emailTextField text],[self.passwordTextField text],@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];

    NSURL *mainUrl= [NSURL URLWithString:@"http://192.168.1.5:8080/referamaid/app/noauth/signin"];

    [Connectivity connectWithURL:mainUrl withData:data];

}
}
@end`

0 个答案:

没有答案