블로그 이미지
유틸리티를 여기저기 찾기가 힘들어서 이 블로그에 좀 보아모았습니다. 되도록이면 이곳에 오시는 분들이 사용하기 좋은 프로그램을 찾아서 올렸구요. 혹시 궁금한 것이 있으면 명록이나 404page@hanmail.net 이나, 아래 보이는 웹메신저를 통해 물어보세요.
404page

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

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
  • total
  • today
  • yesterday

'2013/07'에 해당되는 글 1건

  1. 2013.07.29 WebViewController
2013. 7. 29. 22:22 카테고리 없음

===============

.h

===============

#import <UIKit/UIKit.h>


@interface WebViewController : UIViewController


- (void) loadUrl:(NSURL*) url;


@end

===================

.m

===================

#import "WebViewController.h"


@interface WebViewController () < UIWebViewDelegate, UIActionSheetDelegate >


@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@property (nonatomic, retain) UIWebView *webView;

@property (nonatomic, retain) UIBarButtonItem *buttonForward;

@property (nonatomic, retain) UIBarButtonItem *buttonBackward;


- (IBAction)onClose:(id)sender;

- (IBAction)onBackward:(id)sender;

- (IBAction)onForward:(id)sender;

- (IBAction)onReload:(id)sender;


@end


@implementation WebViewController


@synthesize activityIndicator = _activityIndicator;

@synthesize webView = _webView;

@synthesize buttonForward = _buttonForward;

@synthesize buttonBackward = _buttonBackward;


- (void)loadView

{

    // 툴바 사용

    [self.navigationController setToolbarHidden:NO animated:YES];

    

    // 화면 사이즈를 가져와서 네비게이션과 툴바의 사이즈만큼을

    // webview 보일 있는 영역을 구한다.

    CGRect viewRect = self.navigationController.view.bounds;

    if (! self.navigationController.isNavigationBarHidden) {

        viewRect.size.height -= 44;

    }

    if (! self.navigationController.isToolbarHidden){

        viewRect.size.height -= 44;

    }

    

    UIView *view = [[UIView alloc] initWithFrame:viewRect];

    

    // Orientaion 변경되도 화면을 꽉채울 있도록 설정

    UIViewAutoresizing completeFlexibleFlag = UIViewAutoresizingFlexibleLeftMargin

    | UIViewAutoresizingFlexibleLeftMargin

    | UIViewAutoresizingFlexibleWidth

    | UIViewAutoresizingFlexibleRightMargin

    | UIViewAutoresizingFlexibleTopMargin

    | UIViewAutoresizingFlexibleHeight

    | UIViewAutoresizingFlexibleBottomMargin ;

    

    [view setAutoresizingMask: completeFlexibleFlag];

    

    UIWebView *webView = [[UIWebView alloc] initWithFrame:viewRect];

    [webView setAutoresizingMask:completeFlexibleFlag];

    webView.delegate = self;

    self.webView = webView;

    [view addSubview:webView];

    

    self.view = view;

    

    self.view.autoresizesSubviews = YES;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

    self.title = @"Loading.";

    

    [self.view setBackgroundColor:[UIColor redColor]];

    

    // 네비게이션 설정

    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone

                                                                                          target:self

                                                                                          action:@selector(onClose:)];

    

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];

    self.activityIndicator = activityIndicator;

    

    // 툴바 설정

    UIBarButtonItem *btnBackward = [[UIBarButtonItem alloc] initWithTitle:@"◀\U0000FE0E"

                                                                    style:UIBarButtonItemStylePlain

                                                                   target:self

                                                                   action:@selector(onBack:)];

    

    UIBarButtonItem *btnForward = [[UIBarButtonItem alloc] initWithTitle:@"▶\U0000FE0E"

                                                                   style:UIBarButtonItemStylePlain

                                                                  target:self

                                                                  action:@selector(onForward:)];

    

    UIBarButtonItem *btnReload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh

                                                                               target:self

                                                                               action:@selector(onReload:)];

    

    

    UIBarButtonItem *btnAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction

                                                                               target:self

                                                                               action:@selector(onAction:)];

    

    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

                                                                           target:nil

                                                                           action:nil];

    

    self.buttonBackward = btnBackward;

    self.buttonForward = btnForward;

    self.buttonBackward.enabled = NO;

    self.buttonForward.enabled = NO;

    

    self.toolbarItems = [[NSArray alloc] initWithObjects:space, btnBackward, space, btnForward, space, btnReload, space, btnAction, space, nil];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)viewDidUnload {

    [self setWebView:nil];

    [super viewDidUnload];

}


- (void) loadUrl:(NSURL*) url

{

    [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:url]];

}



#pragma mark - webview delegate


- (void)webViewDidStartLoad:(UIWebView *)webView

{

    [self.activityIndicator startAnimating];

}


- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    self.buttonForward.enabled = webView.canGoForward;

    self.buttonBackward.enabled = webView.canGoBack;

    

    [self.activityIndicator  stopAnimating];

    

    self.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];

}


#pragma mark - UIActionSheetDelegate handler


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 0 /* open in safari */) {

        if( [[UIApplication sharedApplication] canOpenURL:self.webView.request.URL ]){

            [[UIApplication sharedApplication] openURL:self.webView.request.URL];

        }

    }

}


#pragma mark - event handler


- (IBAction)onBackward:(id)sender{

    [self.webView goBack];

}


- (IBAction)onForward:(id)sender{

    [self.webView goForward];

    

}


- (IBAction)onReload:(id)sender{

    [self.webView reload];

}


- (IBAction)onClose:(id)sender {

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    [self.navigationController setToolbarHidden:YES animated:YES];

    [self.navigationController popViewControllerAnimated:YES];

}


- (IBAction)onAction:(id)sender {

    

    NSString* textCancel = @"Cancel";

    NSString* textOpenSafari = @"Open in Safari";

    

    UIActionSheet *acitonSheet = [[UIActionSheet alloc] initWithTitle:@""

                                                             delegate:self

                                                    cancelButtonTitle:textCancel

                                               destructiveButtonTitle:textOpenSafari

                                                    otherButtonTitles:nil ];

    

    [acitonSheet showFromToolbar:self.navigationController.toolbar];

}


@end

posted by 404page