Search code examples
iosobjective-cuiimagepickercontrollerwkwebview

WKWebView behavior. Reloading after Image picker returns


I am getting a weird behavior of a WKWebview. Resuming the problem is that after the web content ask for a image, the app opens automatically an image picker controller, and after select the image, the WKWebview starts reloading the whole page and I lost my current state, so the image selection does not works.

The wkwebview is contained in a custom navigation controller and that navigation controller is contained in a ViewController.

This is a little part of code:

if(isNewProfile)
    _webvc = [[DJTWebViewVC alloc] initWithOutDataObject:_dataUser];
else
    _webvc = [[DJTWebViewVC alloc] initWithDataObject:_dataObject];

CGFloat WEBNAVOFFSET = [self setWebNavOffset];

_webnav = [[DJTWebNavVC alloc] initWithRootViewController:_webvc];
[_webnav.view setFrame:CGRectMake(0.0, 0.0, c_width, c_height-WEBNAVOFFSET)];

[self.view addSubview:_webnav.view];

The image picker is called from webvc that it is the wkwebview host. Here you can see that webnav contains webvc, and self.view contains webnav. I suppose this is the problem. Is there any way to set the view which the image controller should be fired? Or there is anybody facing this issue and has a workaround?


Solution

  • Because the problem is the way the web view is being called, the solution I used to make it work was like this. First define a global var to know if the web view is previously loaded, so initialize it this way:

    @property (nonatomic, assign) BOOL isPreviouslyLoaded;
    

    In the viewDidLoad function I added this:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        ...
        
        self.isPreviouslyLoaded = NO;
    }
    

    Then in the viewDidAppear function I used the full code of the question this way:

    - (void)viewDidAppear:(BOOL)animated
    {
        if(!self.isPreviouslyLoaded){
            [self loadWkWebNavigationController: YES];
            self.isPreviouslyLoaded = YES;
        }
    }
    

    Finally the code to load the web view:

    - (void)loadWkWebNavigationController:(BOOL)isNewProfile
    {
        if(isNewProfile)
            _webvc = [[DJTWebViewVC alloc] initWithOutDataObject:nil];
        else
            _webvc = [[DJTWebViewVC alloc] initWithDataObject:nil];
        
        CGFloat WEBNAVOFFSET = [self setWebNavOffset];
        
        _webnav = [[DJTWebNavVC alloc] initWithRootViewController:_webvc];
        [_webnav.view setFrame:CGRectMake(0.0, self.navigationController.navigationBar.bounds.size.height+WEBNAVOFFSET, self.view.bounds.size.width, self.view.bounds.size.height-self.navigationController.navigationBar.bounds.size.height-WEBNAVOFFSET)];
            
        [self.view addSubview:_webnav.view];
        
    }
    

    This is the way I do not reload the view if it is previously loaded.