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