当前位置:   article > 正文

iOS开发:给UIWebview的导航栏添加返回、关闭按钮_ios开发:给uiwebview导航栏添加返回与关闭按钮

ios开发:给uiwebview导航栏添加返回与关闭按钮

在我们平日的开发中,不免有原生与H5的交互,比如说:从原生页面的一个按钮,点击之后跳转到了一个H5的页面A,A页面中又有一个按钮,点击之后,又加载了一个新的H5页面B,从B点击一个按钮,又加载一个新的H5页面C,如果此时我们点击左上角的返回按钮,会直接返回到我们的原生页面;

是不是上面给用户的体验很不好(当然残品经理会觉得是,我们都是无所谓的啦),此时我们想要重新定制返回按钮,我们想要从C页面判断是否还有上一级H5页面可供返回,如果有上一级页面还是H5,点击左上角的返回则返回到B页面,并且在B页面的左上角加上一个关闭按钮,这个关闭按钮的作用主要是为了关闭所有的H5的页面,直接返回到我们原生的页面;如果我们不点击关闭按钮,还是点击返回,则从B页面返回到A页面;再次点击返回,则关闭了H5的页面,回到了原生的页面;

说的也许有点儿绕,不过大致思想就是:先判断当前的H5页面是否可以返回:

  1. //判断当前H5是否可以返回
  2. [self.webView canGoBack]

如果可以返回,则返回到上一个H5页面,并在左上角添加一个关闭按钮,如果不可以返回,则直接:

  1. //回到原生页面
  2. [self.navigationController popViewControllerAnimated:YES];

下面是我的主要实现代码,我写一个继承与UIViewController的类,接受了UIWebviewDelegate,并定义了一个UIwebview的属性,给外面留了一个方法,只需要传递一个URL,我们就可以加载;如果有地方需要加载H5的页面,我们可以直接集成与这个类,这样的好处在于方便维护;

当然我封装的这个类,同时也是支持HTTPS的请求的;话不多说,代码如下:

创建一个类,继承与UIViewController,.h中的代码

  1. #import <UIKit/UIKit.h>
  2. @interface SYWebViewController : UIViewController<UIWebViewDelegate, NSURLConnectionDelegate>
  3. //定义一个属性,方便外接调用
  4. @property (nonatomic, strong) UIWebView *webView;
  5. //声明一个方法,外接调用时,只需要传递一个URL即可
  6. - (void)loadHTML:(NSString *)htmlString;
  7. @end

