我目前正在實施從應用程序到我的web服務的連接,此刻我想弄清楚什麼是正確的方式做到這一點。我決定使用AFNetworking庫進行連接,並通過AFNetworking處理JSON輸出。我在這裏讀到,實現這種異步的最佳方式是使用回調委託。這裏是我的兩個班級的鏈接。我真的很喜歡我的實施,特別是要改進什麼。這是正確的方式來消費與AFNetworking的網絡服務
代碼:
#import "SLLoginViewController.h"
@interface SLLoginViewController()
@end
@implementation SLLoginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
sharedWebService = [SLWebService sharedWebService];
[sharedWebService setDelegate:self];
[sharedWebService login:@"[email protected]" withPassword:@"12345"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)requestFinished:(NSString *)name withResult:(NSDictionary *)result andError:(NSError *)error
{
NSLog(@"%@", result);
}
@end
的WebService
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
@class SLWebService;
@protocol SLWebServiceDelegate <NSObject>
@required
- (void)requestFinished:(NSString *)name withResult:(NSDictionary *)result andError:(NS
Error *)error;
@end
@interface SLWebService : NSObject
{
NSURL *baseURL;
AFHTTPClient *client;
}
@property id <SLWebServiceDelegate> delegate;
+ (id)sharedWebService;
- (void)login:(NSString *)email withPassword:(NSString *)password;
- (NSError *)generateError:(NSString *)description domain:(NSString *)domain;
@end
#import "SLWebService.h"
@implementation SLWebService
@synthesize delegate;
+ (id)sharedWebService
{
static SLWebService *sharedWebService = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedWebService = [[self alloc] init];
});
return sharedWebService;
}
-(id)init
{
self = [super init];
if(self)
{
baseURL = [[NSURL alloc] initWithString:@"http://www.domain.de/"];
client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
}
return self;
}
-(void)login:(NSString *)email withPassword:(NSString *)password
{
NSDictionary *params = @{@"email":email, @"password":password};
NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"login.php" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSString *result = [JSON objectForKey:@"result"];
if ([result isEqualToString:@"fail"])
{
[[self delegate] requestFinished:@"login" withResult:nil andError:[self generateError:[JSON objectForKey:@"reason"] domain:@"de.Searchlight.WebServiceError"]];
}
else
{
NSDictionary *dic = @{@"firstname":[JSON objectForKey:@"firstname"], @"lastname":[JSON objectForKey:@"lastname"], @"gender":[JSON objectForKey:@"gender"]};
[[self delegate] requestFinished:@"login" withResult:dic andError:[self generateError:[JSON objectForKey:@"reason"] domain:@"de.Searchlight.WebServiceError"]];
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *err, id JSON)
{
[[self delegate]requestFinished:@"login" withResult:nil andError:err];
}];
[operation start];
}
-(NSError *)generateError:(NSString *)description domain:(NSString *)domain
{
NSError *error;
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : description};
error = [NSError errorWithDomain:domain code:200 userInfo:userInfo];
return error;
}
@end
在您的問題中發佈您的代碼,而不是鏈接到pastebin。否則,您可能會冒你的問題被降低並關閉。詳細信息請參見[faq]。 – dandan78