I'm trying to load any URL, after the initial load, in Safari. With any other webpage, this code works fine:
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL* url = [request URL];
if (UIWebViewNavigationTypeLinkClicked == navigationType)
{
[[UIApplication sharedApplication] openURL:url];
return NO;
}
return YES;
}
But as soon as I head over to m.youtube.com, the code no longer does anything, and YouTube continues to load inside the UIWebView. I can't find anything about this online. Any help?
Here is my entire .m file:
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize topLayer = _topLayer;
#synthesize layerPosition = _layerPosition;
#synthesize webView = webView;
#define VIEW_HIDDEN 260
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.youtube.com"]]];
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
webView.scrollView.bounces = NO;
webView.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[self performSelector:#selector(makeTick)];
self->promoteImages.contentSize = CGSizeMake(1920, 101);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Scroll3.png"]];
[self->promoteImages addSubview:imageView];
[promoteImages setShowsHorizontalScrollIndicator:NO];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
#end
And here's my .h file for the View Controller:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <Social/Social.h>
#interface SecondViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIScrollView *promoteImages;
NSTimer *time;
IBOutlet UIWebView *webView;
}
#property (nonatomic, nonatomic) UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIView *topLayer;
#property (nonatomic) CGFloat layerPosition;
#property(nonatomic,readonly,getter=isDragging) BOOL dragging;
#end
Here's the code that I use:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
Here's your problem:
[[UIApplication sharedApplication] openURL:url];
Change to:
[[UIApplication sharedApplication] openURL:request.URL];
You can also specify only certain URLs to open in Safari, for example any URLS beginning with http://www.example.com/share as shown here:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ((navigationType == UIWebViewNavigationTypeLinkClicked) && ([currentURL hasPrefix:#"http://www.example.com/share"])) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
}
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];
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.
I'm having problems getting the current URL through a URL request and passing it on to the text field above my web view. I tried logging it and it returns NULL, and the text field remains blank.
This is the delegate I added to my implementation file:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSURL* url = [webView.request URL];
_webAddress.text = [url absoluteString];
}
And here is my WebViewController.h file:
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController {
IBOutlet UIWebView *_webView;
IBOutlet UITextField *_webAddress;
IBOutlet UIActivityIndicatorView *activityIndicator;
NSTimer *timer;
NSString *_webURL;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) IBOutlet UITextField *webAddress;
#property (nonatomic, retain) NSString *webURL;
- (IBAction) doneButton:(id)sender;
#end
And here is my WebViewController.m file:
#import "WebViewController.h"
#implementation WebViewController
#synthesize webView = _webView;
#synthesize webAddress = _webAddress;
#synthesize webURL = _webURL;
// Dismiss WebViewController
- (IBAction) doneButton:(id)sender {
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSURL* url = [webView.request URL];
_webAddress.text = [url absoluteString];
}
- (void)viewDidLoad
{
[_webView addSubview:activityIndicator];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webURL]]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)loading {
if (!_webView.loading)
[activityIndicator stopAnimating];
else
[activityIndicator startAnimating];
}
- (void)viewDidUnload
{
_webURL = nil;
_webView = nil;
_webAddress = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc
{
[_webURL release];
[_webView release];
[_webAddress release];
[super dealloc];
}
#end
Does anybody know what I am doing wrong here? Thank you.
Use the - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType delegate instead. This will give you the actual request.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006951-CH3-SW6
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
_webAddress.text = [url absoluteString];
}
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