Posts Tagged ‘Objective-C’

MacアプリでURLをブラウザで開く方法

これでHTTPなURLスキームに対応しているアプリのidentifierの一覧が取れます。

NSArray *identifiers = (NSArray*)LSCopyAllHandlersForURLScheme((CFStringRef)@"http");

ただ、このままだとブラウザでないアプリケーション(例えばEvernoteなど)も取れてしまいますので、うまい具合にブラウザだけ取る方法があれば教えてください。

次にURLをデフォルトブラウザで開く方法

NSURL *url = [NSURL URLWithString:@"http://www.example.com/"];
[[NSWorkspace sharedWorkspace] openURL:url];

最後にURLを指定のアプリケーションで開く方法

NSURL *url = [NSURL URLWithString:@"http://www.example.com/"];
[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:url]
                withAppBundleIdentifier:@"com.apple.Safari" //取得したidentifierを渡してやる
                                options:NSWorkspaceLaunchDefault
         additionalEventParamDescriptor:nil
                      launchIdentifiers:nil];
Facebooktwitterlinkedintumblrmail

画面いっぱいのUITextViewがキーボードに隠れないようにする

iPadで画面いっぱいにUITextViewを配置すると、入力状態になった際にキーボードが出てきて入力カーソルが隠れてしまいます。これには「キーボードが表示・非表示になるタイミングで、UITextViewの高さを変えてあげる」ことで 対処可能です。UICatalogのサンプルが少し古く、3.2ではDeprecatedになっているUIKeyboardBoundsUserInfoKeyを使っていたため、UIKeyboardFrameEndUserInfoKeyを使うようにしてみました。恐らくiPhoneでも同じようにできるはずです。

@interface CustomViewController : UIViewController  {
    UITextView *_textView; //対象となるテキストビュー。UIViewControllerのサブビューとして追加する。
}
@property (nonatomic, retain) IBOutlet UITextView *_textView;
@end


@implementation CustomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];    
    //親ビューの高さが変更された時に追従するようにする
    _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //キーボード表示・非表示の通知の開始
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    //キーボード表示・非表示の通知を終了
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

//キーボードが表示された場合
- (void)keyboardWillShow:(NSNotification *)aNotification {
    //キーボードのCGRectを取得
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [[self.view superview] convertRect:keyboardRect fromView:nil];
    
    //キーボードのanimationDurationを取得
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    //メインビューの高さをキーボードの高さ分マイナスしたframe
    CGRect frame = self.view.frame;
    frame.size.height -= keyboardRect.size.height;

    //キーボードアニメーションと同じ間隔でメインビューの高さをアニメーションしつつ変更する。
    //これでUITextViewも追従して高さが変わる。
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

//キーボードが非表示にされた場合(keyboardWillShowと同じことを高さを+してやっているだけ)
- (void)keyboardWillHide:(NSNotification *)aNotification {
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [[self.view superview] convertRect:keyboardRect fromView:nil];

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect frame = self.view.frame;
    frame.size.height += keyboardRect.size.height;

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

@end
Facebooktwitterlinkedintumblrmail