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

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];

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.

How make an outgoing call from my app?

i develop a calling program in the below way.
my main aim is enter the number in textfield and whenever i press the call button it convert
into the integer and then perform calling action.in xcode is there any possibility to create a
code for outgoing call?
And is there any possibility to block the incoming calls?
i am writing the code below way.
my .h file is
#interface mytextViewController : UIViewController
{
IBOutlet UILabel *enterphonenumber;
int currentNumber;
}
#property(nonatomic,retain)IBOutlet UILabel *enterphonenumber;
-(IBAction)call;
-(IBAction)press:(id)sender;
#end
my .m file is
-(IBAction)press:(id)sender
{
currentNumber = currentNumber*10 + (int)[sender tag];
enterphonenumber.text = [NSString stringWithFormat:#"%i",currentNumber];
}
-(IBAction)call
{
int x =[enterphonenumber.text intValue];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:x]];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"%tel:9963610177"]];
NSLog(#"sample %#",[NSURL URLWithString:#"my phone number is calling"]);
}
And also how to examine it performs a calling
please any one help because i am the new to develop this application
Thanking you,
Try using the tel protocol.
if ([[[UIDevice currentDevice] model] isEqualToString:#"iPhone"])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://%d", number]]];
}
You cannot block any incoming calls.
You can't block any incoming calls in your app.

Faulty button makes the phonecall before pressing it

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) .

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 to stop the NetworkActivity on a UIWebView?

I load some content on my UIWebview ,and when the UIWebView load finish the content of a URL such as "http://www.google.com", but the network NetworkActivityIndicator didn't stop,any suggestion?
I found somebody use [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES],but I think this is just to hidden the NetworkActivityIndicator ,in fact it does't stop,is that right??
Now what I want is really to stop the NetworkActivityIndicator ,how to accomplish it??
In your UIWebView delegate:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
Remove any other calls to -setNetworkActivityIndicatorVisible in this delegate.'
If you haven't already assigned a delegate for this UIWebView, then do so as follows:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[webView setDelegate:self];
[self.view addSubview:webView];
This of course assumes you are inside a UIViewController and that self.view exists, so change as needed.
#McGrady:Try this..
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidfinishLoad");
[activity stopAnimating];
}
Here activity is your activityindicator name
Hope this help you..
Regards
Renuga