2016年11月17日 星期四

iOS tips - Xcode 8 compile very slow

更新到Xcode 8之後,把project全改成swift 3相當痛苦。但不止如此,在把app透過USB裝到手機上時發現compile速度超慢,一次可能需要個5分鐘以上...(這是以前在寫BIOS的時代嗎? build個code可以先去上廁所買個飲料這樣) 在stackoverflow上發現了有個解法,真的不知道這些神人是怎麼發現的,或許是apple的engineer自己上來回覆的吧,不然怎麼會知道加這參數就能改善了

SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
在build setting中加上以上的參數後,compile and install的速度就正常了,爽

2016年9月30日 星期五

iOS tips - Get IP Address

在swift中似乎沒有get ip address的function可以直接call,stackoverflow一下後看到了這一篇Get IP Address,其中重點為在swift專案中的objective-c header include一個標頭檔

2016年9月12日 星期一

iOS tips - CocoaAsyncSocket + Swift tutorial

UDP or TCP/IP socket傳送是許多開發者經常會用到的protocol,當想要用iPhone device跟其他在同網域下的device溝通時,這是我們一定會碰到的課題,網路上有許多好的tutorial以及example,google一下就能找到很多英文的教學,我是用了CocoaAsyncSocket這個library,只不過裡面的demo code是objective-c的,小弟照著寫了個UDP swift版本的,有興趣的可以參考,也歡迎提出指正。

2016年9月7日 星期三

iOS tips - 'No UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath' crash

工作寫的app中的collection view使用了custom collection view layout。最近為了一個分享的需求所以需要在開啟外部url link後捲到指定的位置,透過collectionview的function scrollToItemAtIndexPath使得collectionview跑到指定的位置。


2016年8月17日 星期三

iOS tips - Image Exif

因為工作的需要,必須處理image的exif資訊。在iOS上從來沒考慮過這檔事,所以花了些時間研究了一下。
首先了解到的是,原來UIImage不會帶著原始image file的額外資訊,所以假設從網路或者是手機內部資料夾得到的原始檔案轉成UIImage的話,那怎麼讀也不會有exif資訊的,因為這個類別把哪些資訊都去掉了,只留下生成影像的data而已。因此如果要讀取或修改exif資訊的話只能從原始的data下手

2016年6月8日 星期三

2016年5月17日 星期二

iOS tips - Remove subviews at once

當我們create了一個UIView後,常常有可能在上面加了許多的subview,而之後需要清除這些subview再做其他的處理,只需要一行code就可以做到清除所有subview的動作

2016年5月11日 星期三

iOS tips - Crop Image

因工作需求,需要將影片的thumbnail剪半再rotate,stackoverflow後總算完成,做個小筆記
重點就是使用CGImageCreateWithImageInRect使用將原本的Image重設rect為想要的size

2016年5月9日 星期一

iOS tips - PopoverView on iPhone

在iPad上的menu選單常常可以看到popover方式呈現的menu,但不知為何在iPhone上不能implement,不過iOS 8之後iPhone也可以透過別的方式實現了

2016年5月4日 星期三

iOS tips - Force ImagePicker landscape view

ImagePicker如果沒加上一段神秘的code,在target只設定Landscape的話,一打開ImagePicker,系統不囉唆馬上給一個crash,要解決這問題很簡單,在會出現picker的view上加了這段code就沒問題了


- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeRight;

}

2016年4月29日 星期五

iOS tips - add delay

寫app時,不管是為了加上一些效果,或者是解一些奇怪的bug,常會需要加上delay然後來處理我們之後想要做的code,下面這段code就是delay 2秒然後處理另外的task

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //code to be executed on the main queue after delay
    [self doSometingWithObject:obj1 andAnotherObject:obj2];
});

2016年4月28日 星期四

iOS tips - PCH file in latest Xcode

記得好像Xcode 6之後PCH file在建project時就不會自動產生了,小弟不才,不知道把這拿掉的原因是啥,能夠有個標頭檔import常用到的file不是很好嗎?!下面這篇說明了要怎麼把pch file加回來.
http://stackoverflow.com/questions/24305211/pch-file-in-xcode-6

2016年3月29日 星期二

iOS tips error and warning handle collect

1. Unexpected CFBundleExecutable key
http://stackoverflow.com/questions/32096130/unexpected-cfbundleexecutable-key

2. Could not find Developer Disk image
http://stackoverflow.com/questions/33885080/xcode-7-1-with-ios-9-2-error-could-not-find-developer-disk-image

