Open UIWebView URL in Safari - iphone

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;

Related

How to show a UIWebView after app launch only after the page loads?

I am attempting to load a UIWebView only after the page loads. Until the page is ready, I only want the splash screen to display. I tried following the first answer on this page (XCode Prevent UIWebView flashing white screen after Launch Image) but it doesn't seem to work...the splash screen loads and the UIWebView never loads.
Any advice?
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webview;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webview;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://http://warm-chamber-7399.herokuapp.com/"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"Default.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Default.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this,
#interface ViewController () <UIWebViewDelegate>
{
UIWebView * webView;
UIImageView * imageView;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
imageview = [[UIImageView alloc]init];
[imageview setFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
[imageview setImage:[UIImage imageNamed:#"Red.png"]];
[self.view addSubview:imageview];
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
// UIWebView Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[imageView removeFromSuperview];
[self.view addSubview:webView];
}
#end
Have you set the delegate of webView? Without setting that, your webViewDidFinishLoad: method will never fire.
webView.delegate = self;
Why dont you directly add launch image to your .plist file

Open YouTube URL in Safari instead of UIWebView

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;
}
}

rootViewController addSubview, the keyboard doesn't work IOS 4 (working on iOS5), Why?

I have a problem with my app (ipad) on iOS4. The Keyboard doesn't work, doesn't display on my webview AFTER the first view (login view).
Appdelagate :
AppDelegate.h
#class RootViewController;
#interface StandardFacileAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;
//[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
return YES;
}
RootViewController :
- (void)viewDidLoad {
[super viewDidLoad];
[self loadViewController];
[self.view addSubview:viewController.view];
}
- (void)loadViewController {
ViewController *viewControllertemp = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.viewController = viewControllertemp;
[viewControllertemp release];}
viewController is my login view after request i call the method for change the view (my webview) :
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView{
UIViewController *currentView = (UIViewController*)fromView;
NSLog(#"currentView =>%#",currentView);
if(!ipadTheWebView)
[self loadipadTheWebView];
[currentView.view removeFromSuperview];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
[self.view addSubview:ipadTheWebView.view];
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[ipadTheWebView.webView loadRequest:requestURL];
My webview is ok but if i want search word in google, the keyboard doesn't display (the view scroll) when the textfield have the focus.
What is the problem, this code working on iOS5 but not ios4?
Thanks for your help ;)
Edit :
Thanks Mathieu,
RootViewController.h
#import <UIKit/UIKit.h>
#class ViewController;
#class IpadTheWebView;
#interface RootViewController : UIViewController {
ViewController *viewController;
IpadTheWebView *ipadTheWebView;
}
#property (nonatomic,retain) ViewController *viewController;
- (void)loadViewController;
- (void)changeViewToViewController:(UIViewController *)fromView;
#property (nonatomic,retain) IpadTheWebView *ipadTheWebView;
- (void)loadipadTheWebView;
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView;
- (void)keyboardWillShow:(NSNotification *)notification;
- (void)keyboardWillHide:(NSNotification *)note;
#end

TapDetectingWindow on WebView is not Working Properly

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.

Trouble getting webViewDidStartLoad and webViewDidFinishLoad to work

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