Two UIWebViews, how to set the second webview so when links are clicked it sends them to the first webview to load? - iphone

This question may seem a little odd/weird, but I have two UIWebViews, one of which is set to hidden in the .xib file, and it's delegate set to my view controller. The 2nd UIWebView is used to parse and "cleanup" the html from the hidden UIWebView and display it as a "mobilized" page to the user. There is no delegate on the non-hidden UIWebView in my .xib file.
The problem I'm having is that when the 2nd UIWebView finishes loading, the user taps on a link within the 2nd UIWebView, and it finishes loading the non parsed webpage because it didn't send this link click back to the hidden UIWebView to be loaded so that the 2nd UIWebView can parse it when it finishes loading. My two UIWebView's are properties in my .m file.
Is there any way I can solve this? I appreciate any responses offered! If i haven't stated my question clearly, please let me know!
Some code:
.h:
#import <UIKit/UIKit.h>
#interface Web : UIViewController <UIWebViewDelegate>
#property (nonatomic, strong) NSURL *url;
#end
.m:
#import "Web.h"
#interface Web()
#property (nonatomic, strong) IBOutlet UIWebView *webView2;
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#property (nonatomic, strong) NSMutableString *output;
#property (nonatomic, strong) NSString *wrapper;
#end
#implementation Web
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if(webView == self.webView2)
{
[self.webView loadRequest:(NSURLRequest*)request];
}
else
{
[self.webView loadRequest:(NSURLRequest*)request];
}
return NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self cleanUp];
[self.webView2 loadHTMLString:self.output baseURL:nil];
}
- (void)cleanUp
{
NSString *input = [self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"ctl_ctl_cphBody_cphCenter_divHtml\").innerHTML;"];
NSString *breadcrumbString = [self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('breadcrumb')[0].outerHTML;"];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
self.wrapper = #"<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"/><meta name=\"viewport\" content=\"initial-scale = 1.37,maximum-scale = 2.0\"/></html>";
}
else
{
self.wrapper = #"<html><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"/><meta name=\"viewport\" content=\"initial-scale = 0.57,maximum-scale = 2.0\"/></html>";
}
self.output = [NSMutableString stringWithString:self.wrapper];
[self.output insertString:input atIndex:147];
[self.output replaceOccurrencesOfString:breadcrumbString withString:#"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [self.output length])];
}

You can implement the delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
This lets you intercept load requests and cancel them. You can then grab the URL from the request and hand it off to your hidden webview. The only caveat is, IIRC, this method will be called even for requests that you load yourself with -loadRequest:, so you'll want to check the navigationType argument.

You should be able to use - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType. Forward the request to the first webView and return NO to prevent the second webView from loading the page.

Related

adding html text to my UIWebView

I want to add text from my database to a UIWebView, but I have an error I can't deal with.
in my .h:
#import <UIKit/UIKit.h>
#interface readNotice : UIViewController {
IBOutlet UILabel *message;
IBOutlet UIWebView *body;
}
#property(nonatomic, retain)IBOutlet UILabel *message;
#property(nonatomic, retain)IBOutlet UIWebView *body;
-(void)getNoticeData:(NSString *)noticeID:(NSString *)noticeTitle;
#end
In the .m:
#synthesize message,body;
-(void)getNoticeData:(NSString *)noticeID:(NSString *)noticeTitle {
//some taks not useful for this code
NSString *bodyText = [dict objectForKey:#"introtext"];
[body loadHTMLString: bodyText];
}
The error I get:
No visible #interface for 'UIWebView' declares the selector 'loadHTMLString:'
why is this happening? Thank you in advance
try using like this
NSString *html = #"Should be halfI wish the answer were just 42";
[webView loadHTMLString:html baseURL:nil];
The correct call would be loadHTMLString:baseURL:

Passing variables to the view from AppDelegate

i open a view in AppDelegate
with:
AppDelegate
PushDetail *pushdtls = [[PushDetail alloc] initWithNibName:nil bundle:nil];
pushdtls.seminarurl = [NSURL URLWithString:resourcePathURL]; /passing URL
[self.window presentModalViewController:pushdtls animated:YES];
PushDetail.h
#property (nonatomic) NSURL *seminarurl;
PushDetail.m
- (void)viewDidLoad
{
NSURLRequest *requestObj = [NSURLRequest requestWithURL:seminarurl];
[pushDetails loadRequest:requestObj];
}
But the webview is empty... what I do wrong?
and the second question how to close the view I opened?
- (IBAction) closeNews{
//[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//[self release];
[self dismissModalViewControllerAnimated:YES];
}
does not work :(
You may implement the UIWebViewDelegate protocol within your view controller and fetch the results/error messages yourself.
Namely didFailLoadWithError - see the UIWebViewDelegate Protocol References for more.
Maybe its just a typo on your URL.
Example:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"oops, failed with error: %#", error);
}
You've not specified what pushDetails is, but I'm assuming it's an IBOutlet to a UIWebview on your PushDetail controller. My guess is that you've forgotten to bind your pushDetails outlet to the webview in your nib file.
You could create lets say a file that holds all your global variables lets call it GlobalVariables.h and .m. In the GlobalVariables.h file add this
extern NSURL *seminarurl;
#interface GlobalVariables
#property (retain, nonatomic) NSURL *seminarurl;
#end
and in the GlobalVariables.m file add
#import "GlobalVariables.h"
#implements GlobalVariables
#synthesize seminarurl;
NSURL *seminarurl; // You could add what your URL is here as well you would just use '='
#end
So when you want to access it or assign a variable to it, it would just be like accessing or assigning any other variable. Just remember to import 'GlobalVariables.h' into which ever .m file you are using it in.
make your property retained. ex >> #property (nonatomic,retain).
if the url is a property in your app delegate .. you can access it like this
[UIApplication SharedApplication].delegate.yourpropertyname;

