We have TextView control with some website url, if we click the textview website url, it is automatically open the safari browser, how t开发者_如何转开发o open the this webpage within our application?
I subclass a webview and give it a method inside like this:
-(void)loadWebPageFaceBook{
NSURL *url = [ [ NSURL alloc ] initWithString: @"http://www.facebook.com" ];
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url];
tabBar.selectedItem = [tabBar.items objectAtIndex:1];
[self loadRequest:urlRequest];
[url release];
[urlRequest release];
}
the tabBar part is cos I have a tab controller at bottom with a couple hyperlinks, this is like a small sandboxed browser..
obviously you could be feeding the URL as a NSString (@"www.someWebsite.com") argument into the method call, modifying above to something like..
-(void)loadWebPage:(NSString)urlString{
NSURL *url = [ [ NSURL alloc ] initWithString: urlString ];
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url];
[self loadRequest:urlRequest];
[url release];
[urlRequest release];
}
and then call that from your view controller, something in the textfield should return...
if ([myTextFieldObject.text length > 0]){
NSString *urlEntry = myTextFieldObject.text;
[myWebViewObject loadWebPage:urlEntry];
the webView object will also need a method to deal with errors, when there is no network/internet connection, and when the URL is bad. hope that helps
精彩评论