.m中的实现如下:

  1. #import "SYWebViewController.h"
  2. @interface NSURLRequest (InvalidSSLCertificate)
  3. + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
  4. + (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
  5. @end
  6. @interface SYWebViewController ()
  7. @property (nonatomic, strong) NSURLRequest *request;
  8. //判断是否是HTTPS的
  9. @property (nonatomic, assign) BOOL isAuthed;
  10. //返回按钮
  11. @property (nonatomic, strong) UIBarButtonItem *backItem;
  12. //关闭按钮
  13. @property (nonatomic, strong) UIBarButtonItem *closeItem;
  14. @end
  15. @implementation SYWebViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
  20. [self.view addSubview:self.webView];
  21. [self addLeftButton];
  22. }
  23. //加载URL
  24. - (void)loadHTML:(NSString *)htmlString
  25. {
  26. NSURL *url = [NSURL URLWithString:htmlString];
  27. self.request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
  28. [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
  29. [self.webView loadRequest:self.request];
  30. }
  31. #pragma mark - UIWebViewDelegate
  32. //开始加载
  33. - (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  34. {
  35. NSString* scheme = [[request URL] scheme];
  36. //判断是不是https
  37. if ([scheme isEqualToString:@"https"]) {
  38. //如果是https:的话,那么就用NSURLConnection来重发请求。从而在请求的过程当中吧要请求的URL做信任处理。
  39. if (!self.isAuthed) {
  40. NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  41. [conn start];
  42. [awebView stopLoading];
  43. return NO;
  44. }
  45. }
  46. return YES;
  47. }
  48. //设置webview的title为导航栏的title
  49. - (void)webViewDidFinishLoad:(UIWebView *)webView
  50. {
  51. self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
  52. }
  53. #pragma mark ================= NSURLConnectionDataDelegate <NSURLConnectionDelegate>
  54. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
  55. {
  56. return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
  57. }
  58. - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  59. {
  60. if ([challenge previousFailureCount] == 0) {
  61. self.isAuthed = YES;
  62. //NSURLCredential 这个类是表示身份验证凭据不可变对象。凭证的实际类型声明的类的构造函数来确定。
  63. NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
  64. [challenge.sender useCredential:cre forAuthenticationChallenge:challenge];
  65. }
  66. }
  67. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  68. {
  69. NSLog(@"网络不给力");
  70. }
  71. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  72. {
  73. self.isAuthed = YES;
  74. //webview 重新加载请求。
  75. [self.webView loadRequest:self.request];
  76. [connection cancel];
  77. }
  78. #pragma mark - 添加关闭按钮
  79. - (void)addLeftButton
  80. {
  81. self.navigationItem.leftBarButtonItem = self.backItem;
  82. }
  83. //点击返回的方法
  84. - (void)backNative
  85. {
  86. //判断是否有上一层H5页面
  87. if ([self.webView canGoBack]) {
  88. //如果有则返回
  89. [self.webView goBack];
  90. //同时设置返回按钮和关闭按钮为导航栏左边的按钮
  91. self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];
  92. } else {
  93. [self closeNative];
  94. }
  95. }
  96. //关闭H5页面,直接回到原生页面
  97. - (void)closeNative
  98. {
  99. [self.navigationController popViewControllerAnimated:YES];
  100. }
  101. #pragma mark - init
  102. - (UIBarButtonItem *)backItem
  103. {
  104. if (!_backItem) {
  105. _backItem = [[UIBarButtonItem alloc] init];
  106. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  107. //这是一张“<”的图片,可以让美工给切一张
  108. UIImage *image = [UIImage imageNamed:@"sy_back"];
  109. [btn setImage:image forState:UIControlStateNormal];
  110. [btn setTitle:@"返回" forState:UIControlStateNormal];
  111. [btn addTarget:self action:@selector(backNative) forControlEvents:UIControlEventTouchUpInside];
  112. [btn.titleLabel setFont:[UIFont systemFontOfSize:17]];
  113. [btn setTitleColor:[UIColor sy_backColor] forState:UIControlStateNormal];
  114. //字体的多少为btn的大小
  115. [btn sizeToFit];
  116. //左对齐
  117. btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  118. //让返回按钮内容继续向左边偏移15,如果不设置的话,就会发现返回按钮离屏幕的左边的距离有点儿大,不美观
  119. btn.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
  120. btn.frame = CGRectMake(0, 0, 40, 40);
  121. _backItem.customView = btn;
  122. }
  123. return _backItem;
  124. }
  125. - (UIBarButtonItem *)closeItem
  126. {
  127. if (!_closeItem) {
  128. _closeItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeNative)];
  129. }
  130. return _closeItem;
  131. }
  132. @end

具体的使用方法就是,创建一个类,继承与这类:SYWebViewController

  1. #import <UIKit/UIKit.h>
  2. #import "SYWebViewController.h"
  3. @interface SYFlashHTMLViewController : SYWebViewController
  4. @end

然后在这个类的.m中的viewDidLoad中,调用父视图加载URL的方法,即:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.webView.delegate = self;
  4. [self loadHTML:self.htmlString];
  5. }

效果如下图:打开百度(图片1),点击图片进入(图片2),点击返回返回到(图片3),图片1和图片的区别在于多了个关闭按钮:


图片1.png

图片2.png

图片3.png

GitHub地址:https://github.com/FirstDKS521/DKSWebView
纯手打,喜欢点个赞,希望对看到的你有所帮助!!!



作者:First灬DKS
链接:http://www.jianshu.com/p/fa070e790647
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/93624
推荐阅读
相关标签
  

闽ICP备14008679号