xref: /expo/ios/Exponent/Kernel/Views/EXErrorView.m (revision e7a5287c)
1// Copyright 2015-present 650 Industries. All rights reserved.
2
3#import "EXAppLoader.h"
4#import "EXErrorView.h"
5#import "EXEnvironment.h"
6#import "EXKernel.h"
7#import "EXKernelAppRecord.h"
8#import "EXUtil.h"
9
10@interface EXErrorView ()
11
12@property (nonatomic, strong) UILabel *lblError;
13@property (nonatomic, strong) UIButton *btnRetry;
14@property (nonatomic, strong) UIButton *btnBack;
15@property (nonatomic, strong) UILabel *lblUrl;
16@property (nonatomic, strong) UILabel *lblErrorDetail;
17@property (nonatomic, strong) UIScrollView *vContainer;
18
19- (void)_onTapRetry;
20
21@end
22
23@implementation EXErrorView
24
25- (instancetype)initWithFrame:(CGRect)frame
26{
27  if (self = [super initWithFrame:frame]) {
28    self.backgroundColor = [UIColor whiteColor];
29
30    self.vContainer = [[UIScrollView alloc] init];
31    [self addSubview:_vContainer];
32
33    // error description label
34    self.lblError = [[UILabel alloc] init];
35    _lblError.numberOfLines = 0;
36    _lblError.textAlignment = NSTextAlignmentCenter;
37    _lblError.font = [UIFont systemFontOfSize:14.0f];
38    [_vContainer addSubview:_lblError];
39
40    // retry button
41    self.btnRetry = [UIButton buttonWithType:UIButtonTypeRoundedRect];
42    [_btnRetry setTitle:@"Try again" forState:UIControlStateNormal];
43    [_btnRetry addTarget:self action:@selector(_onTapRetry) forControlEvents:UIControlEventTouchUpInside];
44    [_vContainer addSubview:_btnRetry];
45
46    // back button
47    self.btnBack = [UIButton buttonWithType:UIButtonTypeRoundedRect];
48    [_btnBack setTitle:@"Go back to Expo Home" forState:UIControlStateNormal];
49    [_btnBack addTarget:self action:@selector(_onTapBack) forControlEvents:UIControlEventTouchUpInside];
50    [_vContainer addSubview:_btnBack];
51
52    for (UIButton *btnToStyle in @[ _btnRetry, _btnBack ]) {
53      [btnToStyle setTintColor:[EXUtil colorWithRGB:0x49a7e8]];
54      [btnToStyle.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
55    }
56
57    // url label
58    self.lblUrl = [[UILabel alloc] init];
59    _lblUrl.numberOfLines = 0;
60    _lblUrl.textAlignment = NSTextAlignmentCenter;
61    [_vContainer addSubview:_lblUrl];
62
63    // error detail label
64    self.lblErrorDetail = [[UILabel alloc] init];
65    _lblErrorDetail.numberOfLines = 0;
66    _lblErrorDetail.textAlignment = NSTextAlignmentLeft;
67    [_vContainer addSubview:_lblErrorDetail];
68
69    for (UILabel *lblToStyle in @[ _lblUrl, _lblErrorDetail ]) {
70      lblToStyle.font = [UIFont systemFontOfSize:14.0f];
71      lblToStyle.textColor = [UIColor lightGrayColor];
72    }
73  }
74  return self;
75}
76
77- (void)setType:(EXFatalErrorType)type
78{
79  _type = type;
80  NSString *appOwnerName = @"the requested app";
81  if (_appRecord) {
82    if (_appRecord == [EXKernel sharedInstance].appRegistry.homeAppRecord) {
83      appOwnerName = @"Expo";
84    } else if (_appRecord.appLoader.manifest && _appRecord.appLoader.manifest.name) {
85      appOwnerName = [NSString stringWithFormat:@"\"%@\"", _appRecord.appLoader.manifest.name];
86    }
87  }
88
89  switch (type) {
90    case kEXFatalErrorTypeLoading: {
91      _lblError.text = [NSString stringWithFormat:@"There was a problem loading %@.", appOwnerName];
92      if (_error.code == kCFURLErrorNotConnectedToInternet) {
93        _lblError.text = [NSString stringWithFormat:@"%@ Make sure you're connected to the internet.", _lblError.text];
94      } else if (_appRecord.appLoader.manifestUrl) {
95        NSString *url = _appRecord.appLoader.manifestUrl.absoluteString;
96        if ([self _urlLooksLikeLAN:url]) {
97          NSString *extraLANPermissionText = @"";
98          if (@available(iOS 14, *)) {
99            extraLANPermissionText = @", and that you have granted Expo Go the Local Network permission in the Settings app,";
100          }
101          _lblError.text = [NSString stringWithFormat:
102                            @"%@\n\nIt looks like you may be using a LAN URL. "
103                            "Make sure your device is on the same network as the server%@ or try using the tunnel connection type.", _lblError.text, extraLANPermissionText];
104        }
105      }
106      break;
107    }
108    case kEXFatalErrorTypeException: {
109      _lblError.text = [NSString stringWithFormat:@"There was a problem running %@.", appOwnerName];
110      break;
111    }
112  }
113  [self _resetUIState];
114}
115
116- (void)setError:(NSError *)error
117{
118  _error = error;
119  _lblErrorDetail.text = [error localizedDescription];
120  [self _resetUIState];
121}
122
123- (void)setAppRecord:(EXKernelAppRecord *)appRecord
124{
125  _appRecord = appRecord;
126  [self _resetUIState];
127}
128
129- (void)layoutSubviews
130{
131  [super layoutSubviews];
132
133  _vContainer.frame = self.bounds;
134  CGFloat maxLabelWidth = self.bounds.size.width - 32.0f;
135
136  _lblError.frame = CGRectMake(0, 0, maxLabelWidth, CGFLOAT_MAX);
137  [_lblError sizeToFit];
138  _lblError.center = CGPointMake(self.bounds.size.width * 0.5f, self.bounds.size.height * 0.25f);
139
140  _btnRetry.frame = CGRectMake(0, 0, self.bounds.size.width, 24.0f);
141  _btnRetry.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_lblError.frame) + 32.0f);
142
143  _btnBack.frame = CGRectMake(0, 0, self.bounds.size.width, 24.0f);
144  _btnBack.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_btnRetry.frame) + 24);
145
146  _lblUrl.frame = CGRectMake(0, 0, self.bounds.size.width - 48.0f, CGFLOAT_MAX);
147  [_lblUrl sizeToFit];
148  _lblUrl.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_btnBack.frame) + 12.0f + CGRectGetMidY(_lblUrl.bounds));
149
150  _lblErrorDetail.frame = CGRectMake(0, 0, maxLabelWidth, CGFLOAT_MAX);
151  [_lblErrorDetail sizeToFit];
152
153  _lblErrorDetail.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_lblUrl.frame) + 24.0f + CGRectGetMidY(_lblErrorDetail.bounds));
154
155  _vContainer.contentSize = CGSizeMake(_vContainer.bounds.size.width, CGRectGetMaxY(_lblErrorDetail.frame) + 12.0f);
156}
157
158#pragma mark - Internal
159
160- (void)_resetUIState
161{
162  EXKernelAppRecord *homeRecord = [EXKernel sharedInstance].appRegistry.homeAppRecord;
163  _btnBack.hidden = (!homeRecord || _appRecord == homeRecord);
164  _lblUrl.hidden = (!homeRecord && ![self _isDevDetached]);
165  _lblUrl.text = _appRecord.appLoader.manifestUrl.absoluteString;
166  // TODO: maybe hide retry (see BrowserErrorView)
167  [self setNeedsLayout];
168}
169
170- (void)_onTapRetry
171{
172  if (_delegate) {
173    [_delegate errorViewDidSelectRetry:self];
174  }
175}
176
177- (void)_onTapBack
178{
179  if ([EXKernel sharedInstance].browserController) {
180    [[EXKernel sharedInstance].browserController moveHomeToVisible];
181  }
182}
183
184- (BOOL)_urlLooksLikeLAN:(NSString *)url
185{
186  return (
187    url && (
188      [url rangeOfString:@".local"].length > 0 ||
189      [url rangeOfString:@"192."].length > 0 ||
190      [url rangeOfString:@"10."].length > 0 ||
191      [url rangeOfString:@"172."].length > 0
192    )
193  );
194}
195
196- (BOOL)_isDevDetached
197{
198  return [EXEnvironment sharedEnvironment].isDetached && [EXEnvironment sharedEnvironment].isDebugXCodeScheme;
199}
200
201@end
202