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 = NSTextAlignmentCenter; 67 [_vContainer addSubview:_lblErrorDetail]; 68 69 for (UILabel *lblToStyle in @[ _lblUrl, _lblErrorDetail ]) { 70 lblToStyle.font = [UIFont systemFontOfSize:14.0f]; 71 } 72 } 73 return self; 74} 75 76- (void)setType:(EXFatalErrorType)type 77{ 78 _type = type; 79 NSString *appOwnerName = @"the requested app"; 80 if (_appRecord) { 81 if (_appRecord == [EXKernel sharedInstance].appRegistry.homeAppRecord) { 82 appOwnerName = @"Expo"; 83 } else if (_appRecord.appLoader.manifest && _appRecord.appLoader.manifest.name) { 84 appOwnerName = [NSString stringWithFormat:@"\"%@\"", _appRecord.appLoader.manifest.name]; 85 } 86 } 87 88 switch (type) { 89 case kEXFatalErrorTypeLoading: { 90 _lblError.text = [NSString stringWithFormat:@"There was a problem loading %@.", appOwnerName]; 91 if (_error.code == kCFURLErrorNotConnectedToInternet) { 92 _lblError.text = [NSString stringWithFormat:@"%@ Make sure you're connected to the internet.", _lblError.text]; 93 } else if (_appRecord.appLoader.manifestUrl) { 94 NSString *url = _appRecord.appLoader.manifestUrl.absoluteString; 95 if ([self _urlLooksLikeLAN:url]) { 96 NSString *extraLANPermissionText = @""; 97 if (@available(iOS 14, *)) { 98 extraLANPermissionText = @", and that you have granted Expo Go the Local Network permission in the Settings app,"; 99 } 100 _lblError.text = [NSString stringWithFormat: 101 @"%@\n\nIt looks like you may be using a LAN URL. " 102 "Make sure your device is on the same network as the server%@ or try using the tunnel connection type.", _lblError.text, extraLANPermissionText]; 103 } 104 } 105 break; 106 } 107 case kEXFatalErrorTypeException: { 108 _lblError.text = [NSString stringWithFormat:@"There was a problem running %@.", appOwnerName]; 109 break; 110 } 111 } 112 [self _resetUIState]; 113} 114 115- (void)setError:(NSError *)error 116{ 117 _error = error; 118 _lblErrorDetail.text = [error localizedDescription]; 119 [self _resetUIState]; 120} 121 122- (void)setAppRecord:(EXKernelAppRecord *)appRecord 123{ 124 _appRecord = appRecord; 125 [self _resetUIState]; 126} 127 128- (void)layoutSubviews 129{ 130 [super layoutSubviews]; 131 132 _vContainer.frame = self.bounds; 133 CGFloat maxLabelWidth = self.bounds.size.width - 32.0f; 134 135 _lblError.frame = CGRectMake(0, 0, maxLabelWidth, CGFLOAT_MAX); 136 [_lblError sizeToFit]; 137 _lblError.center = CGPointMake(self.bounds.size.width * 0.5f, self.bounds.size.height * 0.25f); 138 139 _btnRetry.frame = CGRectMake(0, 0, self.bounds.size.width, 24.0f); 140 _btnRetry.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_lblError.frame) + 32.0f); 141 142 _btnBack.frame = CGRectMake(0, 0, self.bounds.size.width, 24.0f); 143 _btnBack.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_btnRetry.frame) + 24); 144 145 _lblUrl.frame = CGRectMake(0, 0, self.bounds.size.width - 48.0f, CGFLOAT_MAX); 146 [_lblUrl sizeToFit]; 147 _lblUrl.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_btnBack.frame) + 12.0f + CGRectGetMidY(_lblUrl.bounds)); 148 149 _lblErrorDetail.frame = CGRectMake(0, 0, maxLabelWidth, CGFLOAT_MAX); 150 [_lblErrorDetail sizeToFit]; 151 152 _lblErrorDetail.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_lblUrl.frame) + 24.0f + CGRectGetMidY(_lblErrorDetail.bounds)); 153 154 _vContainer.contentSize = CGSizeMake(_vContainer.bounds.size.width, CGRectGetMaxY(_lblErrorDetail.frame) + 12.0f); 155} 156 157#pragma mark - Internal 158 159- (void)_resetUIState 160{ 161 EXKernelAppRecord *homeRecord = [EXKernel sharedInstance].appRegistry.homeAppRecord; 162 _btnBack.hidden = (!homeRecord || _appRecord == homeRecord); 163 _lblUrl.hidden = (!homeRecord && ![self _isDevDetached]); 164 _lblUrl.text = _appRecord.appLoader.manifestUrl.absoluteString; 165 // TODO: maybe hide retry (see BrowserErrorView) 166 [self setNeedsLayout]; 167} 168 169- (void)_onTapRetry 170{ 171 if (_delegate) { 172 [_delegate errorViewDidSelectRetry:self]; 173 } 174} 175 176- (void)_onTapBack 177{ 178 if ([EXKernel sharedInstance].browserController) { 179 [[EXKernel sharedInstance].browserController moveHomeToVisible]; 180 } 181} 182 183- (BOOL)_urlLooksLikeLAN:(NSString *)url 184{ 185 return ( 186 url && ( 187 [url rangeOfString:@".local"].length > 0 || 188 [url rangeOfString:@"192."].length > 0 || 189 [url rangeOfString:@"10."].length > 0 || 190 [url rangeOfString:@"172."].length > 0 191 ) 192 ); 193} 194 195- (BOOL)_isDevDetached 196{ 197 return [EXEnvironment sharedEnvironment].isDetached && [EXEnvironment sharedEnvironment].isDebugXCodeScheme; 198} 199 200@end 201