Faulty button makes the phonecall before pressing it - iphone

The code works but the app is calling the string without the press of the button. As soon as I reach the particular page of the app, it is bypassing everything (Other buttons, map views, etc) and makes the call...
The code I'm using is:
- (void)loadNo2 {
UIButton *no2 = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[no2 setTitle:#"2108642700" forState:UIControlStateNormal];
[no2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[no2 setBackgroundColor:[UIColor blackColor]];
[no2 setFrame:CGRectMake(84, 260, 152, 31)];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://1234567890"]];
[self addSubview:no2];
}
What is wrong with it???

This line brings up the dialog to dial
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://1234567890"]];
You didn't add it as a button action -- you are just calling it.
Replace that line with this:
[no2 addTarget:self
action:#selector(onNo2Touch)
forControlEvents:UIControlEventTouchUpInside];
And add this:
-(void) onNo2Touch:(id) sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://1234567890"]];
}

Looks like you've called the openURL from the method that creates the button.
You should put the call to UIApplication sharedApp... (i.e.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://1234567890"]];
)
inside the method called from the action (that is mapped to the button) .

Related

Hide Status bar with if statement?

I have a QR code reader in my app. Once the reader scans a code, it takes the app to a survey page. I'm trying to get the survey page to hide the statusbar. Here is my code:
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
// self.resultsToDisplay = result;
if (self.isViewLoaded) {
[[NSBundle mainBundle] loadNibNamed:#"yellaViewController" owner:self options:nil];
initWithNibName:#"yellaViewController" bundle:[NSBundle mainBundle]];
[topImage setImage:[UIImage imageNamed:#"yellalogoREAL.png"]];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
This isn't working for me, and the statusbar stays visible. What am I doing wrong?
ALSO: Is there a way I can hide the tabbarcontroller on the surveypage using the same if statement?
In ZxingController's viewDidAppear: (ZxingWidgetController.m)
self.isStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
if (!isStatusBarHidden)
[[UIApplication sharedApplication] setStatusBarHidden:YES];
it cached the previous statusbar state, and when you exit the ZxingController, in viewDidDisappear:
if (!isStatusBarHidden)
[[UIApplication sharedApplication] setStatusBarHidden:NO];
Since the viewDidDisappear in ZxingController will enter after zxingController:didScanResult:
So your setStatusBarHidden in zxingController:didScanResult: is no use.

UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser?

I have a custom UITableViewCell with two labels (UILabel). The table cells are used to display information / text. Inside some of these cells (not all) there is text in this way set:
cell.myTextlabel.text = #"http://www.google.de"
Now I want if I click this text / link, a safari webbrowser should open this webpage. How can I do this?
Best Regards Tim.
Set userInteractionEnabled to YES of your label and add a gesture recognizer to it:
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openUrl:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:gestureRec];
[gestureRec release];
Then implement the action method:
- (void)openUrl:(id)sender
{
UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;
id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];
if ([hitLabel isKindOfClass:[UILabel class]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:((UILabel *)hitLabel).text]];
}
}
If you use a UITextView instead of a UILabel it will automatically handle link detection. Set the view's dataDetectorTypes to UIDataDetectorTypeLink.
Inside your click event you can open safari browser by the following code
 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];

How can i give the external link to UIButton?Is it possible?

I am having the UIButton name as "Buy now".If any one touch the button,the external link should open in the safari browser.How can i achieve this?
It's easy. You set the target and selector for the button, then inside the selector, you call safari to open your link.
Code to call Safari:
Objective-C
- (void)buttonPressed {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString: #"https://www.google.co.uk"]];
}
Swift 2.x
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.co.uk")!)
Swift 3.x
UIApplication.shared.openURL(URL(string: "https://www.google.co.uk")!)
Create a button, and give it a target to a selector that opens Safari with the link.
Basic example:
Make a UIButton
UIButton *button = [[UIButton alloc] initWithFrame:...];
[button addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
Then the method to open the URL
-(void)someMethod {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.ca"]];
}
Don't forget to give your button a proper frame and title, and to add it to your view.
- (IBAction)buyNowButtonPressed {
NSString *s = [ NSString stringWithFormat:#"http://www.sample.com/buynowpage"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:s]];
}
openURL is deprecated from iOS 10 and use following instead.
UIApplication *application = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:#"http://www.yourDomain.com"];
[application openURL:url options:#{} completionHandler:nil];

Remove Done Button From Number Pad upon a New Keyboard Loading

Alright, I'll try to explain this the best I can. I have an iPhone app and it has a text field which the user can only input Numbers. Not a problem there. However, there is no done button on the numpad, so I can't make it disappear. I could make a button that the user presses to dismiss the keyboard, but I'd rather have a done button because the screen is "busy" as is.
Well, after some research, I came across this. Wow, that worked great. However, I also have another text field which requires the default keyboard. And whenever the keyboard comes up, regardless of the type, it has the "Done" button on it.
Not good.
So I do some more digging. Read through the comments, and people mentioned a way to get rid of the Done button using the "Editing Did Begin" trait, to only call the "Done Button" code when necessary. I also got that mostly done. If you type in the number field, and then dismiss the keyboard, and then type in the normal field, the done button does not appear.
However, there is one bug, where the "Done" button still appears. If you tap the Numeric Field, and then tap the Normal field, the keyboard never "disappears", so even though it changes from a numpad to a normal keyboard, the button still is there. I want to remove the buttom from the view, but I'm not sure how to go about this. This is the code I have...
//Done button for numpad
- (void)keyboardWillShow:(NSNotification *)note {
if (showDoneButton == YES)
{
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] hasPrefix:#"3"]) {
[doneButton setImage:[UIImage imageNamed:#"DoneUp3.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown3.png"] forState:UIControlStateHighlighted];
} else {
[doneButton setImage:[UIImage imageNamed:#"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneDown.png"] forState:UIControlStateHighlighted];
}
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
showDoneButton = NO; //This tells done button code to run or not
}
}
- (void)doneButton:(id)sender {
NSLog(#"Input: %#", playerOneLifeLabel.text);
NSLog(#"Input: %#", playerTwoLifeLabel.text);
[playerOneLifeLabel resignFirstResponder];
[playerTwoLifeLabel resignFirstResponder];
}
When the NumPadfield triggers "editing did begin" it calls this function:
- (IBAction)needNumberPad:(id)sender{
showDoneButton = YES;
}
Which then makes sure the done button is shown. When the done button code is done, the variable (as seen above) is set back to NO, so it doesn't show up again if you tap the default text field. The Boolean variable is set to NO as default.
What needs to happen is if you jump immediately from editing the numPad field to the default field, the done button disappears. I can put the code in the function that is called when "Editing did end" but I don't know what to put there. Help would be appreciated, I've gotten this far!
Thank you!
Follow these steps
a. First make the doneButton an instance varible of your class, this will help you maintain the reference to the button
b. Add this code at the beginning of your keyboardWillShow:(NSNotification *)note method
if(!showDoneButton ){
if(doneButton){
[doneButton removeFromSuperview];
doneButton = nil;
}
return;
}
c. add notification for keyBoardWillHide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
d. in keyBoardWillHide method do the following
- (void)keyboardWillHide:(NSNotification *)note
{
if(doneButton)
{
[doneButton removeFromSuperview];
doneButton = nil;
}
}
This should do the trick.
You can use the method below:
//This delegate notify when the user done from the textfield
- (void) textFieldDidEndEditing:(UITextField *)textField{
[yourTextField resignFirstResponder];
[doneButton removeFromSuperview];
doneButton = nil;
}
yourTextField means the textfield that contains done button on keypad.
The previous method will remove the doneButton from the next keyboards.

Disabled touch when UIProgressBarHUD is shown

I've created an app where I'm creating a UIProgressBarHUD to show that something is loading. My question is, how can I disable the view so nothing can be pressed untill the loading is finished?
I've tried setting:
[self.view setUserInterationEnabled:NO];
However this doesn't work :/
Here is the code I'm using for adding the UIProgressHUD:
- (IBAction) showHUD:(id)sender
{
//[self.view setUserInteractionEnabled:NO];
UIProgressHUD *HUD = [[UIProgressHUD alloc]
initWithWindow:[[UIApplication sharedApplication] keyWindow]];
[HUD setText:#"Loading…"];
[HUD show:YES];
[HUD performSelector:#selector(done) withObject:nil afterDelay:1.5];
[HUD performSelector:#selector(setText:) withObject:#"Done!"
afterDelay:1.5];
[HUD performSelector:#selector(hide) withObject:nil afterDelay:4.0];
//[self.view setUserInteractionEnabled:YES];
[HUD release];
}
Any help would be muchly appreciated!!
- James
You can disable user interaction with the nifty property named userInteractionsEnabled, that is defined for UIView. It just so happens that UIWindow is a subclass of UIView, we we can easily disable user interactions for out whole app.
anyViewInYouApp.window.userInteractionsEnabled = NO;
Or keep a reference to the window if you like.
In MBProgressHUD.m
#define APPDELEGATE
(TMAppDelegate *)[[UIApplication sharedApplication]delegate]
- (void)show:(BOOL)animated
{
[[APPDELEGATE window] setUserInteractionEnabled:NO]; //use this line
}
- (void)hide:(BOOL)animated
{
[[APPDELEGATE window] setUserInteractionEnabled:YES]; //use this line
}
As pointed out here, UIProgressHUD is private. You should not use it.
There is a library that gives you what you are looking for though.
It allows you to keep the user from tapping anything while it is updating as you requested.
UIWindow *win = [UIApplication sharedApplication].keyWindow;
[win setUserInteractionEnabled:NO];