获取当前的webview URL并登录

时间:2015-07-05 13:28:17

标签: ios uiwebview

我尝试获取当前的URL,因为当我在我的应用程序中输入错误时,我会显示登录页面。我不希望这个页面显示出来。所以,我使用解决方案获取当前网址,如果此网址为http://www.example.com/account/login/,我想创建一个操作。但是目前,当我尝试这个解决方案时,我的响应为空。

 NSString * currentURL = _loadscreen.request.URL.absoluteString;
    if ([currentURL isEqualToString:@"http://www.example.com/account/login/"]) {
        NSLog(@"WARNING");
    }
    else
    {
        NSLog(@"URL is %@",currentURL); <- return (null).
    }

我使用此代码自动登录用户:

NSString *identifiant = [[NSUserDefaults standardUserDefaults] valueForKey:@"Identifiant"];
    NSString *password = [[NSUserDefaults standardUserDefaults] valueForKey:@"Password"];
    if((identifiant) && (password))
    {
        //Configure URL
        NSString* host = @"http://www.example.com";
        NSString* complement = @"/account/login";



        NSString* user_name = [[NSUserDefaults standardUserDefaults] valueForKey:@"Identifiant"];
        NSString* password = [[NSUserDefaults standardUserDefaults] valueForKey:@"Password"];
        //Add URL + Folder
        NSString *urlString = [NSString stringWithFormat:@"%@%@", host, complement];


        //NSURL *url = [NSURL URLWithString:urlString];
        NSURL *url = [NSURL URLWithString:urlString];


        //NSURLRequest *request = [NSURLRequest requestWithURL:url];

        //Add some informations POST
        NSString *post = [NSString stringWithFormat:@"user_name=%@&password=%@&submit=submit", user_name, password];;


        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];


        NSOperationQueue *queue = [[NSOperationQueue alloc] init];



        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
         {
             if ([data length] > 0 && error == nil) [_loadscreen loadRequest:request];
             else if (error != nil) NSLog(@"Error: %@", error);
         }];
    }
    else
    {


    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"login"];
    [self presentViewController:controller animated:YES completion:nil];
    }
    NSString * currentURL = _loadscreen.request.URL.absoluteString;
    if ([currentURL isEqualToString:@"http://www.example.com/account/login/"]) {
        NSLog(@"WARNING");
    }
    else
    {
        NSLog(@"URL is %@",currentURL);
    }

它的工作正常,如果用户输入好的ID,如果没有,则显示登录页面,但我不想要。

0 个答案:

没有答案