In my iPhone app an UITextView is containing an URL. I want to open this URL in an UIWebView instead of opening it into safari? My UITextView contains some data along with an URL. In some cases the no. of URLs can be more than one.
Thanks Sandy
Android is powered by Chrome. Mobile Safari UIWebView. The UIWebView is different from the ordinary Safari browser, as it is not a stand-alone browser, but merely browser functionality that is embedded in a third party app that allows the app to display content from the web.
A view that embeds web content in your app.
You can follow these steps:
UITextView
taken from Xib or Storyboard.OR write these for textview taken dynamically.
textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
delegate
method :-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { NSLog(@"URL: %@", URL); //You can do anything with the URL here (like open in other web view). return NO; }
I think you are searching for that.
The UITextView has the capability to detect URLs and embed hyperlinks accordingly. You can turn that option on in:
myTextView.dataDetectorTypes = UIDataDetectorTypeLink;
Then you need to configure your app to trap this URL request and let your application handle it. I published a boilerplate class on github that does this, which might be the easiest route: http://github.com/nbuggia/Browser-View-Controller--iPhone-.
The first step is to sub-class UIApplication so you can override who gets to take action on the 'openUrl' request. Here's what that class might look like:
#import <UIKit/UIKit.h>
#import "MyAppDelegate.h"
@interface MyApplication : UIApplication
-(BOOL)openURL:(NSURL *)url;
@end
@implementation MyApplication
-(BOOL)openURL:(NSURL *)url
{
BOOL couldWeOpenUrl = NO;
NSString* scheme = [url.scheme lowercaseString];
if([scheme compare:@"http"] == NSOrderedSame
|| [scheme compare:@"https"] == NSOrderedSame)
{
// TODO - Update the cast below with the name of your AppDelegate
couldWeOpenUrl = [(MyAppDelegate*)self.delegate openURL:url];
}
if(!couldWeOpenUrl)
{
return [super openURL:url];
}
else
{
return YES;
}
}
@end
Next, you need to update main.m to specify MyApplication.h
as being the bonified delegate for your UIApplication class. Open main.m and change this line:
int retVal = UIApplicationMain(argc, argv, nil, nil);
to this
int retVal = UIApplicationMain(argc, argv, @"MyApplication", nil);
Finally, you need to implement the [(MyAppDelegate*) openURL:url] method to have it do what ever you would like with the URL. Like maybe open up a new view controller with a UIWebView in it, and show the URL. You could do something like this:
- (BOOL)openURL:(NSURL*)url
{
BrowserViewController *bvc = [[BrowserViewController alloc] initWithUrls:url];
[self.navigationController pushViewController:bvc animated:YES];
[bvc release];
return YES;
}
Hopefully that should work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With