重定向后从UIWebView获取URL

时间:2014-06-25 21:17:11

标签: uiwebview uiwebviewdelegate

我的应用程序将一个小HTML文件写入用户的临时缓存。此文件指向我们在服务器中的登录,并包含用户名和密码以及重定向到我希望用户前往的实际URL。

一切正常但是在UIWebView的方法中我需要检测最终结束的URL,因为我必须解析此URL以确定是否显示或隐藏导航。

一切正常但我无法检测到页面最终的实际URL - 我只是不断获取filePath。

我的标题如下:

#import <UIKit/UIKit.h>

@interface ViewControllerApprovals : UIViewController <UIWebViewDelegate> {
}

@property(nonatomic, retain) IBOutlet UIWebView *webView;
@property(nonatomic, retain) NSURLRequest *currentRequest;

@property(nonatomic, strong) NSString *requestType;
@property(nonatomic, strong) NSString *pathToFile;

@end

我的实施文件:

#import "ViewControllerApprovals.h"
#import "ContactsDatabase.h"
#import "UserDefaults.h"
#import "SettingsViewController.h"

@interface ViewControllerApprovals ()

@end

@implementation ViewControllerApprovals

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  self.view.backgroundColor =
      [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"sos.png"]];

  if (self) {
  }
  return self;
}

- (void)viewDidAppear:(BOOL)animated {
}

- (void)viewDidLoad {

  [self setEdgesForExtendedLayout:UIRectEdgeNone];
  self.webView.delegate = self;

  NSURL *targetURL = [NSURL fileURLWithPath:self.pathToFile];
  NSURLRequest *tmpRequest = [NSURLRequest requestWithURL:targetURL];
 [self.webView loadRequest:tmpRequest];

}

- (BOOL)webView:(UIWebView *)webView
    shouldStartLoadWithRequest:(NSURLRequest *)request
                navigationType:(UIWebViewNavigationType)navigationType {

  self.currentRequest = request;
  [self hideNavigationBar:request];
  return YES;
}

#pragma mark - Hide Navigation Bar
- (void)hideNavigationBar:(NSURLRequest *)pRequest {

  NSString *currentURL = self.webView.request.URL.absoluteString;

  NSURL *url = pRequest.URL;
  NSString *urlString = [url absoluteString];
  NSString *nsfURL = @"#approvalPage";

  if ([urlString rangeOfString:nsfURL].location == NSNotFound) {
    [self.navigationController setNavigationBarHidden:FALSE animated:YES];
  } else {
    [self.navigationController setNavigationBarHidden:TRUE animated:YES];
  }
}

@end

0 个答案:

没有答案