1
我创建了一个sharedClient传递值到sharedClient
+ (id)sharedClient {
static MyClient *__instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLComponents *urlComponents = [NSURLComponents componentsWithString:BASE_URL];
urlComponents.user = @"username";
urlComponents.password = @"password";
NSURL *url = urlComponents.URL;
NSLog(@"%@",url);
__instance = [[MyClient alloc] initWithBaseURL:url];
});
return __instance;
}
但正如你可以看到我已经硬编码的用户名和密码。将变量传递到此类和sharedClient的最佳方式是什么?目前我打电话,像这样
- (IBAction)login:(id)sender {
[SVProgressHUD show];
[[MyClient sharedClient]getPath:@"https://stackoverflow.com/users/current.json"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *authToken = [responseObject valueForKeyPath:@"user.api_key"];
[self.credentialStore setAuthToken:authToken];
[SVProgressHUD dismiss];
[self performSegueWithIdentifier: @"loginSuccess" sender: self];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.response.statusCode == 500) {
[SVProgressHUD showErrorWithStatus:@"Something went wrong!"];
} else {
NSData *jsonData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:nil];
NSString *errorMessage = [json objectForKey:@"error"];
[SVProgressHUD showErrorWithStatus:errorMessage];
}
}];
[SVProgressHUD dismiss];
}
我一直在思考的实例变量,但不确定什么是最好的方式做,这将是客户端。