如何使用参数调用IOS中的服务?
我已经给出了一个url和三个参数来在应用程序中使用和调用这些服务,我必须创建一个带有id、用户名和密码的登录页面,如果它们匹配,那么用户应该移动到下一个页面,我对如何启动代码有点困惑。有人能帮我吗。请让我知道你有任何例子的代码 我有登录名、密码和apikey匹配的代码,如果都正确,请导航到其他页面如何使用参数调用IOS中的服务?,ios,objective-c,xcode7,Ios,Objective C,Xcode7,我已经给出了一个url和三个参数来在应用程序中使用和调用这些服务,我必须创建一个带有id、用户名和密码的登录页面,如果它们匹配,那么用户应该移动到下一个页面,我对如何启动代码有点困惑。有人能帮我吗。请让我知道你有任何例子的代码 我有登录名、密码和apikey匹配的代码,如果都正确,请导航到其他页面 -(void)login { NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSe
-(void)login
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"Your Login URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"*/*" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSString *mapData = [NSString stringWithFormat:@"username=admin&password=testing&api_key=bf45c093e542f057c123ae7d6"];
NSData *postData = [mapData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(error!=nil)
{
NSLog(@"error = %@",error);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self checkUserSuccessfulLogin:json];
});
}
else{
NSLog(@"Error : %@",error.description);
}
}];
[postDataTask resume];
}
- (void)checkUserSuccessfulLogin:(id)json
{
// NSError *error;
NSDictionary *dictionary = (NSDictionary *)json;
if ([[dictionary allKeys] containsObject:@"login"])
{
if ([[dictionary objectForKey:@"login"] boolValue])
{
if(checkBoxSelected == YES)
{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSString* textField1Text = usernameField.text;
[defaults setObject:textField1Text forKey:@"textField1Text"];
NSString *textField2Text = passwordField.text;
[defaults setObject:textField2Text forKey:@"textField2Text"];
[defaults synchronize];
}
NSString *strID = [[NSUserDefaults standardUserDefaults] stringForKey:@"textField1Text"];
NSString *strPWD = [[NSUserDefaults standardUserDefaults] stringForKey:@"textField2Text"];
[[NSUserDefaults standardUserDefaults] setValue:[dictionary objectForKey:@"user_id"] forKey:@"CurrentUserLoggedIn"];
NSString *strUser = [[NSUserDefaults standardUserDefaults] stringForKey:@"CurrentUserLoggedIn"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self saveLoginFileToDocDir:dictionary];
ItemManagement *i = [[ItemManagement alloc]init];
[self.navigationController pushViewController:i animated:YES];
}
else
{
NSLog(@"Unsuccessful, Try again.");
UIAlertView *alertLogin = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Wrong Username Or Password" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertLogin show];
}
}
}
- (void)saveLoginFileToDocDir:(NSDictionary *)dictionary
{
NSArray *pListpaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *pListdocumentsDirectory = [pListpaths objectAtIndex:0];
NSString *path = [pListdocumentsDirectory stringByAppendingPathComponent:@"Login.plist"];
BOOL flag = [dictionary writeToFile:path atomically:true];
if (flag)
{
NSLog(@"Saved");
}
else
{
NSLog(@"Not Saved");
}
}
也许它肯定会对你有所帮助。你知道如何调用url吗?