fuzigiwa2
@fuzigiwa2 (義将 藤極)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

WKWebViewのデリゲート設定でクラッシュ

Object-Cで開発しております。
古いプロジェクトを現環境に合わせるための対応を行っております。

UIWebViewを使用していた個所をWKWebViewに差し替え作業を行っております。

// ViewController.h

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#import "MBProgressHUD.h"

@interface HoneyShopViewController : UIViewController<WKNavigationDelegate>
{
    IBOutlet    WKWebView          *webView;
}
@property (strong, nonatomic) MBProgressHUD     *hud;

// ViewController.m

#import "ViewController.h"
#import <WebKit/WebKit.h>
@import GoogleMobileAds;

@interface ViewController ()
@property (weak, nonatomic) IBOutlet GADBannerView *bannerView;
@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor   = BODY_COLOR;

    webView.navigationDelegate = self;

    [webView.layer setBorderColor:LOG_COLOR.CGColor];
    [webView.layer setBorderWidth:2.0f];

    // Do any additional setup after loading the view from its nib.
    PBAppDelegate *appDelegate = (PBAppDelegate *)[UIApplication sharedApplication].delegate;
    _hud = [[MBProgressHUD alloc]initWithWindow:appDelegate.window];
    [appDelegate.window addSubview:_hud];

    // In this case, we instantiate the banner with desired ad size.
    self.bannerView = [[GADBannerView alloc]
        initWithAdSize:kGADAdSizeBanner];

    self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
    //self.bannerView.adUnitID = @"ca-app-pub-4057658985177158~5712178529";
    self.bannerView.rootViewController = self;
    GADRequest *request = [GADRequest request];
    [self.bannerView loadRequest:request];
    [self addBannerViewToView:self.bannerView];
}

- (void)addBannerViewToView:(UIView *)bannerView {
  bannerView.translatesAutoresizingMaskIntoConstraints = NO;
  [self.view addSubview:bannerView];
  [self.view addConstraints:@[
    [NSLayoutConstraint constraintWithItem:bannerView
                               attribute:NSLayoutAttributeBottom
                               relatedBy:NSLayoutRelationEqual
                                  toItem:self.bottomLayoutGuide
                               attribute:NSLayoutAttributeTop
                              multiplier:1
                                constant:0],
    [NSLayoutConstraint constraintWithItem:bannerView
                               attribute:NSLayoutAttributeCenterX
                               relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                               attribute:NSLayoutAttributeCenterX
                              multiplier:1
                                constant:0]
                                ]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.title = @"タイトル";
    self.hidesBottomBarWhenPushed = NO;
    PBDataManager   *manager    = [[[PBDataManager alloc]init]autorelease];
    NSDictionary    *user       = [manager getUserData];

    NSString        *urlStr     = HONEY_SHOP_URL;
    NSString        *uid        = [user valueForKey:@"uid"];
    NSString        *platform   = @"ios";
    NSString        *version    = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];

    [_hud show:YES];
    NSDictionary    *postParams     = [[NSDictionary alloc] initWithObjectsAndKeys:uid, @"uid",
                                       [manager getToken],@"token",
                                       platform, @"platform",
                                       version, @"version",
                                       nil];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlStr]];
    [httpClient setDefaultHeader:@"x-appid" value:@"hogehoge"];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:urlStr
                                                      parameters:postParams];
    AFHTTPRequestOperation *operation   = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary    *dict =(NSDictionary *)[[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]JSONValue];
        [webView loadHTMLString:[dict objectForKey:@"message"] baseURL:[[NSBundle mainBundle] pathForResource:@"html_template" ofType:@"html"]];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Shop Error: %@", error);
        [_hud hide:YES];
    }];

    [operation start];
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if ([navigationAction.request.URL.scheme isEqualToString:@"inapp"]) {
        if ([[PBStoreManager sharedInstance] canMakePayment]) {
            [_hud show:YES];
        }
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic] == YES){ 
        NSLog(@"didReceiveAuthenticationChallenge");
    }else{
        completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil);
    }
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [_hud hide:YES];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
   [_hud hide:YES];
}

上記のようなコードに修正したのですが、

webView.navigationDelegate = self;



Exception NSException * "-[UIWebView setNavigationDelegate:]: unrecognized selector sent to instance 0x15ebe0d0" 0x15e7b0c0

というエラーが出てクラッシュしてしまいます。
デリゲートの設定を行わなければクラッシュせず表示されるのですが、イベントの取得が出来ず、WebView内でのボタン反応が取れない状態です。

こちら原因や修正方法を教えてください。

0

No Answers yet.

Your answer might help someone💌