获取UIWebView URL的查询

时间:2012-07-16 06:43:16

标签: iphone objective-c xcode

嘿伙计们,我正试图找到一种方法来专门从我URL当前已加载的UIWebView中获取查询字符串。

例如:

www.google.com?page=235

我希望最终得到NSString,文字“page = 235”。

甚至更好的页面位于NSData标题键,235位于ID键。

我基本上试图将查询字符串组织成可用的数据。

在我的项目中,我有一个按钮:

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

我通过检查绝对URL来查看该按钮是否被触发。

有没有办法以我想要的方式描述信息?

2 个答案:

答案 0 :(得分:4)

我创建了你想要的演示,这将进一步描述..

以下是viewcontroller.h文件代码

#import <UIKit/UIKit.h>

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

这可能有助于解决您的问题 感谢

以下是viewcontroller.m文件代码

#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

答案 1 :(得分:1)

要获取查询字符串,请获取NSURL对象的查询字符串。

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

有关详情,请参阅NSURL class文档中的访问网址部分