UIWebView inside UICollectionViewCell does not call Delegate Method - ios

I am currently embedding a UIWebView inside a UICollectionViewCell to be able to view HTML formatted text residing in my local data structure.
I know it has been stated many times one should not embed an UIWebView inside a scrollView, but i currently see no other way to achieve displaying mixed content overviews (That is the current requirement, and so far UICollectionView does work very well with large data sets).
The Problem is, that i am not able to call the UIWebViews Delegate method:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType { ... }
I need the delegate method to be able to display external links or call local methods.
Is there a way to achieve this? Maybe by handing over the events programatically?

Thanks for the feedback!
I just found out what i did wrong:
I just needed to update the cells' contentView: [[cell contentView] setFrame:frameContainingWebViewSize]
That did the trick. The delegate method was not called because the contentView did not cover the webview (clipSubviews is set to NO) and thus the tap events where not fired.

Related

Tableview not reloading after button click

I am working in swift. I have a protocol defined that has a function called reload. I have a home class which is a tableviewcontroller where I extend the protocol and implement the reload function. I have another class where there is a button. On this click of the button I set the delegate and the reload function is called. Until this step it works fine. Now in the reload function i want to refresh the home page, so i tried
tableview.reload()
this is not working and the app is crashing, then i tried calling
viewDidLoad()
this is also not refreshing the page. I dont want to use NSNotification
what am i doing wrong?
Can anybody please help
Thanks in advance
Are you sure your UITableView data source delegate methods are working properly? You should look up viewDidLoad() and viewWillLoad(), etc... in the UIViewController reference page (Apple doc). If you're serious about app development I recommend at the very least making the time to spend 15 - 30 minutes each looking at the UIViewController and UIView pages to familiarize yourself with them, since you'll be using them all the time. You should know basically how they work. Also check out CALayers in UIViews. Then you'll be better established to write really nice apps.
you are setting a delegate on button touchup inside event, please check whether you are setting a delegate to your Home class .
I assume your mean tableview.reloadData() which is the correct method to call for reloading your tableview.
You shall not call viewDidLoad by yourself.

Ideal place to put a method after orientation has changed

I have an issue and here how it goes,
I have a view with a subview, the subview is loaded conditionally, only if the parent view is setHidden property is set to YES;
something like [parentView setHidden:YES] and if([parentView isHidden]),
I want to call a method when the orientation changes and that is the cited snippet above, but I have observed that the method shouldAutorotateToInterfaceOrientation is called 4 times during loading and 2 times during runtime, since the method is called more than once, how can I possibly implement a method call ideally since apple's existing method doesn't seem to give me the intuitiveness to put my custom method call with the existing method.
If I would hack this thing, it is possible, but somebody might have a better idea before resorting to things that in the future would just cause me more trouble than benefit.
TIA
Have you tried with
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
// check here for your desired rotation
}

Is is possible to communicate between UINavigationController and web page loaded inside UIWebView?

I am new to iOS development. I will try my best to explain my question, please pardon me if you find it silly.
I was wondering if one can load a subview in a UINavigationController by clicking a link inside a UIWebView.
Explaining it further, let's say there is UINavigationController that has a subview and that subview has a UIWebView called A, and there is another subview(that is not loaded yet) called B. Is is possible to load B (or perform any other native Objective C event) when user taps on a link inside the UIWebView A?
For example in Instagram app for iPhone, as I have observed (I may be wrong), there are UIWebViews in views controlled by UINavigationControllers. Taping a link inside a web view loads a new view. How is it happening? Can one set up the communication between the web page loaded inside the UIWebView and the parent/super UIView?.
Please correct me if I am lost. I am a n00b in iOS development so far. Also reference me the APIs or tutorials to do this if this makes any sense. Thanks in advance.
If you want just to handle a click-on-link inside UIWebView, just implement a delegate for the UIWebView. It has webView:shouldStartLoadWithRequest:navigationType: , which is called any time any content is attempted to be loaded in UIWebView. In the implementation of this method, you can block loading of the HTML content and perform any obj-c code, you want.
Hope that helps!

A class like UITextView, but with the ability to handle URL clicks

I am wondering if there is something already like this out there. But I basically want something like UITextView to display text with embedded links. However, I want to be able to handle the URL clicks as a delegate.
I've read a few posts like the following:
How to intercept click on link in UITextView?
However, I really don't want to override the openURL method. My app works with lots of webServer data, and I don't want to keep creating exceptions for different hosts in the openURL method.
I guess my questions is, is there another way to intercept the click on UITextView?
My alternative is to write my own, with a UIScrollView, and use a TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel) inside it. But am looking for suggestions, or alternatives.
Thanks.
You may use UIWebView + Local HTML instead of UITextView.
And use the -(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType delegate to handle the URL clicks,like:
-(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType
{
NSURL * clickedURL=[request URL];
//Do something here.
return NO;
}

"loadHTMLString:baseURL:" iPhone SDK

I'm coding up my firs iPhone app..
How can I use "loadHTMLString:baseURL:" to send the user to a view called "pagetwo.m"?
To navigate from one view to another or to insert subview given solution is correct and following is the sample code to let you know, how to use delegate method of UIWebView loadHTMLString:baseURL:...
NSString *embedHTML = [NSString stringWithFormat:#"<HTML><HEAD><TITLE>Page 1</TITLE></HEAD><BODY><H2>HTML Code Editor</H2><H3>p1 - The Basic Page</H3>"
"<P>This is the basic webpage. The HTML tags, BODY, H2, H4, P and A were used, but since no attributes were added to these tags, they are left at their defaults. The links below are shown using their default colors.</P>"
"Google.com<BR>Yahoo<BR></BODY></HTML>"];
[webView loadHTMLString:embedHTML baseURL:nil];
I assume pagetwo.m is a UIViewController subclass, right? You can't send a user to view with loadHTMLString:baseURL: since that's a UIWebView's method to load html page into webview. You will need something like:
[self presentModalViewController:pagetwo animated:YES];
Or if it's a subclass of UIView, you need:
[self.view addSubview:pagetwo];
But before that you need to alloc/init pagetwo (and release it later) which will be hard to do since your class is also named "pagetwo" and you instance variable can't be called the same. you could call it (the instance variable) pagetwoview or something, but preferred way would be to follow Objective-C naming conventions and always name your classes starting with a capital letter.

Resources