0

This is my first attempt at creating an app. and using Xcode.

The reason this question hasn't been answered before is because there are no answers to my question from the perspective I'm currently at, namely, I've followed the instructions on this URL to creating an app:

http://www.wikihow.com/Make-an-iPhone-App

I have got to the Part 3 of 5: Creating the App, section 6 - thereafter my question is not answered - which is how to make my button make a call when tapped.

Therefore I am now, in Xcode at the point where (remember I followed those instructions on the linked page) I have my one button on the screen but ALL the instructions I could find doesn't address exactly what I need to do to make that button make a call.

Some examples show code like here: Making a Button Call a Phone Number in iOS

but doesn't tell me what to do with that code, I'm new to all of this so finding out the exact steps from this point has been brutal at best. Also, all the code I have tried pasting into sheets that have code in them (by clicking around) the code shows errors - all the code I've obtained from the web.

Any help?


P.S. On this page, a poster says that there is actually a button that is associated with making calls, but I again, know not where to find this…

http://www.insanelymac.com/forum/topic/126918-initiating-a-call-on-iphone/

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
njames
  • 23
  • 1
  • 7
  • I'm locked from editing the code! – njames Aug 01 '14 at 12:00
  • So my first link in my main post - if I followed the steps all the way to making the button, and have added likes code to h and m, and have the newest version of Xcode, how do I make the button make a call? – njames Aug 01 '14 at 21:48

2 Answers2

0

Open your xib/storyboard side-by-side with your view controller implementation, hold Control and drag your button (interface builder) to your implementation. Xcode should generate an IBAction for you automatically. enter image description here

On your code, call:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:911"]];

B.R.W.
  • 1,566
  • 9
  • 15
  • THank you too Bruno! For this I don't know how to make the xib/storyboard side by side and the same as above - when I drag the blue line anywhere it just snaps back and nothing happens. – njames Aug 01 '14 at 12:50
  • @njames To get the storyboard and code side by side press the tuxedo button in the toolbar above 'editor'. It should automatically open AppDelegate.h on the other side for you – Luke Aug 01 '14 at 14:38
  • @njames The view controller in your xib/storyboard must have the same class name as your view controller in your code. See: http://cdn2.pqp.vc/vc_cfg.png – B.R.W. Aug 01 '14 at 15:43
0

Basically you need to add the code you found into a 'method' in your appdelegate.m file. This 'method' is a segment of code that is executed whenever it is told to.

So in appdelegate.m, add in the lines above @end

- (IBAction)makeCall:(id)sender {

}

Now paste the line of code you found in between the two curly brackets. Now whenever 'makeCall' is called that line of code you found will be executed which will make a phone call (hopefully)

The next step is making the button tell the 'makeCall' method to run. In order to do this you need to 'declare' the makeCall method, it's the equivalent of putting an item up on sale on eBay: in the previous step you made the item(method), now you want to show the world it's available.

Appdelegate.h is the equivalent to eBay/Craigslist/gumtree in this scenario: add this line of code anywhere above @end:

-(IBAction)makeCall:(id)sender;

Now the final step is to link your button to this, and it's the easiest part. Go back to your interface builder and click on your button. Right click the button and drag a line to the blue box on the left called 'AppDelegate' (this is the files you added code to earlier, remember?) and select makeCall from the little list that pops up. You have successfully linked your button to your method, so now when you click the button you should be able to make a call!

If you want to know more about the specifics of the code you just added, IBAction is the type of method, and it means a method that can have buttons linked to it in interface builder. The (id)sender part means that whenever the method is called, the object/button that called the method is passed along so the method can see who 'sent' for it.

Edit: Ok since you're using storyboards we'll need to create what's called a 'view controller'. This basically delegates and controls (hence the name) whatever is on your phone's screen.

So create a new class by going to file -> new -> cocoa class, and in the fields call it ViewController and make it a subclass of UIViewController.

Now we'll need to copy all the code that we added to appdelegate.h and appdelegate.m over to viewcontroller.h and viewcontroller.m, with the code we added to the appdelegate.h being copied to the same place in viewcontroller.h etc.

They should look something like this:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

-(IBAction)makeCall:(id)sender;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)makeCall:(id)sender {

}

@end

With makeCall having the line of code that you pasted in it earlier.

Now go back to interface builder and click on the little yellow square inside a cube above your screen, and then click on the newspaper-looking icon on the right hand side:

enter image description here

In the class field type in ViewController (it should automatically complete it for you) and then go to the arrow icon tab (which is known as bindings):

enter image description here

Click and drag the little circle beside make call onto your button, and select one of the 'touch down' options that appear. These just mean when to call that method i.e as soon as the user presses the button or when they lift their finger off it. They should now be linked. Give it a try and let me know how it works.

Luke
  • 4,908
  • 1
  • 37
  • 59
  • Thank you Luke - I've come really far but am stuck at the final step - I cannot do any dragging - the blue line does show up when I Ctrl Click and drag the line to either the h or m file - but you don't tell me which one to drag it to. And when I drag it to either of them to those code sections - the blue line just snaps back - nothing happens. Oh I feel so close! – njames Aug 01 '14 at 12:49
  • Don't drag the line to the file, in Bruno's answer above there's a screenshot; do you see the little cubes in between the files and interface builder? If you hover over one of the blue cubes, one of them will say AppDelegate. You need to link the button to this cube. You're really close, this is the final step – Luke Aug 01 '14 at 14:37
  • I don't have any blue cubes Luke, and nor do I have that section between the files and the interface builder - it could be my version - i'm so stressed. I just have the files, interface builder and the code - I'm trying the view options but I can't find anything that will display that column with the cubes in it. I do have a round yellow circle with a square in it, a red cube and green exit button under my interface when I click on the button (or not) and when I click on View Controller I am lost from here. I wish everyone could see my screen - you'd probably all get it in a second... – njames Aug 01 '14 at 20:51
  • OK guys this is my view, I think I've followed all the instructions - as you will see there is a button in the middle of the interface, and as it seems based on what you're saying, if I have the code in the right files like Luke mentioned above (which I do) this now should be really easy… but it isn't and it's a little maddening…but I do thank you guys for helping - that's awesome. Ok here it is http://s1383.photobucket.com/user/345ad/media/screen_zpsc80d488e.jpg.html – njames Aug 01 '14 at 21:13
  • Ah, you're using the new 'storyboard' thing that Xcode 5 introduced. I haven't used it before but I think you should be still link it by dragging a line from your button to the yellow square inside a circle underneath the screen. I'm going to try it now myself actually and I'll report back to you – Luke Aug 01 '14 at 21:46
  • Hmmm, unfortunately in order to link the button we're going to need to use a slightly more complicated method. I'll update my answer – Luke Aug 01 '14 at 21:56
  • Thank you Luke, and Bruno, everyone helped and also this link worked in line with your assistance - https://www.youtube.com/watch?v=ln6T2rm-Njg and I finally even got it onto my device - you guys are great! – njames Aug 02 '14 at 02:47