‘Objective-C’ タグのついている投稿

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

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

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

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

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

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

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

1
2
3
4
5
6
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];

Popularity: 8% [?]

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@interface CustomViewController : UIViewController <UITextViewDelegate> {
    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

Popularity: 100% [?]