gpt4 book ai didi

ios - 使用 AFNetworking 3.0 从下一个 View Controller 发送特定参数以响应服务器?

转载 作者:行者123 更新时间:2023-11-29 00:58:45 25 4
gpt4 key购买 nike

我是 iOS 应用程序开发新手。我将使用 AFNetworking 3.0。当我输入手机号码时,第一眼看到。然后单击提交按钮,然后它与服务器连接,我从服务器得到以下响应:

responseData: [{"edriverId":"bd307a3ec329e10a2cff8fb87480823da114f8f4","token":"6uc4d1houfecbmjgy9ezpru9n25nw40b17cwk439j52"}]

现在我的问题是,我如何从responseData中仅获取该 token 并将其从下一个 View 发送到服务器。请帮助我。

注意:每当我们调用服务时, token 每次都会发生变化。

我的代码是:

-(void)serverconnection

{

NSString *Loginurl = [NSString stringWithFormat:@"https://24x7tracker.com/busservices/School/DriverPreLogin"];


NSDictionary *params = @{@"mobile":self.phonenumber.text,

@"archive":@"schooldb1"

};


AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];


manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];


manager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

[policy setValidatesDomainName:NO];

[policy setAllowInvalidCertificates:YES];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",nil];



manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil];


[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {


NSLog(@"Json: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

[self getdata:responseObject];


NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"responseData: %@", str);

[self performSegueWithIdentifier:@"loginsegue" sender:self];

}
failure:^(NSURLSessionTask *operation, NSError *error)

{

NSLog(@"Error: %@", error);

UIAlertController *Erroralert= [UIAlertController

alertControllerWithTitle:@" Network Connection Failed!!"

message:@"Please try again"

preferredStyle:UIAlertControllerStyleAlert];

[self presentViewController:Erroralert animated:YES completion:nil];





UIAlertAction* yesButton = [UIAlertAction

actionWithTitle:@"Ok"

style:UIAlertActionStyleDefault

handler:^(UIAlertAction * action)

{
[self resignFirstResponder];

[Erroralert dismissViewControllerAnimated:YES completion:nil];



}];

[Erroralert addAction: yesButton];


}];


}

-(void)getdata:(NSData *)data

{}

最佳答案

你可以通过两种方式做到这一点

  1. NSUserDefault

    // NSArray *temp = (NSArray *)responseObject;

    NSError *error;
    NSArray *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

    [[NSUserDefaults standardUserDefaults] setObject:[[temp objectAtIndex:0] valueForKey:@"token"] forKey:@"token"];
    [self performSegueWithIdentifier:@"loginsegue" sender:self];

在第二页

检索

获取NSUserDefaults中的String可以直接使用objectForKey

NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
  1. 像这样将字符串传递给第二页

第一页创建一个全局值以将对象存储在字符串中

@property (strong, nonatomic)          NSString *tokenString;

序列化响应对象并存储在你需要的全局值中

                   NSError *error;
NSArray *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

// in this line we assingn the current value to global value
tokenString = [[temp objectAtIndex:0] valueForKey:@"token"];
[self performSegueWithIdentifier:@"loginsegue" sender:self];

在目标 View Controller 上创建一个全局值以传递当前字符串

@property (strong, nonatomic)          NSString *fetchtokenString;

在第一个 VC 上,prepareForSegue 方法将全局化值传递给第二个页面

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

secondViewController *vc = segue.destinationViewController;
// pass the current object to second page value
vc.fetchtokenString = tokenString;

最后第二个 VC 你可以在 viewDidload 页面上获取值

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog (@"token value ==%@",fetchtokenString);
}

关于ios - 使用 AFNetworking 3.0 从下一个 View Controller 发送特定参数以响应服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37291149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com