displaying Wiki mobile page in UIWebView within UIPopoverController

前端 未结 3 1712
孤独总比滥情好
孤独总比滥情好 2020-12-18 15:34

I try to open wiki mobile version webpage by a UIWebView within a UIPopoverController. the problem is, not matter how I set my contentSizeForViewInPopover, or just UIWebView

3条回答
  •  难免孤独
    2020-12-18 15:44

    oh, i found in another QA that sometimes if html got a line "width=device-width", and you load a webview from popover controller, this popover controller will automatically send out device-width, not the view width you specified, and make your view ugly and funky. in that post it is a jQuery issue, and it solved with a jQuery way. In my problem, it is just a html issue in wiki mobile version. so I try another way, but similar.

    I simple add a code in webViewdidload delegate method, first get URL html into a NSString, then use NSString instance method to search for "device-width" in loaded html, and replace it with my view width to make it a new NSString, then load this page with this new NSString. that's it.

    - (void) webViewDidFinishLoad:(UIWebView *)webView
    {
    if (!alreadyReload) 
    {
        NSString *webHTML = [NSString stringWithContentsOfURL:webView.request.URL encoding:NSUTF8StringEncoding error:NULL];
        NSRange range = [webHTML rangeOfString:@"device-width"];
        if ((range.location!=NSNotFound)&&(range.length != 0)) 
        {
            webHTML = [webHTML stringByReplacingOccurrencesOfString:@"device-width" withString:@"whatever width you need" options:0 range:range];
            [webView loadHTMLString:webHTML baseURL:wikiWebView.request.URL];
            alreadyReload = YES;
        }
    }
    }
    

    something like this.

    by the way, since I only use this on wiki mobile version, the html is simple and this kind of compare and replace is pretty easy. if you wanna use it in a more general case, you might use other way.

提交回复
热议问题