Necro
Necro

Reputation: 333

Get the Query Of a UIWebView URL

Hey guys I'm trying to find a way to specifically grab the Query String out of the URL that my UIWebView currently has loaded up.

So for example:

www.google.com?page=235

I want to end up with a NSString with the text "page=235".

Or even better Page at a NSData Key of Title and 235 at a Key of ID.

Im basically trying to organise the Query String into useable data.

In my project I have a button:

[HTML appendString:@"<a href='didtap://goto?Page=56'>Post Comment</a>"];

That i access with checking the absolute URL to see if the button was fired.

Is there any way to profile the information the way i want to?

Upvotes: 2

Views: 1679

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 340070

To get the query string, get the query string of a NSURL object.

NSURL *url = …;
NSString *queryString = url.query;

For more information, see Accessing the Parts of the URL in the NSURL class documentation.

Upvotes: 1

Nimit Parekh
Nimit Parekh

Reputation: 16864

I create the demo what you want get which is describe further..

Following is the viewcontroller.h file code

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    NSURL *Url;
}
@property(nonatomic,retain)IBOutlet UIWebView *webVctr;
@end

This may helping to solve your problem Thanks

Following is the viewcontroller.m file code

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webVctr = _webVctr;


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"%@",request);
    Url = [request URL];
    //    if ([[UrlParts objectAtIndex:(1)] isEqualToString:@"NeverGonnaFindMe"]) {
    // CONVERT TO STRING AN CLEAN
    NSString *urlResources = [Url resourceSpecifier];
    urlResources = [urlResources stringByReplacingOccurrencesOfString:@"?" withString:@"/"];

    // SEPORATE OUT THE URL ON THE /
    NSArray *urlResourcesArray = [urlResources componentsSeparatedByString:@"/"];
    // THE LAST OBJECT IN THE ARRAY
    NSString *urlParamaters = [urlResourcesArray objectAtIndex:([urlResourcesArray count]-1)];
    // SEPORATE OUT THE URL ON THE &
    NSArray *urlParamatersArray = [urlParamaters componentsSeparatedByString:@"&"];

    if([urlParamatersArray count] == 1) {
        NSString *keyValue = [urlParamatersArray objectAtIndex:(0)];
        NSArray *keyValueArray = [keyValue componentsSeparatedByString:@"="];

        if([[keyValueArray objectAtIndex:(0)] isEqualToString:@"page"]) {
            NSLog(@"%@",[keyValueArray objectAtIndex:1]);
        }

        [self dismissModalViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}
// ACTIVITY INDICATOR
- (void)webViewDidStartLoad:(UIWebView *)webView{
///    [_activityIndicator startAnimating];
    NSLog(@"Resting to server");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    [_activityIndicator stopAnimating];
    NSLog(@"Finish request  server");  
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"Error");
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *fullAuthUrlString =[[NSString alloc] initWithFormat:@"www.google.com?page=235"];
    NSURL *authUrl = [NSURL URLWithString:fullAuthUrlString];
    NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL:authUrl];
    [self.webVctr loadRequest:myRequest];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

Upvotes: 4

Related Questions