Displaying a Wiki Page in a UIWebView in a UIPopoverController

I am trying to open a webpage of a mobile version of a wiki using a UIWebView in a UIPopoverController. the problem is that it doesn't matter how I set my contentSizeForViewInPopover, or just set the UIWebView frame, or just set UIWebView.scalesPageToFit = YES. The page size of the mobile version of the Wiki seems to be larger than my UIWebView. But if I use it on an iPhone, there is no such problem. here is my code for the popover controller:

//create a UIWebView UIViewController first
WikiViewController *addView = [[WikiViewController alloc] init];
addView.contentSizeForViewInPopover = CGSizeMake(320.0, 480.0f);

//then create my UIPopoverController
popover = [[UIPopoverController alloc] initWithContentViewController:addView];
popover.delegate = self;
[addView release];

//then get the popover rect
CGPoint pointforPop = [self.mapView convertCoordinate:selectAnnotationCord 
                                        toPointToView:self.mapView];
CGRect askRect = CGRectMake((int)pointforPop.x, (int)pointforPop.y+10, 1.0, 1.0);
[popover presentPopoverFromRect:askRect 
                         inView:self.mapView 
       permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[self.mapView deselectAnnotation:annotation animated:YES];

and this is my code when creating a UIWebView:

- (void)viewDidLoad
{
 wikiWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
 wikiWebView.scalesPageToFit = YES;
 //or No, doesn't matter, it all get larger than this
 wikiWebView.delegate = self;
 self.view = wikiWebView;
}

the whole code seems typical ... I wonder if anyone can shed some light, thank you very much.

+2
source share
3 answers

auco, , viewport , :

- (void)webViewDidFinishLoad:(UIWebView*)webView
{
    int webviewWidth = (NSUInteger)webView.frame.size.width;

    if (!webView.loading) {

        NSString *jsCmd = [NSString stringWithFormat:@"try {var viewport = document.querySelector('meta[name=viewport]');if (viewport != null) {viewport.setAttribute('content','width=%ipx, initial-scale=1.0, user-scalable=1');} else {var viewPortTag=document.createElement('meta');viewPortTag.id='viewport';viewPortTag.name = 'viewport';viewPortTag.content = 'width=%ipx, initial-scale=1.0, user-scalable=1';document.getElementsByTagName('head')[0].appendChild(viewPortTag);}} catch (e) {/*alert(e);*/}", webviewWidth, webviewWidth];

        [webView stringByEvaluatingJavaScriptFromString:jsCmd];
    }
}

Javascript , WebView 320

try {
    var viewport = document.querySelector('meta[name=viewport]');
    if (viewport != null) {
        viewport.setAttribute('content',
                'width=320px, initial-scale=1.0, user-scalable=1');
    } else {
        var viewPortTag = document.createElement('meta');
        viewPortTag.id = 'viewport';
        viewPortTag.name = 'viewport';
        viewPortTag.content = 'width=320px,initial-scale=1.0, user-scalable=1';
        document.getElementsByTagName('head')[0].appendChild(viewPortTag);
    }
} catch (e) {
    /*alert(e);*/
} 

try/catch, .

+3

oh, QA, , html "width = device-width", webview popover, popover , . jQuery, jQuery. html wiki. -, .

webViewdidload delegate, URL html NSString, NSString " " html , NSString, NSString. .

- (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;
    }
}
}

- .

, wiki, html . , .

+2

JavaScript, html , html.

( , ):

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    if(aWebView.frame.size.width < aWebView.window.frame.size.width) {
        // width=device-width results in a wrong viewport dimension for webpages displayed in a popover
        NSString *jsCmd = @"var viewport = document.querySelector('meta[name=viewport]');";
        jsCmd = [jsCmd stringByAppendingFormat:@"viewport.setAttribute('content', 'width=%i, initial-scale=1.0, user-scalable=1');", (NSUInteger)aWebView.frame.size.width];
        [aWebView stringByEvaluatingJavaScriptFromString:jsCmd];
    }
    // stop network indicator
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
+2
source

Source: https://habr.com/ru/post/1542677/


All Articles