Wednesday, February 8, 2023
HomeiOS Developmentios - Goal-C WKWebView: didFinish not triggering when WebView finishes loading

ios – Goal-C WKWebView: didFinish not triggering when WebView finishes loading


My app is Goal-C, sure, fairly outdated, however I am migrating little by little, so I need assistance with Goal-C, not Swift.

I am migrating UIWebView to WKWebView and having issues to make didFinish to work.

This the category the place I take advantage of the WebView, TMAnswerView:

#import "TMAnswerView.h"
#import "TMConsts.h"
#import "TMPersistanceManager.h"
#import <WebKit/WebKit.h>

@interface TMAnswerView () <WKNavigationDelegate>
//UIWebViewDelegate
@finish

@implementation TMAnswerView.m

-(void)customInit{
        
}

-(void)setAnswer:(TMAnswerModel *)reply{
    
    _answer = reply;
    
    float font = 17;
    NSNumber *sort = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
    if([type isEqual:SettingsFontSizeType1]){
        font = font * 0.75;
    }else if([type isEqual:SettingsFontSizeType3]){
        font = font * 1.25;
    }else if([type isEqual:SettingsFontSizeType4]){
        font = font * 1.5;
    }else if([type isEqual:SettingsFontSizeType5]){
        font = font * 2;
    }
    
    NSString *htmlBody = [TMUtils getHTMLStringForMath:[answer.answer stringByReplacingOccurrencesOfString:@"$$" withString:@"$"] andFontSize:(int)font];
    
    [_answerWebView loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/", [[NSBundle mainBundle] bundlePath]]]];
    _answerWebView.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
    
}

#pragma mark - click on listeners

- (IBAction)onCheckButton:(id)sender {
    if(_viewControllerType != TMMainTestViewConstrollerTypeDoTest){
        return;
    }
    _checkButton.chosen = !_checkButton.chosen;
    if(_delegate){
        [_delegate onCheckChanged:_answer];
    }
}

- (void)webView:(WKWebView *)webView
didFinishNavigation:(WKNavigation *)navigation{
    [self setWebViewHeight];
}

//-(void)webViewDidFinishLoad:(WKWebView *)webView{
//    [self setWebViewHeight];
//}

-(void) setWebViewHeight{
    CGSize fittingSize = [_answerWebView sizeThatFits:CGSizeZero];
    _heightOfWebView.fixed = fittingSize.top;
}

@finish

Right here I’ve changed UIWebView delegate by WKNavigationDelegate. I’ve to say that’s working positive with the outdated webViewDidFinishLoad of the UIWebView.

TMAnswerView.h:

#import "TMCustomView.h"
#import "TMAnswerModel.h"
#import "TMMainTestViewController.h"
#import <WebKit/WebKit.h>

@protocol TMAnswerViewProtocol <NSObject>

-(void) onCheckChanged:(TMAnswerModel*) reply;

@finish

@interface TMAnswerView : TMCustomView

@property (nonatomic, robust) TMAnswerModel *reply;

@property (weak, nonatomic) IBOutlet UIButton *checkButton;
@property (weak, nonatomic) IBOutlet WKWebView *answerWebView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfWebView;

@property (weak, nonatomic) id<TMAnswerViewProtocol> delegate;

@property (nonatomic) TMMainTestViewConstrollerType viewControllerType;
-(void) setWebViewHeight;

@finish

And eventually within the storyboard I’ve added a WebKitView ingredient in substitute of the outdated UIWebView.

I additionally tried with didFinishNavigation to no avail.

I checked this subsequent web page as reference:

WKWebView

Something improper in my code? Any assistance on making the didFinish occasion set off?

Edit 1:

I additionally tried:

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self setWebViewHeight];
}

to no avail.

Edit 2:
That is the category the place the WKWebView is load, TMQuestionView:

#import "TMQuestionView.h"
#import "TMColors.h"
#import "TMDBManager.h"
#import "TMConsts.h"
#import "TMAnswerModel.h"
#import "TMAnswerView.h"
#import "TMViewUtils.h"
#import "TMPersistanceManager.h"
#import "TMImagePreviewView.h"
#import <WebKit/WebKit.h>

@interface TMQuestionView () <TMAnswerViewProtocol, WKNavigationDelegate>