3. Debug unrecognized selector
http://blog.jackhl.com/2012/05/03/how-to-debug-objective-c-unrecognized-selector/

4. Avoid warning of “xxx in block is likely to lead a retain cycle"
http://stackoverflow.com/questions/7853915/how-do-i-avoid-capturing-self-in-blocks-when-implementing-an-api

5. Could not insert new outlet connection
http://stackoverflow.com/questions/15288773/could-not-insert-new-outlet-connection

6. SearchDisplayController‘ is deprecated: first deprecated in iOS 8.0


7. NSGregorianCalendar is deprecated: first deprecated in iOS 8.0

2016年2月24日 星期三

iOS tips - weak, strong, copy, , retain, assign概念介紹

寫了obj-c也兩年多了,但對於obj-c的properity一直沒有弄得很清楚,網路上找到了一篇文章之後,對於一些觀念有了比較清楚的認知,以下主要是參考這篇文章的心得
http://rypress.com/tutorials/objective-c/properties

2016年2月18日 星期四

iOS tips - gesture cancelsTouchesInView 屬性

最近因為一些功能需要加上gesture來做一些額外的動作,然後加上gesture的同時遇到了一些問題,找到了一個文章解決了我心中大部分的疑問!

http://blog.csdn.net/kylinbl/article/details/9139473

簡單來說,cancelsTouchesInView屬性的設定為NO時,在parentview加上的手勢就能不衝突的跟原本subview中的元件tap事件同時被觸發

2016年2月4日 星期四

iOS tips - xib subview overlay navigation bar

最近遇到一個挺怪的問題,用xib產生的view controller在被某個view controller以push方式呈現之後,xib中的一些物件會重疊到navigation bar,像是下面這樣

2016年1月18日 星期一

iOS tips - dismiss keyboard

textfield是我們常常會使用到的元件,而有時在原本的VC不需要寫textfield的delegate時要收起鍵盤常常是一件麻煩的事情,加上下面這段code就可以在鍵盤外的畫面touch然後收下鍵盤

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
然而如果是在tableview加上這段code的話會發現除了textfield以外,原本的cell都不能點擊了,此時再將我們宣告的gestureRecognizer設定一個屬性
gestureRecognizer.cancelsTouchesInView = NO
如此一來tableview的cell也可以正常運作了!

2016年1月9日 星期六

iOS tips - Local Notification

我們常常在內部某一個VC完成一個A task後,需要讓另一個VC能夠根據A task的結果或內容去做另一個B task,此時就需要在A task內加上通知,B task的VC加上一個observer,如此一來只要A task完成後發出notification,B task的觀察者馬上能夠知道A task已完成,然後去執行他該做的工作,可參考以下代碼

// Add an observer that will respond to loginComplete [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bTask:) name:@"aTaskComplete" object:nil]; // Post a notification to loginComplete [[NSNotificationCenter defaultCenter] postNotificationName:@"aTaskComplete" object:nil]; // the function specified in the same class where we defined the addObserver - (void) bTask:(NSNotification *)note { NSLog(@"Received Notification - Do something here!"); }

2016年1月6日 星期三

iOS tips - UIRefreshControl

幾乎每個有tableview的app都會用到UIRefreshControl,也就是下拉更新(pull down to refresh),當所使用的view controller是基本的tableview的話,實現的方法相當簡單,在viewDidload中加上這些code來initial一個UIRefreshControl
- (void)viewDidLoad {
    [super viewDidLoad];
    refreshControl = [[UIRefreshControl alloc]init];
    [self.mytableView addSubview:refreshControl];
    [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
}
然後在相對應的selector上加上個人更新table的method,很簡單的就完成了一個下拉更新的功能
- (void)refreshTable {
    //TODO: refresh your data
    [refreshControl endRefreshing];
    [self.mytableView reloadData];
}

iOS tips - 動態設置label width

在設置一個UILabel時,常常會遇到設置的frame width不夠文字內容使用的問題,雖說也有adjustsFontSizeToFitWidth這個property可以設定,但是調整label字型大小通常不會是設計的選項之一.
此時我們可以先依靠"sizeWithFont"這個function來detect要帶入的文字的寬度,再去設置UILabel的frame
//use this for system font 
CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;

label.frame = CGRectMake(point.x, point.y, width,height);
如此就能夠根據文字內容來動態定義Label的width了