Ultra beginner. Issues with hiding UIBarButton - ios

I dived into learning apps development couple days ago and faced my first problem. Basically what I'm trying achieve is to hide and disable back button when there is no UIWebView history. Here is my code:
-(IBAction)backButtonPressed:(id)sender {
[webView goBack];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:#"http://nearom.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
}
- (void) backButton{
if(webView.canGoBack == YES)
{
back.enabled = YES;
back.width = 0;
back.title = #"back";
}
else {
back.enabled = NO;
back.width = 0.01;
back.title = nil;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView{
[self backButton];
}
But backButton method isn't working. Could anybody explain what I'm doing wrong?

You might want to add more of your code. The only strange thing I have seen is that you use a parameter width. What do you want to do with that?
What I can not see from your code is if you have assigned the webviewDelegate to self so that webViewDidFinishLoad gets called.

Assuming you have a Navigation Controller, you should do the following:
self.navigationController.navigationItem.hidesBackButton = TRUE;

Related

UISearchBar url request

I have a downloader in which I am trying to get the pasted text in my search bar to start downloading on tapping the return key.
How would I get the search bar to send a request to the pasted text to initialize the download?
What I'm calling:
NSString *pasteboardString = searchBar.text;
NSURL *url = [NSURL URLWithString:pasteboardString];
What needs to get triggered:
[self downloadURL:*HAS TO BE URL* userInfo:nil];
Any help is appreciated, thank you.
So I ended up figuring it out. In case somebody finds this useful.
#pragma mark - Firing Download
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
dispatch_async(dispatch_get_main_queue(), ^
{
// Do the search...
NSURL *url = [NSURL URLWithString:searchBar.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
bool valid = [NSURLConnection canHandleRequest:req];
if(valid)
{
[self downloadURL:url userInfo:nil];
NSLog(#"Valid URL - %#",searchBar.text);
[searchBar resignFirstResponder];
[searchBar setText:nil];
}
else
{
[alert showError:#"Enter valid url"];
[searchBar becomeFirstResponder];
[searchBar setText:nil];
}
});
NSLog(#"Search Button Pressed");
}

why does NOT UIWebView load the content in the first time

I have a UITableView within a UINavigationController,when I select a cell in the table view,it should slide to a UIWebView
//code in table view delegate(tableviewcontroller)
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSURL *url = [NSURL URLWithString:#"https://www.baidu.com/"];
self.webViewController.title = #"UIWebViewTest";
self.webViewController.URL = url;
[self.navigationController
pushViewController:self.webViewController animated:YES];
}
self.webViewController was initialized by
- (void)viewDidLoad {
[super viewDidLoad];
//..
//other code
//..
YGGWebViewController *wvc = [self.storyboard
instantiateViewControllerWithIdentifier:#"wvc"];
self.webViewController = wvc;
}
in the YGGWebViewController, I have a NSURL *URL property and override the setter
- (void)setURL:(NSURL *)URL{
_URL = URL;
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
}
besides,YGGWebViewController also have a UIWebView *webView property linked to a UIWebView in storyboard.
what odd is the web view does not display anything when it slides to the screen in the first time,but it will show the website in the second time(go back to the table view by navigation bar,and select a cell again).
Environment
xcode-7.0
deployment target-7.0
OS X Yosemite-10.10.5
P.S
if I move the code
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
to viewDidLoad in YGGWebViewController,it works right.but why?

webView ShouldStartLoadRequest method not working properly

I am loading a webview with some url using:
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://abc-ecatalogue.xyz-ar.com/"]]];
then i am checking if the home page is being loaded then i dont want the navigation bar for this i am using:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
// Here you can check the URL
NSURL *url = [request URL];
NSString*str=#"http://abc-ecatalogue.xyz-ar.com";
if ([url.absoluteString isEqualToString:#"http://havells-ecatalogue.adstuck-ar.com/"])
{
self.navigationController.navigationBarHidden = YES;
NSLog(#"hello");
return NO;
}
else
{
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:self action:#selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
}
return YES;
}
but when the control goes inside the loop the web view is not loaded and the log which i have printed is continously printed.
You have self.navigationController.navigationBarHidden = NO; in both the if & else part. You should change it to YES in the if section.EDIT:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
// Here you can check the URL
NSURL *url = [request URL];
NSString*str=#"http://abc-ecatalogue.xyz-ar.com";
if ([url.absoluteString isEqualToString:#"http://havells-ecatalogue.adstuck-ar.com/"]) {
self.navigationController.navigationBarHidden = YES;
NSLog(#"hello");
} else {
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:self action:#selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
}
return YES;
}

UIAlertView dismiss on return

I'm having a problem with my search alert when using the search-tab. When someone is typing their search-string and hit return it will only dismiss the keyboard and you still have to tab the "Search" (Zoek) button do actually start a websearch.
Here is my code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
[searchview addSubview:indicator];
NSString *urlAddress= #"http://www.sportdirect.com/articles?search=";
urlAddress =[urlAddress stringByAppendingString:inputText];
NSURL *url= [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
[searchview loadRequest:requestObj];
[[searchview scrollView] setBounces: NO];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
[TestFlight passCheckpoint:#"Voerde zoekopdracht uit"];
}
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[_message dismissWithClickedButtonIndex:(0) animated:YES];
NSString *inputText = [[_message textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
[searchview addSubview:indicator];
NSString *urlAddress= #"http://www.sportdirect.com/articles?search=";
urlAddress =[urlAddress stringByAppendingString:inputText];
NSURL *url= [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
[searchview loadRequest:requestObj];
[[searchview scrollView] setBounces: NO];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
[TestFlight passCheckpoint:#"Voerde zoekopdracht uit"];
}
[textField resignFirstResponder];
return NO; // We do not want UITextField to insert line-breaks.
}
Does anyone know how to actually do a search as soon as the return button on the keyboard is tabbed?
Thanks in advance!
Your class needs to implement the UITextFieldDelegate to get the keyboard return callback. As suggested by borrden, this can be done as follows
[message textFieldAtIndex:0].delegate = self;
Then implement this method,
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Dismiss the alert view
// Initiate the search
return YES;
}
Use this UIAlertView method. Refer Apple docs
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
Call this on your alert view instance when required to dismiss it programmatically. You would require to keep message as an ivar to access it when required.
Hope that helps!
You can do this in UITextField delegate method:
your class must implement UITextFieldDelegate. set textField.delegate = self;
After implement this method:
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
// here you can write what you want, for example search
[textField resignFirstResponder];
return NO; // We do not want UITextField to insert line-breaks.
}

How to use a pull to refresh in a WebView ( iOS 5 )?

How can I make a "Pull to refresh" in iOS 5 for an UIWebView ? I can't find a tutorial.
I have got a WebView in a Tab Bar application, and I want to refresh the WebView with a "Pull to refresh" like Twitter.
Where can I find a complete tutorial ?
Thank you
I cannot provide a tutorial, maybe I'll write one but I think it was quite simple,
I did this slightly modifying SPPullView.
Here you can grab the files (SPPullView.m and SPPullView.h).
Relevant code in the main UIViewController is something like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.pullView = [[SPPullView alloc] initWithScrollView:self.webView.scrollView];
[self.pullView setDelegate:self];
[self.webView.scrollView addSubview:self.pullView];
[self.webView.scrollView setDelegate:self];
for (UIView *subview in [self.webView.scrollView subviews] ) {
subview.userInteractionEnabled = NO; // used to disable copy/paste, etc inside the webview
}
//[self doYourStuff];
}
//Called when the user pulls to refresh (this is when you should update your data)
- (void) pullViewShouldRefresh: (SPPullView *) view {
[self updateYourStuff];
}
You can also add some logic or properties to prevent the user zooming, or deactivate the pull view temporarily while zooming.
This works very well: http://blog.gomiso.com/2012/03/22/yet-another-pull-to-refresh-library/
I wrote this code and it is working fine. This may help you.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = URL you want to hit;
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
webView.delegate = (id)self;
[webView loadRequest:requestObject];
UIRefreshControl *refreshControlOnPull = [[UIRefreshControl alloc] init];
[refreshControlOnPull addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView refreshControlOnPull]; //<- this is point to use. Add "scrollView" property.
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
NSString *fullURL = The Url you want to hit;
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[refresh endRefreshing];
}

Resources