@property (weak, nonatomic) IBOutlet WKWebView *webView;
@property (weak, nonatomic) IBOutlet UIView *answersView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webviewHeight;
@property (weak, nonatomic) IBOutlet UIView *viewForLoading;
@property (weak, nonatomic) IBOutlet UIView *loadingView;
@property (weak, nonatomic) IBOutlet WKWebView *webviewExplanations;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webviewExplanationsHeight;

@property (nonatomic) BOOL isExplanationsVisible;

@property (nonatomic, robust) NSMutableArray *photos;

@property (nonatomic, robust) NSString *tempic;

@finish

@implementation TMQuestionView

-(void)customInit{
    
    [[CSLoadingManager sharedManager] addLoadingViewToView:_viewForLoading withColor:TMBaseColor(1)];
    _images = [NSMutableArray new];
    
}

-(void)setQuestion:(TMQuestionModel *)query{
    _question = query;
    
    float font = 17;
    NSNumber *sort = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
    if([type isEqual:SettingsFontSizeType1]){
        font = font * 0.75;
    }else if([type isEqual:SettingsFontSizeType3]){
        font = font * 1.25;
    }else if([type isEqual:SettingsFontSizeType4]){
        font = font * 1.5;
    }else if([type isEqual:SettingsFontSizeType5]){
        font = font * 2;
    }
    
    [_images addObjectsFromArray:[TMUtils getImagesFromQuestion:question.question]];
    [_images addObjectsFromArray:[TMUtils getImagesFromQuestion:question.instructions]];
    
    NSString *htmlString = query.query;
    if(query.directions.size > 0 && ![question.instructions isEqualToString:@"(null)"]){
        htmlString = [NSString stringWithFormat:@"%@<br/>%@", question.instructions, question.question];
    }
    NSString *htmlBody = [TMUtils getHTMLStringForMath:htmlString andFontSize:(int)font];
    htmlBody = [htmlBody stringByReplacingOccurrencesOfString:@"<center>" withString:@"<p style="text-align:center;">"];
    htmlBody = [htmlBody stringByReplacingOccurrencesOfString:@"</center>" withString:@"</p>"];

    _tempic = htmlBody;
    
    [_webView loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/", [[NSBundle mainBundle] bundlePath]]]];
    _webView.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
    _answersView.hidden = YES;
    
    [[TMDBManager sharedManager] getAnswersForQuestion:_question completition:^(NSDictionary *dict) {
        
        NSArray *temp = [dict objectForKey:DBReturnAnswers];
        NSSortDescriptor *sortDescriptor;
        sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"answerNumber" ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        self->_answers = [temp sortedArrayUsingDescriptors:sortDescriptors];

        [self setViewAnswers];
    }];
    
}

-(void) setExplanations{
    _isExplanationsVisible = YES;
    float font = 17;
    NSNumber *sort = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
    if([type isEqual:SettingsFontSizeType1]){
        font = font * 0.75;
    }else if([type isEqual:SettingsFontSizeType3]){
        font = font * 1.25;
    }else if([type isEqual:SettingsFontSizeType4]){
        font = font * 1.5;
    }else if([type isEqual:SettingsFontSizeType5]){
        font = font * 2;
    }
    NSString *htmlString = _question.clarification;
    NSString *htmlBody = [TMUtils getHTMLStringForMath:htmlString andFontSize:(int)font];
    [_webviewExplanations loadHTMLString:htmlBody baseURL:[NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/", [[NSBundle mainBundle] bundlePath]]]];
    _webviewExplanations.scrollView.contentInset = UIEdgeInsetsMake(0,-8,0,-8);
}

-(void)setRecordAnswer:(TMRecordAnswerModel *)recordAnswer{
    _recordAnswer = recordAnswer;
}

-(void) setViewAnswers{
    int i = 0;
    
    float font = 17;
    NSNumber *sort = [TMPersistanceManager fetchObjectForKey:PERSettingsFontSize];
    if([type isEqual:SettingsFontSizeType1]){
        font = font * 0.75;
    }else if([type isEqual:SettingsFontSizeType3]){
        font = font * 1.25;
    }else if([type isEqual:SettingsFontSizeType4]){
        font = font * 1.5;
    }else if([type isEqual:SettingsFontSizeType5]){
        font = font * 2;
    }
    
    for(TMAnswerModel *merchandise in _answers){
        
        TMAnswerView *view = [[TMAnswerView alloc] init];
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [_answersView addSubview:view];
        [[view.leadingAnchor constraintEqualToAnchor:_answersView.leadingAnchor constant:0] setActive:YES];
        [[view.rightAnchor constraintEqualToAnchor:_answersView.rightAnchor constant:0] setActive:YES];
        if(i == 0){
            [[view.topAnchor constraintEqualToAnchor:_answersView.topAnchor constant:0] setActive:YES];
        }else{
            UIView *lastView = [[_answersView subviews] objectAtIndex:i-1];
            [[view.topAnchor constraintEqualToAnchor:lastView.bottomAnchor constant:0] setActive:YES];
        }

        view.reply = merchandise;
        view.delegate = self;
        view.viewControllerType = _viewControllerType;
                
        if(_recordAnswer){
            if(merchandise.isCorrect == 1){
                if([_recordAnswer.selectedAnswerId isEqualToString:item.answerId]){
                    [view.checkButton setImage:[UIImage imageNamed:@"checkbox_checked"] forState:UIControlStateNormal];
                }else{
                    [view.checkButton setImage:[UIImage imageNamed:@"checkbox_checked_gray"] forState:UIControlStateNormal];
                }
            }else{
                if([_recordAnswer.selectedAnswerId isEqualToString:item.answerId]){
                    [view.checkButton setImage:[UIImage imageNamed:@"checkbox_error"] forState:UIControlStateNormal];
                }
            }
        }


        i++;
        
        if(i == [_answers count]){
            [[view.bottomAnchor constraintEqualToAnchor:_answersView.bottomAnchor constant:0] setActive:YES];
        }
    }
}

-(void)onCheckChanged:(TMAnswerModel *)reply{
    for (TMAnswerView *merchandise in [_answersView subviews]){
        if(![item.answer isEqual:answer]){
            if(merchandise.checkButton.chosen){
                merchandise.checkButton.chosen = NO;
            }
        }
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self setHeightOfWebView];
}

//-(void)webViewDidFinishLoad:(WKWebView *)webView{
//    [self setHeightOfWebView];
//}

-(TMAnswerModel*) getSelectedAnswer{
    for (TMAnswerView *merchandise in [_answersView subviews]){
        if(merchandise.checkButton.chosen){
            return merchandise.reply;
        }
    }
    return nil;
}

-(void) setHeightOfWebView{
    _viewForLoading.hidden = YES;
    _loadingView.hidden = YES;
    CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
    _webviewHeight.fixed = fittingSize.top;
    _answersView.hidden = NO;
    
    for(UIView *merchandise in [_answersView subviews]){
        if([item isKindOfClass:[TMAnswerView class]]){
            [((TMAnswerView*) item) setWebViewHeight];
        }
    }
    
    if(_isExplanationsVisible){
        CGSize fittingSizeExplanations = [_webviewExplanations sizeThatFits:CGSizeZero];
        _webviewExplanationsHeight.fixed = fittingSizeExplanations.top;
    }

}

- (IBAction)onButtonAboveWebViewClicked:(id)sender {
    if([_images count] > 0){

        TMImagePreviewView *view = [[TMImagePreviewView alloc] initWithFrame:CGRectMake(0, 0, kAppWidth, kAppHeight)];
        [view setImages:_images];
        [[[self superview] superview] addSubview:view];
        [view fadeIn];
        
    }
}

- (IBAction)onButtonTemp:(id)sender forEvent:(UIEvent *)occasion {
    NSSet *touches = [event touchesForView:sender];
    UITouch *contact = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[sender superview]];
    NSLog(@"%@", NSStringFromCGPoint(touchPoint));
    
    
    lengthy htmlLength = _tempic.size;
    
    
    lengthy heightOfWebView = _webView.body.measurement.top;
    
    double percentTouch = (double)touchPoint.y / (double)heightOfWebView;
    
    
    int index = 0;
    
    for(NSString *imageStr in _images){
        NSString *match = [[imageStr componentsSeparatedByString:@"/"] objectAtIndex:1];
        NSRange rangeOfImage = [_tempic rangeOfString:match];
        
        double percentText = (double) rangeOfImage.location / (double)htmlLength;

        if(percentText > percentTouch){
            break;
        }
        index++;
        
    }
    
    NSLog(@"STOP");
    
}

@finish

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments