Files
PojavLauncher_iOS/Natives/LauncherNewsViewController.m
T
Eilionoir Tunnicliff e645cfcde8 Changes
- Refine WKWebView on notched devices (worked fine on my 7 as well...)
- Add iDevice model, iOS version, and install to General footer
2022-12-06 00:12:26 -05:00

76 lines
3.3 KiB
Objective-C

#import <SafariServices/SafariServices.h>
#import <WebKit/WebKit.h>
#import "LauncherNewsViewController.h"
@interface LauncherNewsViewController()<WKNavigationDelegate>
@end
@implementation LauncherNewsViewController
WKWebView *webView;
UIEdgeInsets insets;
- (NSString *)imageName {
return @"MenuNews";
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
insets = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://pojavlauncherteam.github.io/changelogs/IOS.html"]];
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webConfig];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
webView.translatesAutoresizingMaskIntoConstraints = NO;
webView.navigationDelegate = self;
webView.opaque = NO;
[self adjustWebViewForSize:size];
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
webView.scrollView.delegate = self;
NSString *javascript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *nozoom = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[webView.configuration.userContentController addUserScript:nozoom];
[webView.scrollView setShowsHorizontalScrollIndicator:NO];
[webView loadRequest:request];
[self.view addSubview:webView];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x > 0)
scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self adjustWebViewForSize:size];
}
- (void)adjustWebViewForSize:(CGSize)size {
BOOL isPortrait = size.height > size.width;
if (isPortrait) {
webView.scrollView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height + insets.top, 0, self.navigationController.navigationBar.frame.size.height + insets.bottom, 0);
} else {
webView.scrollView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height, 0, self.navigationController.navigationBar.frame.size.height, 0);
}
}
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
SFSafariViewController *vc = [[SFSafariViewController alloc] initWithURL:navigationAction.request.URL];
[self presentViewController:vc animated:YES completion:nil];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
@end