WentingLicenseWebView.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // WentingLicenseWebView.m
  3. // Unity-iPhone
  4. //
  5. // Created by 吴博 on 2021/7/23.
  6. //
  7. #import "WentingLicenseWebView.h"
  8. #import <WebKit/WKWebView.h>
  9. @interface WentingLicenseWebView()
  10. @property (nonatomic, strong)WKWebView *webView;
  11. @property (nonatomic, copy)NSString *url;
  12. @end
  13. @implementation WentingLicenseWebView
  14. - (id)initWithUrl:(NSString *)url {
  15. _url = url;
  16. return [super init];
  17. }
  18. - (void)loadUrl:(NSString *)url {
  19. _url = url;
  20. if (!_webView) return;
  21. [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
  22. }
  23. - (void)dealloc {
  24. if (_webView) {
  25. [_webView removeFromSuperview];
  26. _webView = nil;
  27. }
  28. }
  29. - (BOOL)prefersStatusBarHidden {
  30. return YES;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. if (@available(iOS 13.0, *)) {
  35. self.modalInPresentation = true;
  36. }
  37. CGRect frame = self.view.frame;
  38. self.view.backgroundColor = [UIColor whiteColor];
  39. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
  40. if (@available(iOS 11.0, tvOS 11.0, *)) {
  41. insets = [self.view safeAreaInsets];
  42. }
  43. frame.origin.x += insets.left;
  44. frame.origin.y += insets.bottom; // Unity uses bottom left as the origin
  45. frame.size.width -= insets.left + insets.right;
  46. frame.size.height -= insets.top + insets.bottom;
  47. _webView = [[WKWebView alloc] init];
  48. _webView.frame = frame;
  49. [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
  50. [self.view addSubview:_webView];
  51. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  52. button.backgroundColor = [UIColor greenColor];
  53. button.frame = CGRectMake((frame.origin.x + frame.size.width - 70), (frame.origin.y + frame.size.height - 40), 50, 20);
  54. button.titleLabel.font = [UIFont systemFontOfSize:14];
  55. [button setTitle:@"返回" forState:UIControlStateNormal];
  56. [button setTitleColor:[UIColor colorWithRed:104 green:221 blue:228 alpha:255] forState:UIControlStateNormal];
  57. [button addTarget:self action:@selector(backBtnSel:) forControlEvents:UIControlEventTouchUpInside];
  58. [self.view addSubview:button];
  59. }
  60. - (void)backBtnSel:(UIButton *)button {
  61. [self dismissViewControllerAnimated:NO completion:nil];
  62. }
  63. @end