LoginSignup
0
0

More than 5 years have passed since last update.

iOS UIWebView で404などのステータスコードを取得する。

Last updated at Posted at 2018-02-27

UIWebViewでステータスコード取得

UIWebViewはステータスコードを簡単に取得できないくそAPIでした!

が下記のようにすれば取得できます。

//
//  ViewController.m
//  lp
//
//  Created by Shichimitoucarashi on 2018/02/26.
//  Copyright © 2018年 keisuke yamagishi. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *web;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL* url = [NSURL URLWithString: @"http://shichimitoucarashi.com/api/image/88a8e8049f751ac/TE/"];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    self.web.delegate = self;
    [self.web loadRequest:request];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSCachedURLResponse *urlResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) urlResponse.response;
    NSInteger statusCode = httpResponse.statusCode;

    NSLog(@"status code: %ld",(long)statusCode);
}

@end

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0