In my app I am using TapDetectingWindow to detect touches on a UIWebView (exactly as detailed on http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way).
I used it in one View Controller as suggested with a reference in the header file and the following in the implementation file like
- (id)init {
self = [super init];
if (self) {
tapDetectingWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]; //mWindow, variable reference to TapDetectingWindow
tapDetectingWindow.viewToObserve = webView;
tapDetectingWindow.controllerThatObserves = self;
webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 340, 1900)];
[webView setTag:1];
[webView addSubview:keyboardText];
[webView setUserInteractionEnabled:NO];
[webView setDelegate:self];
[webView setOpaque:0.0];
[webView loadHTMLString:htmlString baseURL:NULL];
[self.view addSubview:webView];
keyboardText = [[UITextField alloc] init];
keyboardText.autocorrectionType = UITextAutocorrectionTypeNo;
[keyboardText setDelegate:self];
[self.view addSubview:keyboardText];
}
return self;
}
but my app is crashing at
"tapDetectingWindow.viewToObserve = webView "
with a report * -[UIWindow setViewToObserve:]: unrecognized selector sent to instance 0x4a26b90
Can Any one help me out ...
The problem is not in the above code. Its due to new property introduction in iOS 5.0 with name window under UIApplicationDelegate protocol.
Change your window name in appdelegate file.
In .h file
#class WebViewController;
#class TapDetectingWindow;
#interface test_touchAppDelegate : NSObject <UIApplicationDelegate>
{
TapDetectingWindow *myWindow;
WebViewController *viewController;
}
#property (retain, nonatomic) TapDetectingWindow *myWindow;
#property (retain, nonatomic) WebViewController *viewController;
#end
In .m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myWindow = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[WebViewController alloc] init];
self.myWindow.rootViewController = self.viewController;
[self.myWindow makeKeyAndVisible];
return YES;
}
Hope this will help.
what Apurv told you is correct and work like a charm. I see you ask for alternate method to recognise gestures on a webview (I assume that it means click on UIWebView.). The answer is yes, you have to use " UIWebView delegate method ".
#pragma mark -
#pragma mark uiwebview delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked){
NSURL *URL = [request URL];
if(URL){
[[UIApplication sharedApplication] openURL:URL];
return NO;
}
}
return YES;
}
The code above, when you click on UIWebView, it will open the link of UIWebView that you clicked. Good luck.
Related
So I'm having a little difficulty setting this up. I'm using this: UIWebView open links in Safari for the code but I can't seem to get it to work.
Here's my .h file:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
{
}
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
And here's my .m file:
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize webView = _webView;
- (void)viewDidLoad
{
[super viewDidLoad];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http:m.youtube.com/user/haatfilms/uploads"]]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
_webView.scrollView.bounces = NO;
_webView.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
#end
And I think I've connected the UIWebView delegate:
But still, when I click on a link, it still loads in the UIWebView and not in Safari.
Can anyone shed some light as to why it won't work? Thank you so much
Found the problem. Instead of using IB to connect my webView delegate, just do this:
[super viewDidLoad];
webView.delegate = self;
Im using the UIWebView to access a mobile website from within my iPhone application. I need help adding the forward and back navigation buttons. Im really new to this so if someone could provide some good details I would really appreciate your help.
Here is what I have:
UIWebView *webView1 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 480, 251)];
[contentView addSubview:webView1];
webView1.alpha = 1.0;
webView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.logsitall.com/m"]]];
webView1.scalesPageToFit = YES;
[webView1 release];
contentView.frame = self.view.bounds;
[self.view addSubview:contentView];
[contentView release];
}
- (void)viewDidUnload {
[self setWebView:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
}
#end
Final code:
//header
#interface MywebsiteViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *aWebView;
}
#property (nonatomic, retain) UIWebView *aWebView;
#end
//implementation
- (void)viewWillAppear:(BOOL)animated {
self.aWebView.delegate = self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://www.WebSiteToLoad.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[aWebView loadRequest:myRequest];
}
#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
//show indicator while loading website
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//turn off indicator and display the name of the website in the navBar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.title = [webView stringByEvaluatingJavaScriptFromString:#"document.title"];
}
There are a lot of UIWebView tutorials on.....wait for it....The Web! That said, as I recall, it's as simple as calling goForward or goBack on your WebView:
[[self mainWebView] goForward];
I need to create a rich text editor (for text alignment, fonts, bold, italics, underlining etc) for an iPhone app. I have heard of storing the data as HTML and rendering it in a UIWebView. I want to allow the user to edit the data and in my app I am using TapDetectingWindow to detect touches on a UIWebView (exactly as detailed here).
- (id)init {
self = [super init];
if (self) {
tapDetectingWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]; //mWindow, variable reference to TapDetectingWindow
tapDetectingWindow.viewToObserve = webView;
tapDetectingWindow.controllerThatObserves = self;
webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 340, 1900)];
[webView setTag:1];
[webView addSubview:keyboardText];
[webView setDelegate:self];
[webView setOpaque:0.0];
[webView loadHTMLString:htmlString baseURL:NULL];
[self.view addSubview:webView];
keyboardText = [[UITextField alloc] init];
keyboardText.autocorrectionType = UITextAutocorrectionTypeNo;
[keyboardText setDelegate:self];
[self.view addSubview:keyboardText];
}
return self;
}
But my app is crashing, with the message
tapDetectingWindow.viewToObserve = webView
and a report
* -[UIWindow setViewToObserve:]: unrecognized selector sent to instance 0x4a26b90
After a few hours of hair pulling, here's the answer: you need to edit your AppDelegate's .m file to get the TapDetectingWindow to work properly:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController* vc = self.window.rootViewController;
self.window = [[TapDetectingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = vc;
return YES;
}
I tried this and it's working perfectly with the rest of the TapDetectingWindow tutorial!
The web view loads the page. It doesn't call the webViewDidStartLoad or webViewDidFinishLoad methods.
N_NumberAppDelegate.h:
#interface N_NumberAppDelegate : UIViewController <UIWebViewDelegate> {
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
N_NumberAppDelegate.m:
#implementation N_NumberAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://registry.faa.gov/aircraftinquiry/NNum_Results.aspx?NNumbertxt=2N"]]];
[window addSubview:webView];
[window makeKeyAndVisible];
[window release];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"Started loading");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"Finshed loading");
}
i think you have forgot to set your delegate.
ie webView.delegate=self;
good luck
Is it possible to access iphone's camera from uiwebview? I know phonegap does it, but how to make it work without phonegap?
Thanks.
You can implement the UIWebView Delegate and intercept any attempted page load. By setting a custom url, you can pass a message to Objective C, that can launch the camera. To send data back to the web weiw you can initiate a new load (as I do in my example), or pass in some javascript using another method on UIWebView.
Here is a working example, I just wrote:
#import "WebViewCamAppDelegate.h"
#import <UIKit/UIKit.h>
#interface uiwebviewcameraAppDelegate : NSObject <UIApplicationDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIWebViewDelegate> {
UIWindow *window;
UIViewController *viewController;
UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
#implementation uiwebviewcameraAppDelegate
#synthesize window;
//This is the HTML we initially show in the WebView. Note the url "showcamera:" is one I
//invented with the intent to intercept it to show the camera.
static NSString *htmlString = #"<br>Show Camera";
//Pretty Basic stuff. We set the UIWebView Delegate so we can intercept the call and set up //a ViewController so we can animate the UIImagePicker
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[UIViewController alloc] init];
webView = [[UIWebView alloc] initWithFrame:window.bounds];
[webView loadHTMLString:htmlString baseURL:nil];
webView.delegate = self;
[viewController.view addSubview:webView];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
//Note: I check to make sure the camera is available. If it is not (iPod touch or Simulator) I show the photo library instead.
-(void) showCamera {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.delegate = self;
[viewController presentModalViewController:imagePicker animated:YES];
}
//Here we intercept any time the webview tries to load a document. When the user hits our "showcamera: url" we go to work.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqualToString:#"showcamera"]) {
[self showCamera];
return NO;
}
return YES;
}
//After the imagepicker has done it's thing, we pass the data back to the webview to be displayed.
//If we wanted to be fancy here, we could have done this via Javascript so we could dynamically insert an image without reloading the page.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[viewController dismissModalViewControllerAnimated:YES];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation (image, 0.5);
[webView loadData:imageData MIMEType:#"image/jpeg" textEncodingName:#"UTF-8" baseURL:nil];
}
- (void)dealloc {
[viewController release];
[webView release];
[window release];
[super dealloc];
}
#end