How to Open textview url within our application? - iphone

We have TextView control with some website url, if we click the textview website url, it is automatically open the safari browser, how to 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

Related

IOS - UIWebview - Get the URL of the link clicked and edit the url

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
First I initialize the webview with the following url:
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
When I click to a link, for example www.website.com/contact.php
Then I edit the new url with www.website.com/contact.php*?id=12212323*
I need to keep the id parameter in all the urls.
The UIWebViewDelegateProtocol should suit your requirements.
In particular the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
this is a code to get the current URL
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSURL *currentURL = [requestObj URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
but to create a URL bar hidden or not is a lot of code, you can start from this tutorial on YouTube is simple and work very well, is possible give you other idea to make a good job ;)
Web Browser Tutorial
hope this help you

Navigating back from UIViewController causes crash

I am just using this piece of code in viewDidLoad method. It only opens the web page in UIWebView and that's it. But when I go back to the previous view It cause crash.
here is the code:
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[moreWeb loadRequest:requestObj];
Just stop loading like this:
-(void)viewWillDisappear:(BOOL)animated{
[moreWeb stopLoading];
moreWeb.delegate = nil;
}
it'll work fine.

UITabBar with UIWebView problem with switching views

I have an app which has UITabBar with 5 views, each attached to a different UIWebView. Each of the WebViews responds to:
webViewDidStartLoad:(UIWebView *)webView
webViewDidFinishLoad:(UIWebView *)webView
Those two are responsible for displaying a loading screen (separate image for each tab, toggled visible or not) and activity indicator.
It all works fine when the page is loaded to WebView. User taps a link on the page, loading image is displayed along with the activity indicator. When the page is loaded they both disappear and new website is presented.
The problem is when user taps on one of the TabBar items. App intercepts the event and launches a method in appropriate view, which is refreshing the page.
The problem: after the tap view changes immediately to the other WebView, however loading screen takes a long time to appear (from what I gather it only shows when the page passed to homeView starts loading) and I can't figure out why. Displaying loading items is the first thing the app should do after method is called.
Here is the code that calls a method from TabBar controller:
[hv performSelector:#selector(goToPage) withObject:nil afterDelay:0.0];
and here is the goToPage method:
- (void) goToPage
{
homeView.delegate = self;
self.showLoading;
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat: #"%#/seed.php", appURL]];
NSString *body = [NSString stringWithFormat: #"uid=%#", uID];
NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: data];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[request release];
NSString *seedString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSArray *seedArray = [seedString componentsSeparatedByString:#":::"];
NSString *eventNumber = [[seedArray objectAtIndex:0] retain];
NSString *passedSid = [[seedArray objectAtIndex:1] retain];
if (!seedString) {
// can't connect
NSLog(#"Can't connect.");
}else {
// connected
NSLog(#"Events: %#", eventNumber);
if(![evtNo isEqualToString:eventNumber]){
evtNo = eventNumber;
if(![evtNo isEqualToString:#"0"]){
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
}
}
NSURL *urlView = [NSURL URLWithString: [NSString stringWithFormat: #"%#/index.php", appURL]];
NSString *bodyView = [NSString stringWithFormat: #"sid=%#", passedSid];
NSData *dataView = [bodyView dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *requestView = [[NSMutableURLRequest alloc]initWithURL:urlView];
[requestView setHTTPMethod: #"POST"];
[requestView setHTTPBody: dataView];
[homeView loadRequest: requestView];
if([evtNo isEqualToString:#"0"]){
// clearing badge
[[[[[self tabBarController] viewControllers] objectAtIndex: 0] tabBarItem] setBadgeValue: nil];
}else{
[[[[[self tabBarController] viewControllers] objectAtIndex: 0] tabBarItem] setBadgeValue: evtNo];
}
}
The goal I have is to display the loading image the moment user taps the TabBar item and remain visible until page stops loading.
The solution to this issue was to launch the method goToPage without the delay, display loading screen in goToPage and move the time consuming bits into another method inside that class, launched using persormSelector.

Insert url in one view and then display it in uiwebview in new view

hay all,
I want to enter url in one view and then want to switch and display it in another view in uiwebView I am trying like this but its not working.
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webViewController.page loadHTMLString:urlAddress baseURL:url];
webViewController *sec=[[webViewController alloc] initWithNibName:#"webView" bundle:nil];
[self presentModalViewController:sec animated:YES];
can anybody help me, I am new to iphone World
Try this out,.....
I havent checked it but it will work
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
webViewController *sec=[[webViewController alloc] initWithNibName:#"webView" bundle:nil];
sec.webview=[[UIWebView alloc]initWithFrame:CGRectMake(0,0,320,480)];
sec.webview.delegate=sec;
[sec.webview loadHTMLString:urlAddress baseURL:url];
[self presentModalViewController:sec animated:YES];

Retrieve data from website database+how to

I have made a project and when people put a keyword into UITextfield and click the button then my app will retrieve the data from website database.
It is not working here is the code:
- (IBAction) btnClickMe_Clicked:(id)sender {
NSString *kw = s.text; // <-----THIS IS THE UITextField
NSURL *url = [NSURL URLWithString:#"http://www.example.com/index.php?keyword=",kw];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[iMessageLabel loadRequest:request];
}
Any one could help me?
You aren't putting the keyword into the string. You need something like this:
NSString *kw = s.text;
NSString *encodedkw = [ky stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://www.example.com/index.php?keyword=%#", encodedkw];
NSURL *url = [NSURL URLWithString:urlString];
Then look at how to use NSURLConnection to figure out how to put the data into the label.