LoginSignup
30
30

More than 5 years have passed since last update.

UIWebViewを非同期で使う

Posted at

UIWebViewの普通の使い方は以下の通り:

    [self.webView loadRequest:r];

しかしこの動かし方は同期的なので、メインスレッドを消費してしまう。

そこでAFNetworkingで用意されている非同期版UIWebViewを使う。

ViewController.h
#import <AFNetworking/AFNetworking.h>
#import <AFNetworking/UIWebView+AFNetworking.h>
ViewController.m
    [self.webView loadRequest:r
                     progress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite){
        NSLog(@"%lu bytes has been written", (unsigned long)bytesWritten);}
                      success:^NSString *(NSHTTPURLResponse *response, NSString *HTML){
        return HTML;}
                      failure:^(NSError *error){}
     ];

これで非同期でUIWebViewにWebページを読み込むことができる。

30
30
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
30
30