float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) {
} else {
}
2015年1月14日水曜日
[iOS]iOSのバージョンを調べる
2014年7月29日火曜日
[iOS]ディスプレイと画像の比率を求める
- (float)getScale :(CGSize)displaySize :(CGSize)imageSize {
float scale = 0.0;
if (imageSize.height > displaySize.height) {
scale = displaySize.height / imageSize.height;
} else {
scale = imageSize.height / displaySize.height;
}
return scale;
}
[iOS]メールを送信
- (void)sendMail
if([MFMailComposeViewController canSendMail] == YES) { //メーラーが使用できるかどうか
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setSubject:@"subject"]; //題名
[controller setMessageBody:@"message" isHTML:NO]; //本文
controller.mailComposeDelegate = self; //成功、失敗時にdelegateを送る
[self presentViewController:controller animated:YES completion:nil]; //ViewContrllerとして立ち上げる
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil]; //終了時にViewContrllerを閉じる
return;
}
2014年6月30日月曜日
[iOS]UISliderとCGAffineTransform
UISliderでCGAffineTransformを使用するとつまみがタップできなくなるタイミングがある
再現率は100%ではない
ちょっと調べてみたが対策法はなさそうなのでUIScrollViewなどで代用するしかなさそうだ
2014年4月17日木曜日
2014年4月8日火曜日
[iOS]UIGestureRecognizerがtouchイベントを発生させない
UIGestureRecognizerを新しく作ったviewにaddしても有効にならないタイミングがある(?)
とりあえずNSTimerでviewが作られた後にaddすればうまくいった
とりあえずNSTimerでviewが作られた後にaddすればうまくいった
2014年3月9日日曜日
[iOS]UIColorからUIImageを作成する
stackoverflow.comより
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [self createImageFromUIColor:[UIColor blackColor]];
}
- (UIImage *)createImageFromUIColor:(UIColor *)color
{
//1x1のBitmapを作成する
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
//Bitmapを塗りつぶし
CGContextSetFillColorWithColor(contextRef, [color CGColor]);
CGContextFillRect(contextRef, rect);
//UIImageに変換
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
};
2014年2月26日水曜日
[iOS]Xcode5でiPadのスプラッシュが表示されない問題
Xcode5.0.2でスプラッシュ画像追加のためProject>Launch Imagesから
Defaults-Portrait、Defaults-Portrait@2x、Default-Portrait.png、Default-Portrait@2x.png
の画像を設定するもスプラッシュは表示されない
どうやらiOS6.1以下の各種画像とiOS7.0以上の画像が同じリソースを使うと表示されないようだ
対処法
Defaultの画像4つををコピーして適当な名前にする
Launch Images>iPad>各種(iOS 6.1 and Prior)にコピーした画像を設定する
設定時に、リネームするか、と聞いてくるので自動でリネームさせる
iPadのスプラッシュ画像が表示される、良かった
Defaults-Portrait、Defaults-Portrait@2x、Default-Portrait.png、Default-Portrait@2x.png
の画像を設定するもスプラッシュは表示されない
どうやらiOS6.1以下の各種画像とiOS7.0以上の画像が同じリソースを使うと表示されないようだ
対処法
Defaultの画像4つををコピーして適当な名前にする
Launch Images>iPad>各種(iOS 6.1 and Prior)にコピーした画像を設定する
設定時に、リネームするか、と聞いてくるので自動でリネームさせる
iPadのスプラッシュ画像が表示される、良かった
2014年1月27日月曜日
[iOS]UISliderで値の変化時にイベントを取得できない
UISliderの値の変化時に処理を行う時に
[slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged]
とすれば、良いはずだがUISliderでは– addTarget:action:forControlEvents:
が反応しないので
[slider sendActionsForControlEvents:UIControlEventValueChanged];
とするとイベントをキャッチできる
[slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged]
とすれば、良いはずだがUISliderでは– addTarget:action:forControlEvents:
が反応しないので
[slider sendActionsForControlEvents:UIControlEventValueChanged];
とするとイベントをキャッチできる
2013年12月27日金曜日
UIViewのアニメーション
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(scrollAnimationDidEnd)];
//ここに処理を追加
animeView.frame = CGRectMake(0.0, 10.0, 100, 100)
[UIView commitAnimations];
2013年12月10日火曜日
[iOS]UIImageViewに追加されたUIButtonが反応しない場合の処理
UIImageVIewはタッチイベントに対応してないので、
imageView.userInteractionEnabled = YES;
とするとタッチ可能になる
imageView.userInteractionEnabled = YES;
とするとタッチ可能になる
2013年7月3日水曜日
[iOS]NSLogで文字化けする問題
//recvDictをエンコードして表示する
NSString *dictString = [NSString stringWithFormat:@"%@",recvDict];
NSString *encodeString = [NSString stringWithCString:
[dictString cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(@"encodeString %@", encodeString);
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)
Localizable.string
"sample" = "サンプル";
コード上で取得する
タグ,コメントでコメントは省略可
NSLocalizedString(@"network_domain", nil)
2013年5月30日木曜日
[iOS]NSURLConnectionを使用してHTTP接続をする。
cookieの保存が必要だったのでメモを残しておく。
- (void)viewDidLoad
{
[super viewDidLoad];
//リクエストを作成
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:@"http://apple.ios.sample.com"]];
[request setTimeoutInterval:30.0];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
//Cookieを保持している場合は読み込む
//TODO Cookieの扱いがおかしい?
NSHTTPCookie *cookie = nil;
NSData *cookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"cookie"];
if (cookiesData.length != 0) {
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];
for (cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
} else {
//TODO Cookieが保持されていない
}
NSArray *cookieArray = [NSArray arrayWithObjects: cookie, nil];
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
[request setAllHTTPHeaderFields:headers];
//ネットワーク接続を開始
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection release];
[request release];
}
#pragma mark -
//サーバーから認証の要求があった時に呼び出される
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
}
//サーバーからレスポンスがあった時に呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//Cookieを保存
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
[[NSUserDefaults standardUserDefaults] setObject:cookiesData
forKey:@"cookie"];
//レスポンスコード取得
statusCode = [((NSHTTPURLResponse *)response) statusCode];
if ([receivedData retainCount]) {
[receivedData release];
}
receivedData = [[NSMutableData alloc] init];
}
//サーバーからデータを受け取ると呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
//サーバーからの通信が終わる直前に呼び出される
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//TODO receivedDataにbodyが入っている
}
2013年5月14日火曜日
[iOS]Interface BuilderでUIScrollViewのContentSizeを設定することはできない。
Interface BuilderでUIScrollViewのContentSizeを設定使用と思ったが、
それを行うUIが無いことに気づいた。
http://stackoverflow.com/questions/1135163/how-do-i-use-uiscrollview-in-interface-builder
によると、ContentSizeはコード状態のみ設定可能のようだ、IBでやるのは諦めよう。
ただしiOS6のAuto Layoutを使用すれば可能になる。
With Autolayout (iOS6+), you can avoid setting contentSize. Set the following constraints instead:
Pin the top of the scrollview to the top of its top-most child.
And pin the bottom to the bottom of its bottom-most child.
2013年3月15日金曜日
[iOS]iOSシミュレーターの格納場所
フォルダパスは
/Users/[User Name]/Library/Application Support/iPhone Simulator/6.1
または
/Library/Application Support/iPhone Simulator/6.1
iOSのバージョンごとにフォルダ分けされている。
Applicationsフォルダ内の
HOGE1234-HO77-HO77-HO77-HOGE123456789
などのフォルダがアプリ格納の場所。
ファイルのアウトプットはDocuments内に格納される。
/Users/[User Name]/Library/Application Support/iPhone Simulator/6.1
または
/Library/Application Support/iPhone Simulator/6.1
iOSのバージョンごとにフォルダ分けされている。
Applicationsフォルダ内の
HOGE1234-HO77-HO77-HO77-HOGE123456789
などのフォルダがアプリ格納の場所。
ファイルのアウトプットはDocuments内に格納される。
2013年3月8日金曜日
[iOS]Enumeration values * not handled in switch
Enumeration values * not handled in switch
列挙子*はswitchで処理されていません。
つまりswitchで列挙子をすべて網羅する必要がある。
登録:
投稿 (Atom)
