UIWebViewを使用する際に必要な機能を搭載した「DCWebView 」クラスを作成しました。
主な機能
ウェブビューの初期化と取得
ページのローディング中にアクティビティインジケータを表示
ページが画面の上下端でバウンスしないよう指定可能
その他 UIWebViewから継承されたサブクラス/プロパティの使用
ダウンロード
使用方法
1
2
3
4
5
6
7
8
// ウェブビュー生成
DCWebView * webView = [[ DCWebView alloc ] initWithFrame: [[ UIScreen mainScreen ] applicationFrame ]];
// ビューに追加
[ self . view addSubview: webView ];
// URL読み込み
[ webView loadUrl: @"https://dolice.net/mb/" view: self . view ];
画面上下の端でバウンスしないよう指定
ソースコード
DCWebView.h
1
2
3
4
5
6
7
8
9
10
11
12
#import <UIKit/UIKit.h>
@interface DCWebView : UIWebView < UIWebViewDelegate >
#pragma mark - property
@property UIActivityIndicatorView * indicator ;
#pragma mark - public method
- ( void ) loadUrl: ( NSString * ) targetUrl view: ( UIView * ) view ;
- ( void ) bounces: ( BOOL ) isAllow ;
@end
DCWebView.m
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
70
71
72
73
74
75
76
77
78
79
#import "DCWebView.h"
@implementation DCWebView
- ( id ) initWithFrame: ( CGRect ) frame
{
self = [ super initWithFrame: frame ];
if ( self ) {
self . delegate = self ;
self . backgroundColor = [ UIColor whiteColor ];
self . scalesPageToFit = NO ;
self . opaque = NO ;
}
return self ;
}
#pragma mark - delegate method
// ページ読込開始時にインジケータ表示
- ( void ) webViewDidStartLoad: ( UIWebView * ) webView
{
// ステータスバーのインジケータ表示
[ UIApplication sharedApplication ]. networkActivityIndicatorVisible = YES ;
}
// ページ読込完了時にインジケータ非表示
- ( void ) webViewDidFinishLoad: ( UIWebView * ) webView
{
// アクティビティインジケータ非表示
[ self stopActivityIndicatorAnimation ];
// ステータスバーのインジケータ非表示
[ UIApplication sharedApplication ]. networkActivityIndicatorVisible = NO ;
}
#pragma mark - public method
// URL読み込み
- ( void ) loadUrl: ( NSString * ) targetUrl view: ( UIView * ) view
{
NSURL * url = [ NSURL URLWithString: targetUrl ];
NSURLRequest * request = [ NSURLRequest requestWithURL: url ];
[ self loadRequest: request ];
// アクティビティインジケータ表示
[ self startActivityIndicatorAnimation: view ];
}
// バウンスさせるか
- ( void ) bounces: ( BOOL ) isAllow
{
for ( id subview in self . subviews ) {
if ([[ subview class ] isSubclassOfClass: [ UIScrollView class ]]) {
(( UIScrollView * ) subview ). bounces = isAllow ;
}
}
}
#pragma mark - private method
// アクティビティインジケータ表示
- ( void ) startActivityIndicatorAnimation: ( UIView * ) view
{
_indicator = [[ UIActivityIndicatorView alloc ] init ];
_indicator . activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray ;
_indicator . frame = CGRectMake ( 0 , 0 , 20 , 20 );
_indicator . center = view . center ;
_indicator . hidesWhenStopped = YES ;
[ _indicator startAnimating ];
[ view addSubview: _indicator ];
}
// アクティビティインジケータ非表示
- ( void ) stopActivityIndicatorAnimation
{
[ _indicator stopAnimating ];
}
@end
お薦めの参考書
絶対に挫折しない iPhoneアプリ開発「超」入門 増補改訂第4版
Swiftについて知りたい開発者の方のみならず、プログラミング未経験者の方にも参考になる内容になっています。Swiftの基礎を一から丁寧に解説されており、この書籍があればネットで調べる手間をかなり省くことができると思います。