I am just trying something completely new and simple in Xcode. I was looking a tutorial to create a button that plays music or a sound. All seemed to be okay until preview time. There seems to be an expected identifier issue at IBAction as well as it expected something after (void). But each time I do as the prompt says the prompts multiply. I've tried to do as all of them said but then it was no longer the same function. Any ideas?
#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface StrackerViewController : ViewController
//#end
//#implementation StrackerViewController: ViewController
- (IBAction) epic; {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) # "epic", CFSTR ("mp3"),NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
- (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.
}
#end
Your IBAction is not a properly formed function. It needs to be in the form:
-(IBAction) epic:(id)sender {
Once you have done that you should probably delete and remake the link with your storyboard as that is likely to be broken too.
Related
I have a UIWebView inside my app, this loads a website which is trying to get the current gps location of the user. On Safari it presents a popup which asks if you want to give your current location to the website, on my UIWebView no such thing happens and my website is not able to get the gps location of the user. How can I accomplish this? I am new to ios developing so bear with me if this is something very easy to do, but I couldnt find an example so far.
Here is what I have so far:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *_webView;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:#"http://google.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)goBack:(id)sender {
if ([_webView canGoBack]) {
[_webView goBack];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Edit your app's info.plist and set
NSLocationWhenInUseUsageDescription "String you want to explain why you need location."
Hey I am very new to programming in x-code and I have been following geeky lemon's tutorial, and I wanted to add sound into my project. I am trying to create a simple piano application. I have followed every step correctly, and my code was exactly same as their although when I ran the program, the iphone simulator froze at black screen and opened xcode with
Thread 1: Signal SIGABRT in
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}T}
In the console the error was:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
* First throw call stack:
My code is:
.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController : UIViewController
{
SystemSoundID soundID;
}
- (IBAction)c1Press:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
NSURL *buttonURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Resource/Audio/keys/C1" ofType:#"MP3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) buttonURL, &soundID);
[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)c1Press:(id)sender {
AudioServicesPlaySystemSound(soundID);
}
#end
I have done some research on this, and one answer was that the bundle was not in properly or something like that, but I checked in my build Phases and all my resources are all there and none are red.
I have recently started looking into basic IOS app development I am using the following tutorial online https://www.youtube.com/watch?v=GHK3oREwVls&list=PLhAWmh1PlbzGrr8PGLvJBtJ7qniLVTQ9E and I seem to have hit a stumbling block, below are my two view controllers. The error I am getting is regarding the displaytext.text section of the view controller M. The error appearing is;
HelloCocoaViewController.m:30:4: Use of undeclared identifier 'displaytext'; did you mean '_displaytext'?
When I make the suggested change I get a Thread 1: signal SIGABRT around the below code.
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([HelloCocoaAppDelegate class]));
Below is my the code I am currently using.
//
// HelloCocoaViewController.h
// Test
//
#import <UIKit/UIKit.h>
#interface HelloCocoaViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *displaytext;
- (IBAction)hellobutton:(id)sender;
- (IBAction)byebutton:(id)sender;
#end
//
// HelloCocoaViewController.m
// Test
//
//
#import "HelloCocoaViewController.h"
#interface HelloCocoaViewController ()
#end
#implementation HelloCocoaViewController
- (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)hellobutton:(id)sender {
displaytext.text=#"Hello";
}
- (IBAction)byebutton:(id)sender {
displaytext.text=#"Bye";
}
#end
I will appreciate any and all help.
Edit: I have now tried to implement the solutions suggested (I hope this is correct) however I am sill experiencing the same SIGABRT issue.
#implementation HelloCocoaViewController
#synthesize displaytext;
- (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)hellobutton:(id)sender {
self.displaytext.text=#"Hello";
}
- (IBAction)byebutton:(id)sender {
self.displaytext.text=#"Bye";
}
#end
as you are not using #synthesize to define a backing variable for the property it will implicitly add on as _<propertyname>
so either use the property as self.displaytext or access the property's backing variable directly via _displaytext
Since Apple switched the default compiler from GCC to llvm Objective-C evolves very fast. WWDC videos are a great way to keep track of those improvements
WWDC 2013: Advances in Objective-C
WWDC 2012: Modern Objective-C
I am new to Xcode so I've been trying to follow along to tutorials but haven't come across any that explain what I'm trying to achieve. I just want to play a video automatically when you open an application. I have it somewhat working in that the audio plays, but I cannot see any video. Am I missing something? I am getting this Warning in my output window:
Warning: Attempt to present MPMoviePlayerViewController: 0x831d7a0 on ViewController: 0x9d10540 whose view is not in the window hierarchy!
In my ViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Placeholder" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Instead of
[playercontroller.moviePlayer play];
do
[playercontroller play];
Side note:
Also check up on your naming convention. In Objective-C variable names "should" have capital letter on each new word in the variable. E.g. instead of playercontroller use playerController.
I know your status.
it's warning from Xcode.
So, you just change the code.
like this,
[playercontroller performSelector:#selector(player)];
For example, on the iOS SDK download page, there's sample code; I'm using the calculator app (iPhoneUnitTests). I would like to know if it is possible to add sounds to the button presses on the app that's already built, easily.
It is actually very simple to play short sounds, like button sounds. Here is a quick example.
You must link the AudioToolbox.framework binary
SoundExampleViewController.h;
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface SoundExampleViewController : UIViewController{
SystemSoundID mySoundID;
}
#end
SoundExampleViewController.m:
#import "SoundExampleViewController.h"
#implementation SoundExampleViewController
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"SoundFileName" ofType:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID);
}
- (void)viewDidUnload{
[super viewDidUnload];
AudioServicesDisposeSystemSoundID(mySoundID);
}
#end
Then to play you simply call: AudioServicesPlaySystemSound(mySoundID);