Open Link in UIWebview not UIShared Application - iphone

I have been having a rough time trying to open links that a user clicks in a simple web view instead of multitasking and going to safari. It is quite a pain for my users to have to leave the app every time a link is clicked and I know it is probably quite simple but still am having a terrible time making this happen. Here is the code I am using but still when the link is clicked it opens safari.
If anyone can help point me in the right direction I would be greatly appreciative! Thank you!
- (void) handleURL:(NSURL*)url
{
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"%#"]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest
*)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"expected:%d, got:%d", UIWebViewNavigationTypeLinkClicked, navigationType);
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[UIApplication sharedApplication] ;
return NO;
}
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[web loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:replyTweetText.text]]];
return YES;
}
}

I'm not sure what this bit does:
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[UIApplication sharedApplication] ;
return NO;
}
The line [UIApplication sharedApplication]; creates and returns the singleton application instance. You aren't doing anything with it.
Also, both if statements are identical, so only the first will ever be hit and the method returns NO. There is no default returned value, which is bad for a non-void function. Try this instead:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}

Related

ios - UIWebView DidFinishLoad not being called

I have a UIWebView that works ok. The delegate is working. When I open a URL, events shouldStartLoadWithRequest and webViewDidFinishLoad are fired.
But when for example I click on a Google search result, only shouldStartLoadWithRequest is fired, and webViewDidFinishLoad is never called.
This is causing problems because if I put a "loading..." indicator on shouldStartLoadWithRequest, in these cases the indicator still remains even after the page is correctly loaded.
Any idea why this is happening?
in .h file add <UIWebViewDelegate>
and in .m:
yourWebview=[[UIWebView alloc]init];
yourWebview.delegate=self;
When you click on search, the navigationType is UIWebViewNavigationTypeFormSubmitted. So in this case, the return type should be YES. If it is not, webViewDidFinishLoad method is not fired.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"shouldStartLoadWithRequest");
if ( navigationType == UIWebViewNavigationTypeLinkClicked ){
NSLog(#"UIWebViewNavigationTypeLinkClicked");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeFormSubmitted ) {
NSLog(#"UIWebViewNavigationTypeFormSubmitted");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeBackForward) {
NSLog(#"UIWebViewNavigationTypeBackForward");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeFormResubmitted) {
NSLog(#"UIWebViewNavigationTypeFormResubmitted");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeReload) {
NSLog(#"UIWebViewNavigationTypeReload");
return YES;
}
return YES;
}
This problem is usually found on ipad applications .... but you can solve your problem by using [self performselector:afterdelay] , you have to check in selector method whether webview has loaded or not using [m_pWebview loaded]; and according to that perform indicator stopping .... it's not the proper solution but it works .......
Not an expert on Google API, but as I suspected this might be your reason.

Need to open link in safari from uiwebview?

I have stored my whole html document with escape sequence in a variable...
and it contains some anchor tag.. where if that has clicked, it should open in safari, not in my app webview....
You can use following code for that.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
You can use the UIWebViewDelegate protocol to achieve this:
// assuming webView is a valid UIWebView
webView.delegate = self;
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq navigationType:(UIWebViewNavigationType)nt
{
if (nt == UIWebViewNavigationTypeLinkCkicked) {
[[UIApplication sharedApplication] openURL:[rq URL]];
return NO;
}
return YES;
}

Getting signals from uiwebview

Is there a possibility to get a signal from a uiwebview?
In my case, I display a webpage in the webview. When the user presses a button on the website, my App should (also) react to this.
Thanks for help
Yes you can do this. Implement
– webView:shouldStartLoadWithRequest:navigationType:
This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity etc.
Example - here you are trying to intercept any links clicked on your webpage & pass it through myMethodAction first.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
if(overrideLinksSwitch.on == TRUE)
{
[self myMethodAction];
[myWebView stopLoading];
return YES;
}
else
{
return YES;
}
}
return YES;
}
Hope this helps...
UIWebViewDelegate provide several methods. I had used one of them like this
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
// do stuff
return NO;
}
return YES;
}

How to launch links in UIWebView with the Safari app?

I have an small embedded UIWebView for my about-section of the app. When the user taps a link in there, the link opens in that small embedded UIWebView which of course has no navigation controls and isn't full-screen. I don't like that. Can I force it to open links with Safari instead?
You can implement the shouldStartLoadWithRequest method of the UIWebViewDelegate protocol to intercept link clicks. Inside that method you can use UIApplication's openURL method to cause the url to be opened in the appropriate application (ie. Safari for HTTP/HTTPS).
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([url isEqual:YOUR_HOME_URL_CONTSTANT])
return YES;
[[UIApplication sharedApplication] openURL:url];
return NO;
}
You should implement shouldStartLoadWithRequest of the UIWebViewDelegate. In here you can see when the UIWebView will start loading a URL. Then you can handle which should be loaded in this webView and which should be loaded by Safari.
My code looks like:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if (inType != UIWebViewNavigationTypeLinkClicked)
{
//This was not a clicked link, so is probably the initial load
//and should be loaded in the UIWebView
return YES;
}
//This was a clicked link, so load using Safari
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
Here I am using UIWebViewNavigationType to determine whether it was a link click, and responding appropriately.

UIWebView in UIView need links to open up in browser

So i have an app that is not so much a webapp but more uses UIViewControllers and UIViews for most screens. On one particular controller i have a UIWebView control that only occupies a small portion of the UIViewController screen real estate that i load html from a web service as a string.
[self.webView loadHTMLString:marketingBlurb baseURL:nil];
what i'm wondering is if the html links in this string can open up on the browser and not in the UIWebView in my app???? How can i do this?? Is this even possible ?
Set the delegate of the webview to self, and intercept all links using webView:shouldStartLoadWithRequest:navigationType:
You can then call [UIApplication openURL] to open Safari.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
I didn't test this code, but it will get you started.