Open Link in UIWebView in Safari

I am working on an app that receives a feed that I display in a UIWebView. The feed has embedded links that I want to open in Safari instead of thew WebView. I have looked over several other questions that are posted here. I am not sure what I am missing, but I think it is something simple. Here are my .h and .m files
#import
#class BlogRss;
#interface EnduranceDailyWorkoutViewController : UIViewController {
IBOutlet UIWebView * descriptionTextView;
BlogRss * currentlySelectedBlogItem;
}
#property (nonatomic, retain) UIWebView * descriptionTextView;
#property (readwrite, retain) BlogRss * currentlySelectedBlogItem;
#end
#import "EnduranceDailyWorkoutViewController.h"
#import "BlogRss.h"
#implementation EnduranceDailyWorkoutViewController
#synthesize descriptionTextView = descriptionTextView;
#synthesize currentlySelectedBlogItem = currentlySelectedBlogItem;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *html = [NSString stringWithFormat:#"%# %#",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}
-(BOOL)webView:(UIWebView *)descriptionTextView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
Using Interface Builder I have linked the IBOutlet and the UIWebView. Please let me know what I am missing. I have put break points in the webView section but the code never hits there so it is almost like something is not linked correctly in IB.
You need to ensure that the UIWebView's delegate is set to your controller. You can do this in interface builder, or you can modify your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// add this line to set the delegate of the UIWebView
descriptionTextView.delegate = self;
NSString *html = [NSString stringWithFormat:#"%# %#",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}

I can't get UIWebViewDelegate to work

I'm trying to implement a UIWebViewDelegate in my application and I cannot for the life of me get it to work. I would really appreciate a second set of eyes on this.
I know the name MapViewController is confusing, but the WebView is controlling a map (not UIMapView).
Here's the bulk of the code:
MapViewController.h
#import <UIKit/UIKit.h>
#import <UIKit/UIWebView.h>
#interface MapViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
MapViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"map" ofType:#"html"]isDirectory:NO]];
[webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"Done loading.");
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"hi");
return NO;
}
- (void)dealloc {
webView.delegate = nil;
[webView release]
[super dealloc];
}
Thanks for any help!
Does the initial page you're loading actually display? Are you sure you connected your webview in Interface Builder?
go interface builder , connect the delegate to the file's owner , then it should work.
Open Interface Builder
Right click on the Web View object
Drag the delegate onto files owner.
Things to check:
if map.html is in the bundle. Put it on the root of the bundle, not on resources.
if it is, right click on it, and choose get info. Verify on TARGETS if the target is checked for the file.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"hi");
return YES; // for all supported oreintation.
}

