Showing posts with label UIWebView loadHTMLString baseURL example. Show all posts
Showing posts with label UIWebView loadHTMLString baseURL example. Show all posts

Monday, June 3, 2013

UIWebView loadHTMLString baseURL example in Objective C (iOS).


UIWebView loadHTMLString baseURL

Sets the main page content and base URL.

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

Parameters of [UIWebView loadHTMLString baseURL]
string
The content for the main page.
baseURL
The base URL for the content.

UIWebView loadHTMLString baseURL example.
- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        CGRect oFrame = frame;
        CGPoint oPositionCoords = CGPointMake(0, 0);
        oFrame.origin = oPositionCoords;

        m_oDataPresentView = [[UIWebView alloc] initWithFrame:oFrame];
        [m_oDataPresentView loadHTMLString:@"<html><body style=\"background-color: #000000; color: #FFFFFF; font-family: Helvetica; font-size: 10pt; width: 300px; word-wrap: break-word;\"></body></html>" baseURL:nil];
        m_oDataPresentView.delegate = self;

        [self addSubview:m_oDataPresentView];
    }
    return self;
}

Example of [UIWebView loadHTMLString baseURL].
- (void) showWebView:(NSString *)htmlText{
    [webView setHidden:FALSE];
    [txtContent setHidden:TRUE];

    NSString * htmlString = [NSString stringWithFormat:@"<html><head><style type='text/css'>body {background-color:transparent; } p { color:white; font-family:Helvetica; font-size:14px; } a { color:white; }</style></head><body><p>%@</p></body></html>", htmlText];
    webView.delegate = self;

    NSLog(@"htmlstring  %@",htmlString);
   [webView loadHTMLString:htmlString baseURL:nil];
}
Under iOS5 I found that sometimes a UIWebView would not load its content if

<meta charset="utf-8"></meta>
(or other equivalent charset specification) was not present in the HTML.


UIWebView loadHTMLString baseURL example.
- (void)embedYouTube:(NSString*)urls frame:(CGRect)frame {

    urls=[urls stringByAppendingString:@"&autoplay=1"];
    NSString *htmlString =[NSString stringWithFormat: @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head> <body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"212\" height=\"212\"><param name=\"movie\" value=\"%@\">",urls];

    NSString *htm2=[NSString stringWithFormat:@"</param><param name=\"wmode\" value=\"transparent\"></param>    <embed src=\"%@\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"212\"></embed>    </object></div></body></html>",urls];
//    NSString* embedHTML = @"<html><head> </head><body style=\"margin:0\"> <iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" width=\"%d\" height=\"%d\" src=\"%@\" frameborder=\"0\" allowFullScreen></iframe> </body></html>"; 

    NSString* html = [NSString stringWithFormat:@"%@%@",htmlString,htm2];
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];

    if(version<5.0)
    {

        NSRange r=[urls rangeOfString:@"v="];
        NSRange ran=NSMakeRange(r.location+2, [urls length]-r.location-2);
        NSString *vid=[urls substringWithRange:ran];
    html=[NSString stringWithFormat:@"<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=%@&fs=0\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"300\"></embed>", vid];
    }
    //NSLog(html);
    if(webView == nil) { 
        webView = [[UIWebView alloc] initWithFrame:frame]; 
        [self.view addSubview:webView]; 
        [webView setDelegate:self];
    } 
    [webView loadHTMLString:html baseURL:nil]; 
}

End of UIWebView loadHTMLString baseURL example article.