参照:
- http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIWebViewDelegate/webView:shouldStartLoadWithRequest:navigationType:
- http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIWebView
- http://forums.macrumors.com/showthread.php?t=535312
UIWebViewDelegateを利用する。
Interface BuilderでUIWebViewのdelegateを設定する。
以下のコードを追加
// リンクをクリック時、Safariを起動する為の処理
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(@"URL = %@", [[request URL] absoluteString]);
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
以下はソース全体
WebViewTestAppDelegate.h
//
// WebViewTestAppDelegate.h
// WebViewTest
//
// Created by BABUKUMA on 10/08/30.
// Copyright babukuma.com 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WebViewTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
WebViewTestAppDelegate.m
//
// WebViewTestAppDelegate.m
// WebViewTest
//
// Created by BABUKUMA on 10/08/30.
// Copyright babukuma.com 2010. All rights reserved.
//
#import "WebViewTestAppDelegate.h"
@implementation WebViewTestAppDelegate
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[window makeKeyAndVisible];
// WebViewの初期表示
NSLog(@"BABUKUMA : WebViewの初期表示");
NSURL *url = [NSURL URLWithString:@"http://babukuma.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest: request];
return YES;
}
// リンクをクリック時、Safariを起動する為の処理
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(@"URL = %@", [[request URL] absoluteString]);
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end