2010年7月30日 星期五

iPhone stringWithContentsOfFile is Deprecated

如果在使用stringWithContentsOfFile時候
出現Deprecated的警告滿煩人的
雖然編譯會過,但是程式run到那邊鐵定crash
通常會出現警告都是用法上的問題
1
[NSString stringWithContentsOfFile:filePath];

請替換成
1
2
3
[NSString stringWithContentsOfFile:filePath
                          encoding:NSUTF8StringEncoding
                             error:nil];

很簡單的小技巧
資料來源:keyvisuals

2011/04/21 Updates:
都已經提了stringWithContentOfFile沒有講講怎麼拿到file的path好像說不過去
方法如下:

1
2
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"foo"
                                                    ofType:@"html"];

2010年7月28日 星期三

Olivia Ong - All out of love

記得我好幾年前(2006)推薦了Olivia的basanova專輯
沒想到近來因為電視台的歌唱選秀節目而聲名大噪

這首翻唱的經典名歌
在她詮釋下別有一番味道
簡單的吉他和弦搭配清澈嗓音
帶有那麼一點點失意與惆悵感
主旋律沒有用太過高亢強烈的唱法
娓娓道來,百聽不厭...



I'm lying alone with my head on the phone
Thinking of you till it hurts
I know you hurt too but what else can we do
Tormented and torn apart

I wish I could carry your smile in my heart
For times when my life seems so low
It would make me believe what tomorrow can bring
When today doesn't really know, doesn't really know

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

I want you to come back and carry me home
Away from these long, lonely nights
I'm reaching for you, are you feeling it too?
Does the feeling seem oh, so right?

And what would you say if I called on you now?
And said that I can't hold on?
There's no easy way, it gets harder each day
Please love me or I'll be gone, I'll be gone

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

Ooh, what are you thinking of
What are you thinking of
What are you thinking of
What are you thinking of

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong, so wrong


2010年7月21日 星期三

iPhone 檢查飛航模式是否為開啟狀態

要在程式中檢查iPhone是不是有把 flight mode開啟
作法其實很簡單,連半行code都不用加

首先,找到專案的.plist
接著新增一個欄位 key值為SBUsesNetwork


然後在右邊的value空白處按右鍵
變更value type為boolean並且確定是勾選的狀態
                         為integer並且設定值為3

附註:在xocde開發過程中 simulator並沒有飛航模式可以選擇
因此,我還在研究要怎麼驗證這個功能 :p
參考連結:blog.shallarp.com

2010年7月20日 星期二

iPhone 使用NSMutableURLRequest 參數TimeoutInterval沒起作用

通常有用到http connection的人都會需要處理connection timeout問題

但是最近卻遇到了TimeoutInterval沒起作用的問題
這是一般的用法:

1
2
3
4
NSMutableURLRequest* request = [[NSMutableURLRequest alloc
          initWithURL: [NSURL URLWithString: serverurl]  
          cachePolicy:NSURLRequestReturnCacheDataElseLoad 
          timeoutInterval:240];

但是根據Apple官方的論壇提到 timeout預設值為240秒
只要低於這個值都會被自動忽略


所以想把timeout時間設短的人就需要自行做一個timer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
SEL sel = @selector(customCancel);
NSMethodSignature* sig = [self methodSignatureForSelector:sel];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
 
[invocation setTarget:self];
[invocation setSelector:sel];
 
NSTimer *timer = [NSTimer timerWithTimeInterval:CUSTOM_TIMEOUT
        invocation:invocation
        repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer
     forMode:NSDefaultRunLoopMode];

把上面這段程式跟NSMutableURLRequest放在一起
接著實作 customCancel 這個部分

1
2
3
4
5
6
- (void)customCancel{
 if (connection) {
  [connection release];
  connection = nil;
        }
}

這樣就可以了!
參考資料:stackoverflow

內容回應