How to use protocol instead of delegate in iPhone

Scenario: Need to execute CustomWebview delegate on view controller.
Please help me with this code. Instead of using callback, I need to use "Protocol". Can it be done or we can only use callback in this scenario.
On ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//MyWebView *webView = [[MyWebView alloc] initWithDelegate:self callback:#selector(finishLoading)];
MyWebView *webView= [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320, 460)];
[webView LoadURL:#"http://192.168.5.165/"];
[webView setDelegate:self];
[webView setCallback:#selector(finishLoading)];
[self.view addSubview:webView] ;
}
- (void) finishLoading
{
NSLog(#"Finish");
}
On MyWebView.h
#interface MyWebView : UIView<UIWebViewDelegate> {
NSString *strURL;
}
#property(nonatomic, retain) NSString *strURL;
#property (nonatomic, assign) id delegate;
#property (nonatomic, assign) SEL callback;
-(void) LoadURL:(NSString*)url;
#end
On MyWebView.m
#import "MyWebView.h"
#implementation MyWebView
#synthesize strURL,delegate,callback;
UIWebView *webView;
-(id) initWithFrame:(CGRect)frame
{
if(self =[super initWithFrame:frame])
{
webView = [[UIWebView alloc] initWithFrame:frame];
webView.delegate = self;
[self addSubview:webView];
}
return self;
}
-(void) LoadURL:(NSString*)url
{
NSURL *u = [NSURL URLWithString:url];
NSURLRequest *req= [NSURLRequest requestWithURL:u];
[webView loadRequest:req];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[delegate performSelector:callback];
}
Your question is a bit unclear. A protocol and a delegate are two entirely separate, though related, things--apples and oranges. A protocol defines a list of methods an object may or must respond to:
#protocol Document
+ (id)documentWithContentsOfURL: (NSURL *)aURL;
- (void)writeToURL: (NSURL *)aURL error: (NSError **)outError;
#end
A delegate is an object, usually an instance of a custom class, that is handed to another object for custom processing or feedback--that latter object delegates work to the delegate object.
Are you asking how to convert a delegate category on NSObject to a delegate protocol? (The former used to be Apple's pattern for defining the obligations and abilities of a delegate; the latter is the newer way to do the same thing.) If so, it generally looks something like this:
Delegate Category on NSObject
#interface NSObject (WidgetDelegate)
- (void)doSomethingWithWidget: (Widget *)w;
#end
#interface Widget : NSObject
#property (readwrite, assign) id delegate;
#end
Delegate Protocol
#protocol WidgetDelegate <NSObject>
- (void)doSomethingWithWidget: (Widget *)w;
#end
#interface Widget : NSObject
#property (readwrite, assign) id <WidgetDelegate> delegate;
#end
Is that what you're looking for? If not, can you clarify exactly what you're trying to do?
Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors. Though its listed under Mac OS X, most (if not all) appears to apply to iOS also.
On ViewController.m
#interface MyViewController : UIViewController<MyFinishLoadingDelegate> {
//your own definition
}
- (void)viewDidLoad
{
[super viewDidLoad];
//MyWebView *webView = [[MyWebView alloc] initWithDelegate:self callback:#selector(finishLoading)];
MyWebView *webView= [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320, 460)];
[webView LoadURL:#"http://192.168.5.165/"];
[webView setDelegate:self];
[self.view addSubview:webView] ;
}
- (void)finishLoading //this is your implementation for MyFinishLoadingDelegate
{
NSLog(#"Finish");
}
On MyWebView.h
//you declare the protocol here to tell anyone who'd like to act like your delegate that they need to implement "finishLoading"
#protocol MyFinishLoadingDelegate <NSObject>
- (void)finishLoading;
#end
#interface MyWebView : UIView<UIWebViewDelegate> {
NSString *strURL;
}
#property(nonatomic, retain) NSString *strURL;
#property (nonatomic, assign) id<MyFinishLoadingDelegate> delegate;
-(void) LoadURL:(NSString*)url;
#end
On MyWebView.m
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[delegate finishLoading];
}