0

I'm pretty new to iOS development, and can't find an answer that helps my problem anywhere. I'm making an app where the user must first login, using a UIWebView. Once the user logs in, they are taken to the app, but in the background the server has passed a unique authentication key for each user. Example The user logs in at the URL www.example.com/login. on a successful login the URL changes to www.example.com/key/jsaeglihndzlgaskn with the end bit being the key I need to get. I have a method which gets the last segment of the URL but it takes the starting URL not the one that changes.

    NSString *absoluteString= _webView.request.URL.absoluteString;
    NSArray* foo = [absoluteString componentsSeparatedByString: @"/"];
    NSUInteger arrsize = [foo count];//count the size of array
    NSString* bar = foo[arrsize-1];//get last element

Any help would be massively appreciated

Edit

This is how my code now looks, but It still doesn't function as I need it to, any help getting this to work would be greatly appreciated.

NSString *urlString = @"https://probablyrational.com/alpha/dashboard/register/?callback=inline";

NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webView loadRequest:urlRequest];


NSString* absoluteString = [url absoluteString];
NSArray* foo = [absoluteString componentsSeparatedByString: @"/"];//explode() .split()
NSUInteger arrsize = [foo count];//count the size of array
NSString* bar = foo[arrsize-1];//get last element
NSLog(@"bar %@", bar);
//bar = "?callback=inline"

if ([foo[arrsize-2] isEqualToString:@"key"]) {
    // user is authenticated
    NSLog(@"bar %@", bar);


- (BOOL)_webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *URLString = [[request URL] absoluteString];
    if ([URLString isEqualToString:@"https://probablyrational.com/alpha/key"]) {
        // The user reached step 3!
    }
    return YES;
}

EDIT 2

My code is now running but still not tracking the URL change..

        NSString *urlString = @"https://probablyrational.com/alpha/dashboard/register/?callback=inline";

    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:urlRequest];


    NSString* absoluteString = [url absoluteString];
    NSArray* foo = [absoluteString componentsSeparatedByString: @"/"];//explode() .split()
    NSUInteger arrsize = [foo count];//count the size of array
    NSString* bar = foo[arrsize-1];//get last element
    NSLog(@"bar %@", bar);
    //bar = "?callback=inline"

    if ([foo[arrsize-2] isEqualToString:@"key"]) {
        // user is authenticated
        NSLog(@"bar %@", bar);
    }
}

- (BOOL)webView:(UIWebView *)webView didFinishLoading:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *absoluteString = [[request URL] query];
    if ([absoluteString isEqualToString:@"https://probablyrational.com/alpha/key"]) {
        // The user reached step 3!
    ( NSLog(@"this is  the code you're looking for"));
    return YES;
    }
    else
    {

        ( NSLog(@"this is  not code you're looking for"));
        return 0;

    }
}

Any clue how I can get this to work would be life saving, i'm stuck at this point

  • 1
    This answer should be what you need - http://stackoverflow.com/questions/2834946/get-notified-about-a-page-change-in-uiwebview – davedavedave Mar 25 '16 at 08:38
  • Your code seems legit, for debugging purposes, try to do it step by step, i.e. first NSLog your absoluteString and see what it logs, I am not sure maybe if you change it to NSMutableString it would work. Please let us know how you figured it out – mfaani Mar 25 '16 at 11:03
  • I tried using that one and couldn't get it to work, I'll give it another go though, thank you – Nick Jennett Mar 25 '16 at 11:07
  • The first parameter of your `webView:shouldStartLoadWithRequest:` has an underscore, assuming you're setting the delegate correctly, which you didn't show us, ¿that could the problem? – calql8edkos Mar 25 '16 at 21:07
  • Thanks, I fixed that and that method is now being called, but still isn't functioning properly. I need it to get the URL after the page changes, instead it's firing straight away, any ideas? – Nick Jennett Mar 25 '16 at 21:18

1 Answers1

0

you can use [foo lastObject]; to get the last component.

Pratik Jamariya
  • 810
  • 1
  • 10
  • 35