I'm trying to launch a URL with the UIWebView upon launch of my Mac application.
I started out with this,
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
- (void)viewDidLoad {
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
With that, I get two errors that I can't figure out.
(ViewController.h) #interface ViewController : UIViewController {
Error - Cannot find interface decleration 'UIViewController', superclass of 'ViewController'
(ViewController.m) - (void)viewDidLoad {
Error - Missing context for method declaration
I'm not really familiar with is and can't find what I'm doing wrong.
For OSX app, you don't have UIViewController, instead, you have NSWindowController and NSViewController, check Mac Developer Library.
You are using IOS functions instead of OSX.
Try this
In youy .h file
#import <WebKit/WebKit.h>
WebView *myWebView;
In your .m file
- (void)windowDidLoad
{
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[myWebView mainFrame] loadRequest:requestObj];
}
Don't forget to include "webkit.framework"
Related
I have constructed some code that is supposed to play audio when a button is clicked. There are no errors in the code, but when I open the iOS Simulator and press the button, no sound plays. The button does the tap animation, but nothing happens.
I have both the AVFoundation and AudioToolbox frameworks.
I also have my audio file in resources.
I am not using breakpoints.
The button is set to use the 'playAudio:' first responder upon Touch Up Inside.
Here is my 'ViewController.h' file:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
And here is my 'ViewController.m' file:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
sleep(5);
};
-(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
The audio segment is about 3 seconds.
Any help is much appreciated!
Thanks!
I am quite new to ios but I had the same problem and was able to fix it.
I simply declared my AVAudioPlayer variable as a global variable and called
[audioPlayer prepareToPlay];
before
[audioPlayer play];
Just calling prepareToPlay didnt fix it, I had to declare the global variable to for it to work.
Not sure why that makes a difference to the Simulator but it works now.
Play sound/music file in objective-c,
I had the same problem and was able to fix it with below code.
.h file
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)actionOnPlayButton:(id)sender;
- (IBAction)actionOnStopButton:(id)sender;
- (void)playSound ;
#end
.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
AVAudioPlayer *audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Play Music";
}
-(void)playSound{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound10"
ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (IBAction)actionOnPlayButton:(id)sender {
[self playSound];
}
****Used above lines of code, it will work fine...
Note:- Don't forget to import following framework:-**
#import
#import
I am very new to iOS programming and am making a simple app. the first UITabBarItem loads a page but before it loads there is a label. I am trying to make the label disappear after the web page loads but it doesn't work. I believe I need to set the web view delegate but I don't know how.
firstcontroller.h
#import <UIKit/UIKit.h>
#interface OTFFirstViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webPage;
#property (strong, nonatomic) IBOutlet UILabel *pageLoading;
#end
firstcontroller.m
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *fullURL = #"http://asdf.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webPage loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webPage
{
_pageLoading.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Insert _webPage.delegate = self; before calling loadRequest:.
You should alse modify interface definition to #interface OTFFirstViewController : UIViewController <UIWebViewDelegate>.
View controller h:
#interface ViewController : UIViewController
{
IBOutlet UIWebView *WebView1;
IBOutlet UIWebView *WebView2;
IBOutlet UIWebView *WebView3;
IBOutlet UIWebView *WebView4;
IBOutlet UIWebView *WebView5;
}
#end
view controller m:
#import "ViewController.h"
#interface UIViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myURL1 = [NSURL URLWithString:[#"http:://www.youtube.com"]];
NSURLRequest *MyRequest1 = [NSURLRequest requestWithURL:myURL1];
[WebView1 loadRequest:MyRequest1];
NSURL *myURL2 = [NSURL URLWithString:[#"http:://www.gmail.com"]];
NSURLRequest *MyRequest2 = [NSURLRequest requestWithURL:myURL2];
[WebView2 loadRequest:MyRequest2];
NSURL *myURL3 = [NSURL URLWithString:[#"http:://www.soundcloud.com"]];
NSURLRequest *MyRequest3 = [NSURLRequest requestWithURL:myURL3];
[WebView3 loadRequest:MyRequest3];
NSURL *myURL4 = [NSURL URLWithString:[#"http:://www.socialblade.com"]];
NSURLRequest *MyRequest4 = [NSURLRequest requestWithURL:myURL4];
[WebView4 loadRequest:MyRequest4];
NSURL *myURL5 = [NSURL URLWithString:[#"http:://www.fullscreen.net"]];
NSURLRequest *MyRequest5 = [NSURLRequest requestWithURL:myURL5];
[WebView5 loadRequest:MyRequest5];
}
#end
Decription:
The error is on each of the links "]". What am I dong wrong?
And I can't link/connect my outlets to the UIWebView's set up in tabs.
Please help me.
It's simple. You do not need the []'s around each string. Change it's URL to the following:
SURL *myURL4 = [NSURL URLWithString:#"http:://www.socialblade.com";
NSURLRequest *MyRequest4 = [NSURLRequest requestWithURL:myURL4];
[WebView4 loadRequest:MyRequest4];
That should solve it. If not, let me know.
Edit
Also, you are declaring your properties incorrectly, which is causing your to not connect them. You should declare your webView's like this:
View controller h:
#interface ViewController : UIViewController
#property (assign) IBOutlet UIWebView *WebView1;
#property (assign) IBOutlet UIWebView *WebView2;
#property (assign) IBOutlet UIWebView *WebView3;
#property (assign) IBOutlet UIWebView *WebView4;
#property (assign) IBOutlet UIWebView *WebView5;
#end
Then you must synthesize them in your .m
view controller m:
#import "ViewController.h"
#interface UIViewController ()
#end
#implementation ViewController
#synthesize WebView1;
#synthesize WebView2;
#synthesize WebView3;
#synthesize WebView4;
#synthesize WebView5;
After they are synthesized, you can call them as you have done. :)
I have one class UIViewController and Inside I have one button , I want to call one method from another class
would you please help me to implement the action for my button
I'm new to objective-c
my button Action in UIViewController:
- (IBAction)ActionButton:(id)sender {
NSLog(#"A1");
}
my method in WebViewController:
-(void)loadWebView{
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:
#"TestName/TestName1/Test1Name1" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
my question is how to call this method to my button since it's in another view?
I have one method in my WebViewController and I want to call it from one button that I have in UIViewController
You could try this:
Say you have a class called ViewA which implements the method.
If you want the button action to invoke that method, you need to have an instance of another class.
-(void)loadWebView{
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:
#"TestName/TestName1/Test1Name1" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
For you button VIEW:
#import "ViewA.h"
ViewA *instanceOfViewA;
// assign an instance to instanceOfViewA
Your Button method:
- (IBAction)ActionButton:(id)sender {
NSLog(#"A1");
[yourButton addTarget:instanceOfViewA
action:#selector(loadWebView)
forControlEvents:UIControlEventTouchUpInside];
}
You can use notification or delegate or have the reference of WebViewController in your UIViewController ..
You may store a reference to WebViewController in your main view controller and send a message to that reference, check my answer.
code:
in MyViewController.h:
#class WebViewController;
...
#interface MyViewController
{
...
}
#property (nonatomic, retain) IBOutlet WebViewController* webViewController;
...
in MyViewController.m:
#import "WebViewController.h"
...
#implementation MyViewConroller
#synthesize webViewController;
...
- (void) dealloc {
...
self.webViewController = nil;
...
[super dealloc];
}
Assign your WebViewController (like you did with buttons and stuff) in IB and then in code:
- (IBAction)ActionButton:(id)sender {
NSLog(#"A1");
[self.webViewController loadWebView];
}
I am using Xcode 4.3 and i have a problem with playing sound...
I downloaded this project from an answer i found here from but when i change his file with mine http://dl.dropbox.com/u/63377498/AudioText.zip i can't hear anything. Can be a problem of my simulator? Settings of my mac? I don't know...
That's the code i use:
#interface ViewController ()
#property (nonatomic, strong) AVAudioPlayer *theAudio;
#end
#implementation ViewController
#synthesize theAudio = _theAudio;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"sad" ofType:#"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
}
- (IBAction)pushBell {
[self.theAudio play];
}
Finally i found the answer on my own. I was doing everything right but i used a sound file downloaded from youtube in mp3 format and i converted to .caf format by changing the extension....Thats the reason why i can't play that sound!