Load html content in UIWebView - iOS - html

I am loading html content in UIWebview (iPhone6) and the content has actionable buttons. Upon pressing one of the buttons a pop up shows up in the middle of the content (content is approximately 6-7 pages long). As a result, user is unable to see the popup as she is on the first screen. Earlier, I thought it was content's issue, but on android, the pop up comes on the middle of the first page.
-(void) loadScorm {
NSString *scormUrl = self.content.media.scormUrl;
scormUrl = [NSString stringWithFormat:#"%#%#", #"https:" , scormUrl];
NSURL *url = [NSURL URLWithString:scormUrl];
NSString *body;
body = //constructed body
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:request];
self.webView.frame = self.webViewContainer.frame;
}
I also read that javascript for UIWebView is enabled by default. What am I missing? Any pointers would be appreciated.
PS: Can't share the screenshot as the content is classified.

How you create your UIWebView via Code or IBOutlet.
If you are created your webView via IBOutlet then you have to check some webView property in Storyboard like
Then your webView will take Events.
And if you are created your UIWebView via code then write this code
[your_webView_Object setDataDetectorTypes: UIDataDetectorTypeAll];

[webview loadHTMLString: [NSString stringWithFormat:#"<div id ='foo' align='justify' style='font-size:22px; font-family:helvetica; color:#ffffff';>%#<div>",[jsonObject objectForKey:#"msg"]] baseURL:nil];
Hope you will find useful

Apparently, this was an issue from the side of content creator. The person didn't include the support of Safari and Chrome on iOS platform and hence, these issues.

Related

How to load localized html file from WKWebView?

1st StackOverflow question for me...
!! Objective-C !!
I'm in the process of updating an old app and am working on help screens.
I chose to display some useful How-tos through some html mainly for the ease of formatting the text (bullet points, colors, bold, underline etc...).
Since the app is a bit complex, The help screens are in a total of 3:
A main info webpage that links to 2 other detailed pages.
Tha app is localized in English (Base) and French.
Once the setup was complete, and with everything working fine, I went ahead and localized the base html files.
Now, they are neatly placed in the Base.lproj and fr.lproj files.
But when when I tap on a link to another page from the main WebView, nothing happens !
Any ideas ?
Thanks
Here are some bits of code I'm using:
In the .h file :
#interface howToViewController : UIViewController <WKUIDelegate>
{
WKWebView *infoText;
NSString *uRLString;
}
#property (nonatomic, retain) WKWebView *infoText;
In the .m file: (had to programmatically create the WKWebView because of the known Xcode 9 bug when creating it through Storyboards)
- (void)viewDidLoad
{
[super viewDidLoad];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *aWebView = [[WKWebView alloc] initWithFrame:self.view.frame
configuration:theConfiguration];
aWebView.UIDelegate = self;
self.infoText = aWebView;
NSURL *url = [[NSBundle mainBundle] URLForResource:#"infoHTML" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[infoText loadHTMLString:html baseURL:baseUrl];
[self.view addSubview:infoText];
}
In the html file, the links that no longer work once the files are localized:
<p>3/ More information is available for the respective tabs by pressing the following links :</p>
<ul><li>Limitations
</li></ul>
<ul><li>Fueling
</li></ul>
Working code from one of my apps. I'm using NSURLRequest and WKWebView's loadRequest instance method. I'm not sure what's going wrong with your use of loadHTMLString:baseURL: but I think it may have something to do with the value you're passing for the baseURL argument. Have you cleared the console, then single-stepped over that line of code to see if you're getting any message in the console?
NSURL *url = [[NSBundle mainBundle] URLForResource: pageString withExtension: #"html"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
[webView loadRequest: request];

I am trying to Load html string into a webview. But the webview shows a white blank colour on run time

I am trying to Load html string into a webview. But the webview shows a white blank colour on run time. added the UIWebViewDelegate and declare object like this.
#property (strong, nonatomic) IBOutlet UIWebView *aboutWebView;
but still having the same issue.
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
[_aboutWebView setDelegate:self];
[self loadWebView];
// Do any additional setup after loading the view.
}
-(void)loadWebView{
_aboutWebView= [[UIWebView alloc]init];
_aboutWebView.backgroundColor= [UIColor blueColor];
NSString *embedHTML= #"html Code";
[_aboutWebView loadHTMLString: embedHTML baseURL: nil];
}
Please remove the below line from your code.It works for you.
_aboutWebView= [[UIWebView alloc]init];
Set the web-view delegate after initialising it , and add this line of code [webView setScalesPageToFit:YES];
There is no need for allocating your webview again in loadWebView method of yours.
comment this line "_aboutWebView= [[UIWebView alloc]init]".
You are already creating an IBOutlet for your webview which automatically creates an object for view.
Have below in HTML String
<head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 0px;padding: 0px;}</style></head>
And for webview have this
yourWebView.backgroundColor = [UIColor clearColor];
yourWebView.opaque = NO;
This will not show white color in run time...
First at all,your HTML format is not correct,writing correct HTML to load on webView.
Then,add self.view.backgroundColor=[UIColor whiteColor]; in function - (void)viewDidLoad.
Hope to help you!

Open a Pdf in IOS App

I am making a IOS App and i want to open a PDF in div tag of my html file.I have found a code which works well on windows but it is not working in iOS app. In iOS app it just show the image of the first page.
Open the pdf in Webview and keep a button on top right corner (or anywhere) You want it.. And their you can write code for dismissing the WebView.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"documentName" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
And in Done Button Action Just remove the WebView

WKWebView Could not create a sandbox extension for '/'

I would like to use external file .html to use WKWebView. Or is there a solution without external file ? All is working good on simulator, but not on device...
Here is my code :
NSString *path = [[NSBundle mainBundle] pathForResource:#"myHTML" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
WKWebViewConfiguration *theConfiguration =
[[WKWebViewConfiguration alloc] init];
[theConfiguration.userContentController
addScriptMessageHandler:self name:#"interOp"];
_webView = [[WKWebView alloc] initWithFrame:self.view.frame
configuration:theConfiguration];
[_webView loadRequest:request];
[self.view addSubview:_webView];
I've read this topic : WKWebView not loading local files under iOS 8
I don't want to use a server. I'm using Objective-C, and I want that it works on only one view, not like the https://github.com/shazron/WKWebViewFIleUrlTest project with 2 views and button and I need to have connexion with internet in the page (read iFrame).
What have I to do ?
You have to copy all your web files to the app's NSTemporaryDirectory and call your index.html from there. Works fine on iOS 8.3 for me.

Unable to load html string in UIWebView using loadHTMLString:baseURL in iOS?

I am trying to embed youtube video into my iOS application.For that I have created a UIWebView & trying to load the Youtube video from following here
I have gone through the all the answers for the above problem. Even then its not working.
I have also tried loading very simple HTML
NSString *embedHTML =[NSString stringWithFormat:#"<html><body>Hello World</body></html>"];
[webView loadHTMLString:embedHTML baseURL:nil];
Even then, I am getting compile error Parse Issue Expecte ']'
I have tried cleaning, quitting the XCode & relaunching it again. I donno, I am not able to use that method. How to use the above loadHTMLString method for my UIWebView.
PS : Please do not tag this question as duplicate. I have tried all the solutions in Stackoverflow. Nothing has worked
WebView *webDesc = [[UIWebView alloc]initWithFrame:CGRectMake(12, 50, 276, 228)];
NSString *embedHTML = #"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";
webDesc.userInteractionEnabled = NO;
webDesc.opaque = NO;
webDesc.backgroundColor = [UIColor clearColor];
[webDesc loadHTMLString: embedHTML baseURL: nil];
- (NSString *)getHTMLContent
{
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"baseline" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssStyle = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *subtitle = [NSString stringWithFormat:#"%# | %#", self.article.author, [dateFormatter stringFromDate:self.article.publishedDate]];
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'></head><style type=\"text/css\">%#</style><body><div id=\"container\"><h1>%#</h1><p class='subtitle'>%#</p>%#</div></body></html>", cssStyle, self.article.title, subtitle, self.article.content];
return htmlString;
}
It is very simple. You just have to add only one line. Try It:
NSString *htmlString = #"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";
[WebView loadHTMLString: htmlString baseURL: nil];
You probably need to provide more code if you want people to help you. I just used webView in a similar way and it's working fine.
self.webView = [[UIWebView alloc] initWithFrame:myFrame];
self.webView.scalesPageToFit = YES;
[self.webView setBackgroundColor:[UIColor whiteColor]];
//pass the string to the webview
[self.webView loadHTMLString:[[self.itineraryArray objectAtIndex:indexPath.row] valueForKey:#"body"] baseURL:nil];
//add it to the subview
[self.view addSubview:self.webView];
Can you provide more information and code?
it doesnt seems like an issue on webview.Please do add the code where it breaks.Error says you missed a ] somewhere in the code
try to load the url directly into the webview :
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(100, 100, 200, 250)];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://youtube.com/embed/-0Xa4bHcJu8"]]];