Medium上有一篇文章說用_variable取代self.variable 就不會循環引用
實驗之後發現:
_variable也會
還是宣告成weak最好
既使weakSelf去呼叫一個方法,方法裡面有不是weak的self,那還是不會循環引用的。
用變數型態直接宣告在.h或.m,就算加了__weak還是一樣會循環引用,得要改成property的型態並宣告成weak才能避免
如果你在block中使用一些singleton例如[UIApplication sharedApplication],只要不要用變數去接它,就可以安全下莊
2018年5月29日 星期二
2018年5月25日 星期五
WKWebView
// Inject新的attribute給你的wkwebview
- (void)setupWebView {
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('initial-scale', '1'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:userScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = userContentController;
// 因為pdf的高度用webview的寬度去調整,所以要先設定好。
CGRect initialWebViewFrame = self.view.frame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat height = initialWebViewFrame.size.height;
CGFloat width = initialWebViewFrame.size.width;
if (height > width) {
initialWebViewFrame.size = CGSizeMake(width, 0);
} else {
initialWebViewFrame.size = CGSizeMake(height, 0);
}
}
_webView = [[WKWebView alloc] initWithFrame:initialWebViewFrame configuration:wkWebConfig];
_webView.scrollView.contentSize = initialWebViewFrame.size;
[_webViewContainer addSubview:_webView];
_webView.navigationDelegate = self;
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
_webView.scrollView.scrollEnabled = NO;
[_webView.scrollView setBackgroundColor:[UIColor clearColor]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
} else {
if (shouldReloadFlag) {
shouldReloadFlag = NO;
[self loadWebView];
}
}
[self resizeScrollView];
}
- (void)resizeScrollView {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
if (_webView.isLoading == false) {
for (UIView *subview in self.webView.scrollView.subviews) {
subview.backgroundColor = [UIColor clearColor];
// 取得pdf的高
if ([[[subview class] description] isEqualToString:@"WKPDFView"]) {
NSLog(@"pdfview.size: %@ webviewContainer.size:%@", NSStringFromCGSize(subview.frame.size), NSStringFromCGSize(_webViewContainer.frame.size));
[self resizeScrollViewByWebViewContentHeight:subview.frame.size.height / subview.frame.size.width * _webViewContainer.frame.size.width];
break;
}
}
}
} else {
if (_webView.isLoading == false) {
__weak typeof(self) weakSelf = self;
[_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id result, NSError * _Nullable error) {
CGFloat height = [result floatValue] + 30.0;
[weakSelf resizeScrollViewByWebViewContentHeight:height];
}];
}
}
}
- (void)setupWebView {
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('initial-scale', '1'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:userScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = userContentController;
// 因為pdf的高度用webview的寬度去調整,所以要先設定好。
CGRect initialWebViewFrame = self.view.frame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat height = initialWebViewFrame.size.height;
CGFloat width = initialWebViewFrame.size.width;
if (height > width) {
initialWebViewFrame.size = CGSizeMake(width, 0);
} else {
initialWebViewFrame.size = CGSizeMake(height, 0);
}
}
_webView = [[WKWebView alloc] initWithFrame:initialWebViewFrame configuration:wkWebConfig];
_webView.scrollView.contentSize = initialWebViewFrame.size;
[_webViewContainer addSubview:_webView];
_webView.navigationDelegate = self;
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
_webView.scrollView.scrollEnabled = NO;
[_webView.scrollView setBackgroundColor:[UIColor clearColor]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
} else {
if (shouldReloadFlag) {
shouldReloadFlag = NO;
[self loadWebView];
}
}
[self resizeScrollView];
}
- (void)resizeScrollView {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
if (_webView.isLoading == false) {
for (UIView *subview in self.webView.scrollView.subviews) {
subview.backgroundColor = [UIColor clearColor];
// 取得pdf的高
if ([[[subview class] description] isEqualToString:@"WKPDFView"]) {
NSLog(@"pdfview.size: %@ webviewContainer.size:%@", NSStringFromCGSize(subview.frame.size), NSStringFromCGSize(_webViewContainer.frame.size));
[self resizeScrollViewByWebViewContentHeight:subview.frame.size.height / subview.frame.size.width * _webViewContainer.frame.size.width];
break;
}
}
}
} else {
if (_webView.isLoading == false) {
__weak typeof(self) weakSelf = self;
[_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id result, NSError * _Nullable error) {
CGFloat height = [result floatValue] + 30.0;
[weakSelf resizeScrollViewByWebViewContentHeight:height];
}];
}
}
}
2018年5月19日 星期六
keyboard disappaer when tableView scrolling
2018年4月25日 星期三
獲取UIWebView的內容高度
// 直接抓
CGSize newSize = [webView.scrollView contentSize];
webViewHeightConstraint.constant = newSize.height;
// 如果是單純本地端的html,這樣比較準
CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
textViewHeightConstraint.constant = webViewHeight;
CGSize newSize = [webView.scrollView contentSize];
webViewHeightConstraint.constant = newSize.height;
// 如果是單純本地端的html,這樣比較準
CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
textViewHeightConstraint.constant = webViewHeight;
2018年4月18日 星期三
設定用UIModalPresentationFormSheet推出之後的視窗尺寸
categoriesViewController.preferredContentSize = CGSizeMake(440.0, 320.0);
categoriesViewController.modalPresentationStyle = UIModalPresentationFormSheet;
categoriesViewController.modalPresentationStyle = UIModalPresentationFormSheet;
2018年4月13日 星期五
get mode name
#import <sys/utsname.h>
struct utsname systemInfo;
uname(&systemInfo);
NSString *currentDeviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
struct utsname systemInfo;
uname(&systemInfo);
NSString *currentDeviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
2018年3月27日 星期二
存取層級(swift3.0以上適用)
- open:可以在module外面被使用,也可以被override。
- public:可以在module外面被使用,不可以被override。
- internal:不可以在module外面被使用。
- fileprivate:只可以在同一個file中所定義的class間使用。
- private:只可以在宣告它的class中使用,不被其他class存取。
訂閱:
文章 (Atom)