2013年6月25日火曜日

2013年6月19日水曜日

[iOS]NSURLConnectionの証明書切れを回避する方法

// 証明書切れ回避
- (BOOL) connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
             forAuthenticationChallenge:challenge];
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

2013年6月12日水曜日

[iOS]UserDefaultsに保存する

FileOpenなどをしなくて良いので便利

// hogeDictを保存
[[NSUserDefaults standardUserDefaults] setObject:hogeDict forKey:@"hoge"];

// hogeDictを読み出し
NSDictionary *hogeDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"hoge"];

2013年6月6日木曜日

[iOS] AVAudioPlayerでサウンドが再生されない

NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp3"];
NSString *expandedPath = [path stringByExpandingTildeInPath];
NSURL *url = [NSURL fileURLWithPath:expandedPath];
NSError *error = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (audioPlayer) {
   [audioPlayer prepareToPlay];
   [audioPlayer play];
} else 
   NSLog(@"error %@", error);
}

簡単なコードなのに音声が再生されず、シンボルエラーで落ちるという現象が発生した。
落ちている場所はinitWithContentsOfURLのところ。

pathを変えてみるなどしたが、全く受け付けなかった。

・原因
使用した"sample.mp3"にあったようで、"sample.wav"などにエンコードすると問題なく再生された。
私がエンコードのヘッダーなどは分からないので詳細は不明だが、
エンコーダーやファイルによってはiOSで再生できないものがあるらしい。

そういえばAndroidSDKにもMediaPlayer似た問題があったな。
サウンドがが途切れたり、再生が遅かったり、再生されなかったり、、、

サウンド関係は面倒臭がらずにOpenALを使用したほうが結果的に良いかも

2013/06/19 追記
OpenALでも再生できなかった。
ビルドクリーンをして、本体orシミュレーターからアプリを削除して、
project.xcworkspaceとxcuserdataを削除した後に、
ビルドせずにMACを再起動して、ビルドをしたら問題無く再生された。

2013年6月4日火曜日

[iOS]文字列をLocalizable.stringから取得する

タグ = 表示したい文字列のフォーマット
Localizable.string
"sample" = "サンプル";

コード上で取得する
タグ,コメントでコメントは省略可
NSLocalizedString(@"network_domain", nil)