自作アプリから、ボタン一発で Safari を開きたい!
[[UIApplicationsharedApplication] openURL:@”ここにURL”]
ためしに、ボタン一発で Google を開くサンプルでも作ってみます。
Single View Application でプロジェクトを作成した後に、ViewController.m にこんな感じで実装を追加してみます。
– (void)viewDidLoad
{
[superviewDidLoad];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@”Open Google by safari”forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(openBrowser) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(0, 0, 200, 50);
btn.center = self.view.center;
[self.view addSubview:btn];
}
– (void)openBrowser
{
NSURL *url = [NSURLURLWithString:@”http://goolge.com”];
[[UIApplicationsharedApplication] openURL:url];
}
これで OK