▼iPhoneプログラミングメモ▼
WEBビュー

WEBビューを利用するプログラムを作成する。


ソースコードの記述
WebViewEx.h
#import <UIKit/UIKit.h>

//WebViewExの定義
@interface WebViewEx : UIViewController <UIWebViewDelegate> {
    UIWebView* _webView;
}
@end

WebViewEx.m
#import "WebViewEx.h"

//WebViewExの実装
@implementation WebViewEx

//アラートの表示
- (void)showAlert:(NSString*)title text:(NSString*)text {
    UIAlertView* alert=[[[UIAlertView alloc] 
        initWithTitle:title message:text delegate:nil 
        cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}

//WEBビューの生成
- (UIWebView*)makeWebView:(CGRect)rect {    
    //WEBビューの生成
    UIWebView* webView=[[[UIWebView alloc] init] autorelease];
    [webView setFrame:rect];
    [webView setBackgroundColor:[UIColor blackColor]];
    [webView setScalesPageToFit:NO];
    
    //ビューのサイズの自動調整
    webView.autoresizingMask=
        UIViewAutoresizingFlexibleWidth|
        UIViewAutoresizingFlexibleHeight|
        UIViewAutoresizingFlexibleLeftMargin|
        UIViewAutoresizingFlexibleRightMargin|
        UIViewAutoresizingFlexibleTopMargin|
        UIViewAutoresizingFlexibleBottomMargin;
    return webView;
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];
        
    //Webビューの生成
    _webView=[[self makeWebView:self.view.frame] retain];
    [_webView setDelegate:self];
    [self.view addSubview:_webView];

    //インジケーターの表示
    [UIApplication sharedApplication].
        networkActivityIndicatorVisible=YES;
        
    //WEB上のHTMLの読み込み
    [_webView loadRequest:[NSURLRequest 
        requestWithURL:[NSURL URLWithString:
        @"http://npaka.net"]]];
}

//メモリの解放
- (void)dealloc {
    //WEBビューはメモリ解放前にデリゲート解除
    _webView.delegate=nil;
    [_webView release];
    [super dealloc];
}

//文字列の前方一致検索
- (int)indexOf:(NSString*)str c:(NSString*)c {
    NSRange range=[str rangeOfString:c];
    if (range.location==NSNotFound) return -1;
    return range.location;
}

//HTML読み込み開始時に呼ばれる
- (BOOL)webView:(UIWebView*)webView 
    shouldStartLoadWithRequest:(NSURLRequest*)request 
    navigationType:(UIWebViewNavigationType)navigationType {
    //クリック時
    if (navigationType==UIWebViewNavigationTypeLinkClicked || 
        navigationType==UIWebViewNavigationTypeFormSubmitted) {
        //通信中の時は再度URLジャンプさせない
        if ([UIApplication sharedApplication].
            networkActivityIndicatorVisible) return NO;       

        //リクエストからのURL文字列取得
        NSString* url=[[request URL] relativeString];
        NSLog(@"url>%@",url);

        //メーラーの起動
        if ([self indexOf:url c:@"mailto:"]==0) {
            [[UIApplication sharedApplication] 
                openURL:[NSURL URLWithString:url]];
            return NO;
        }             

        //ページ内リンクの時はインジケーターを表示しない
        if ([self indexOf:url c:@"#"]>=0) {
            return YES;
        }
        
        //インジケーターの表示
        [UIApplication sharedApplication].
            networkActivityIndicatorVisible=YES;
    }
    return YES;
}

//HTML読み込み成功時に呼ばれる
- (void)webViewDidFinishLoad:(UIWebView*)webView {
    //インジケーターの非表示
    [UIApplication sharedApplication].
        networkActivityIndicatorVisible=NO;
}

//HTML読み込み失敗時に呼ばれる
- (void)webView:(UIWebView*)webView 
    didFailLoadWithError:(NSError*)error {
    //インジケーターの非表示
    [UIApplication sharedApplication].
        networkActivityIndicatorVisible=NO;
    
    //アラート
    [self showAlert:@"" text:@"通信失敗しました"];
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end


−戻る−