Im building an app with multiple uiwebviews. I wanted to add loading screens to make the app easier to use. I've added one for one of my webviews using this code.
- (void)webViewDidStartLoad:(UIWebView *)vieWeb
{
_loadingmain.hidden=NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)viewWeb
{
_loadingmain.hidden=YES;
}
viewWeb is the name of one of my uiwebviews. How would I add a load scree for my other two webviews? adding identical code with a different name for the webview causes the error "duplicate declaration of method webViewDidFinishLoad". Any help would be greatly appreciated. Thanks!
So you have multiple UIWebViews on one page, right? If so, you have a few options:
1) Create a subclass of UIWebView and set itself as the web view delegate, that way each UIWebView subclass handles it's own loading view.
2) You could also create multiple instances of your loading views and do something like
-(void)webViewDidStartLoad:(UIWebView *)webView
{
if( webView == self.webView1 )
{
[self.webView1 addSubview:self.loadingView1];
}
else if( webView == self.webView2 )
{
[self.webView2 addSubview:self.loadingView2];
}
//repeat for other webviews
}
-(void) webViewDidFinishLoad:(UIWebView *)webView
{
if( webView == self.webView1 )
{
[self.loadingView1 removeFromSuperview];
}
else if( webView == self.webView2 )
{
[self.loadingView2 removeFromSuperview];
}
//repeat for other webviews
}
Related
I've implemented a view controller which is displaying a WKWebView. The web view displays certain pages containing links with a tags, e. g.:
<a id="dien10000513721" href="/ge/tr/issue.html" class="dien">Issue</a>
I would like to add a preview to these links by activating 3d-Touch in the web view, but the delegate method webView:shouldPreviewElement: is never called, when I run my app on a iPhone 7 with iOS 11 and doing a force touch on that link.
When I've created my web view and set the necessary properties with
theWebView.navigationDelegate = self;
theWebView.UIDelegate = self;
theWebView.allowsLinkPreview = YES; // Setting this to NO doesn't help
self.webView = theWebView;
The three delegate methods for previewing are implemented as dummies:
- (BOOL)webView:(WKWebView *)inWebView shouldPreviewElement:(WKPreviewElementInfo *)inElementInfo {
NSLog(#"url = %#", inElementInfo.linkURL);
return NO;
}
- (UIViewController *)webView:(WKWebView *)inWebView previewingViewControllerForElement:(WKPreviewElementInfo *)inElementInfo defaultActions:(NSArray<id<WKPreviewActionItem>> *)inPreviewActions {
return nil;
}
- (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController {
NSLog(#"");
}
Do I still have to perform any important configuration steps? What else could prevent the execution of 3D touch events?
As mentioned in the title, I'm looking out for the way to detect all of the UIWebViews has finished load that are loading HTML string simultaneously.
I have one custom view in which there are 5 web views (1 for question and 4 for options). Each of them loading html string as :
[queWV loadHTMLString:queHtml baseURL:nil];
[opt1WV loadHTMLString:opt1Html baseURL:nil];
[opt2WV loadHTMLString:opt2Html baseURL:nil];
[opt3WV loadHTMLString:opt3Html baseURL:nil];
[opt4WV loadHTMLString:opt4Html baseURL:nil];
I've tried checking this with webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(webView == opt4WV)
{
//All web views has finished loading
}
}
But practically, its incorrect as the webViewDidFinishLoad method is getting called for random web views, depending on contents to load.
Is there any way to detect load finish for all the web views ?
Thanks !
Maybe I'm oversimplifying or not seeing something, but did you try to use webViewDidFinishLoad in a combination with the counter? So if you have 5 web views, implement a counter from 1 to 5 and count how many times does webViewDidFinishLoad get's called. When you get to 5 all views are loaded.
yes, use a counter to keep track and make sure all web views delegate are set to the same controller
May be helpful
take a NSInteger counter; globally
in viewDidLoad initialise counter = 0;
then
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
counter++;
if(counter == 5)
{
//All web views has finished loading
counter = 0; //again reset to 0
}
}
Happy coding...
in .h file create one counter like this
int webViewLoadCount = 0;
then trke this counter like this way
in .m file UIWebview delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = webView.request.URL.absoluteString;
if ([string rangeOfString:queHtml]) {
NSLog(#"queWV webview loaded with this %# ",queHtml);
webViewLoadCount = webViewLoadCount + 1;
} else if ([string rangeOfString:opt1Html]) {
NSLog(#"opt1WV webview loaded with this %# ",opt1Html);
webViewLoadCount = webViewLoadCount + 1;
} else if ([string rangeOfString:opt2Html]) {
NSLog(#"opt2WV webview loaded with this %# ",opt2Html);
webViewLoadCount = webViewLoadCount + 1;
}else if ([string rangeOfString:opt3Html]) {
NSLog(#"opt3WV webview loaded with this %# ",opt3Html);
webViewLoadCount = webViewLoadCount + 1;
}else if ([string rangeOfString:opt4Html]) {
NSLog(#"opt4WV webview loaded with this %# ",opt4Html);
webViewLoadCount = webViewLoadCount + 1;
}
}
i hope this will help you
declare the variable, "loaded" out of this function
int loaded = 0
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(webView == queWV || webView == opt1WV || webView == opt2WV || webView == opt3WV || webView == opt4WV )
{
loaded += 1;
}
if (loaded ==5) {
// process
}
}
i have create a custom UIWebView for my project.now i load one website that UIWebView and also create two button like previous page and next page on top of navigation bar. i want to know how to handle the UIWebView pages,when i click the previous or next button.Those buttons handle the UIWebView pages.i need exact code for the those previous and next buttons.could any one help please. thanks in advance
Try this code to goForward and GoBack.
- (IBAction) buttonGoBack
{
if ([webView canGoBack])
{
[webView goBack];
}
}
- (IBAction) buttonGoForward
{
if ([webView canGoForward])
{
[webView goForward];
}
}
I was wondering if anyone could show me what code I need to implement to make my webview present options when viewing a document. Currently now I have a simple View controller with a webView taking up the entire view. It works fine and can display the documents but thats it. Shown below is an image from the safari iOS App that has the functionality I'm looking for.
Image Link: http://imgur.com/Nt1qIBB/
(Because I don't have enough reputation to upload image.. : /
Any help will be greatly appreciated. The tutorials I come across only show how to make the UIWebView work and it stops just about there.
-when you top long time on you web view and show "open in..." button on the top of web view.
click it then show like this:
- (void)SetmyParentViewController:(id)pViewController
{
if (nil != pmyParentViewController) {
}
self.pmyParentViewController = (UIViewController *)pViewController;
}
- (BOOL)SelectAppToOpenFile:(NSURL *)aUrl;
{
if (nil == pDocInteractCtrl) {
self.pDocInteractCtrl = [UIDocumentInteractionController interactionControllerWithURL:aUrl];
pDocInteractCtrl.delegate = (id)self.pmyParentViewController;
} else {
[pDocInteractCtrl setURL:aUrl];
}
if ([pDocInteractCtrl presentOpenInMenuFromRect:viewRect inView:pmyParentViewController.view animated:YES]) {
return YES;
}
else {
return NO;
}
}
I'm building an iOS app.
I have HTML content in a UIWebView with hyperlinks, and I'm unable to open a link to another UIWebView.
I used UIWebView as a subview of ViewController. Here is the code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked: { //this is when a user tap is detected
//write the handling code here.
isWebViewLoaded = false; //set this to false only if you open another view controller.
return NO; //prevent tapped URL from loading inside the UIWebView.
}
// some other typical parameters within a UIWebView. Use what is needed
case UIWebViewNavigationTypeFormResubmitted: return YES;
case UIWebViewNavigationTypeReload: return YES;
//for all other cases, including UIWebViewNavigationTypeOther called when UIWebView is loading for the first time
default: {
if (!isWebViewLoaded) {
isWebViewLoaded = true;
return YES;
}
else
return NO;
}
}
}
You only need to segue to a new controller with a web view, and pass the request to it. So something like this in your first case,
case UIWebViewNavigationTypeLinkClicked: { //this is when a user tap is detected
[self performSegueWithIdentifier:#"Next" sender:request];
isWebViewLoaded = false; //set this to false only if you open another view controller.
return NO; //prevent tapped URL from loading inside the UIWebView.
}
Notice that the sender argument in performSegueWithIdentifier:sender: is the request returned by the delegate method. Pass this request to a property in the view controller you're segueing to,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSURLRequest *)sender {
NextViewController *next = segue.destinationViewController;
next.request = sender;
}
Finally, use that request to load the web view in NextViewController.