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");
}
Related
I am new to iOS. I have a UITextfield and a Keyword Search Button. When ever I want to search a keyword from a service and press enter. Tt should display the related searched keyword from a service. Please help me to fix this issue? TIA!
- (IBAction)KeywordSearchClicked:(id)sender {
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
[self KeywordcallSignupProfileService:dict];
}
-(void)KeywordcallSignupProfileService:(NSMutableDictionary *)dict
{
[SVProgressHUD showWithStatus:#"" maskType:SVProgressHUDMaskTypeBlack]; // Progress
NSString * post = [[NSString alloc]initWithFormat:#"userId=%#&key_word%#",UserId,[dict objectForKey:#"key_word"]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.amarbiyashaadi.com/service/amarbiya-service.svc/userKeywordSearch/"]];
RBConnect = [[RBConnection alloc]init];
RBConnect.delegate = self;
[RBConnect postRequestForUrl:url postBody:post];
}
#pragma mark - MRConnection Delegate Methods
- (void)jsonData:(NSDictionary *)jsonDict
{
[SVProgressHUD dismiss];
NSMutableArray *jsonArr;
NSMutableDictionary *userDict,*dict;
NSArray *arr=[jsonDict allKeys];
jsonArr=[jsonDict objectForKey:#"DataTable"];
if (jsonArr.count>0) {
// Save credentials in user defaults
matchesProfileArr=[jsonArr mutableCopy];
DisplayTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"DisplayTableViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
else
{
NSString *error=#"Somthing Went Wrong";
[SVProgressHUD showErrorWithStatus:error];
}
}
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;
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 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];
}
I have a UIViewController which contains a UIWebView.
i login to a website for say facebook using the UIWebview in the UIViewController then i click on done, which dismiss the view controller, which i suppose will release the view controller. but when i open the UIWebview again (without exiting the app and also even after terminating the app) the webpage is still log into Facebook after the web view loads. How should i release the web view so that every time i click on done and return back to the web view, the web view will always be like "brand new" not log-on to any website that i had previously log-on to.
- (void)viewDidLoad{
NSLog(#"webView viewDidLoad called");
appDelegate = (LoginDBAppDelegate *)[[UIApplication sharedApplication] delegate];
loginObj = [appDelegate.loginArray objectAtIndex:currentSelectedRow];
myWebView.delegate = self;
[super viewDidLoad];
[self showWebView];
}
-(void)showWebView{
NSLog(#"webView showWebView called");
NSURL *url = [NSURL URLWithString:loginObj.loginURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
-(IBAction)done_Clicked:(id)sender{
NSLog(#"webView Done_Clicked");
//dismiss the controller
[self.navigationController dismissModalViewControllerAnimated:YES];
}
-(void)dealloc{
[myWebView release];
myWebView.delegate = nil;
[activity release];
[urlTextField release];
[super dealloc];
}
I have tried this but it didn't work:
-(IBAction)done_Clicked:(id)sender{
NSLog(#"webView Done_Clicked");
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[myWebView stringByEvaluatingJavaScriptFromString:#"var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';"];
[myWebView stringByEvaluatingJavaScriptFromString:#"document.open();document.close()"];
//dismiss the controller
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I have also tried this without any success:
- (void) viewDidDisappear:(BOOL)animated {
NSLog(#"webView viewDidDisappear called");
[super viewDidDisappear:animated];
[self.myWebView removeFromSuperview];
self.myWebView.delegate = nil;
self.myWebView = nil;
[myWebView release];
}
Got the answer from someone on the forum, so credit to him...
Place this code in viewDidLoad
//Set Cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//Clear All Cookies
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
//if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}