- (void)redirectLogToDocuments {
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"logFile.txt"];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathForLog]) {
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:pathForLog error:&error];
if (error!=nil) {
NSLog(@"%@", error.localizedDescription);
}
}
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
2018年6月12日 星期二
讓圖示在AttributedText上下置中
https://stackoverflow.com/questions/26105803/center-nstextattachment-image-next-to-single-line-uilabel
let iconImage = UIImage(named: "icon.png")!
var icon = NSTextAttachment()
icon.bounds = CGRect(x: 0, y: (titleFont.capHeight - iconImage.size.height).rounded() / 2, width: iconImage.size.width, height: iconImage.size.height)
icon.image = iconImage
let iconString = NSAttributedString(attachment: icon)
titleText.append(iconString)
2018年6月8日 星期五
rate the app
- (IBAction)rateButtonTapped:(UIButton *)sender {
if (@available(iOS 10.3, *)) {
[SKStoreReviewController requestReview];
} else {
NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/tw/app/swipedon-visitor-management/id986185962?l=en&mt=8"];
[[UIApplication sharedApplication] openURL:url];
}
}
if (@available(iOS 10.3, *)) {
[SKStoreReviewController requestReview];
} else {
NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/tw/app/swipedon-visitor-management/id986185962?l=en&mt=8"];
[[UIApplication sharedApplication] openURL:url];
}
}
2018年6月5日 星期二
用CoreData存圖片
不要用core data存圖
是可以轉存NSData去存,但只要你的ManagedObjectContext沒有釋放,那麼那張圖就不會被釋放,如果圖片大一點,一下就爆了
用core data存imageName,把圖片存到文件資料夾裡,用imageName去對應,
並且監聽imageName的setter,在改名之前先刪去原來的圖。
是可以轉存NSData去存,但只要你的ManagedObjectContext沒有釋放,那麼那張圖就不會被釋放,如果圖片大一點,一下就爆了
用core data存imageName,把圖片存到文件資料夾裡,用imageName去對應,
並且監聽imageName的setter,在改名之前先刪去原來的圖。
2018年5月29日 星期二
block中的self
Medium上有一篇文章說用_variable取代self.variable 就不會循環引用
實驗之後發現:
_variable也會
還是宣告成weak最好
既使weakSelf去呼叫一個方法,方法裡面有不是weak的self,那還是不會循環引用的。
用變數型態直接宣告在.h或.m,就算加了__weak還是一樣會循環引用,得要改成property的型態並宣告成weak才能避免
如果你在block中使用一些singleton例如[UIApplication sharedApplication],只要不要用變數去接它,就可以安全下莊
實驗之後發現:
_variable也會
還是宣告成weak最好
既使weakSelf去呼叫一個方法,方法裡面有不是weak的self,那還是不會循環引用的。
用變數型態直接宣告在.h或.m,就算加了__weak還是一樣會循環引用,得要改成property的型態並宣告成weak才能避免
如果你在block中使用一些singleton例如[UIApplication sharedApplication],只要不要用變數去接它,就可以安全下莊
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
訂閱:
文章 (Atom)