2012-07-16 90 views
2

嘿傢伙我試圖找到一種方法來專門從URL中獲取查詢字符串,我的UIWebView當前已加載。獲取UIWebView URL的查詢

因此,例如:

www.google.com?page=235

我想用文本 「頁= 235」 一NSString結束了。

甚至更​​好的頁面在NSData標題密鑰和235在ID密鑰。

我基本上試圖把查詢字符串組織成可用的數據。

在我的項目我有一個按鈕:

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

,我和檢查絕對URL,看看是否按鈕被解僱了訪問。

有沒有辦法以我想要的方式分析信息?

回答

1

要獲取查詢字符串,請獲取查詢 NSURL對象的字符串。

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

欲瞭解更多信息,請參閱訪問URL的部分NSURL class文檔。

+0

好吧,這只是像100行代碼lol保存。 – Necro 2012-08-23 00:08:14

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 
+0

你是個很聰明的人,謝謝。 – Necro 2012-07-16 07:06:18