UIWebView Crashing App - objective-c

This is my Code that all together crashes my app
The bit that assigns the webview its value: It works fine until the last line. When it SIGBARTS.
[cell setLabelText:[cellTitle objectAtIndex:indexPath.row]];
[cell imagesetter:[imageTitle objectAtIndex:indexPath.row]];
[cell desSetter:[desTitle objectAtIndex:indexPath.row]];
[cell changeProductWeb:[webTitle objectAtIndex:indexPath.row]];
The Array
webTitle = [[NSArray alloc] initWithObjects:
#"http://www.google.com/",
#"http://www.google.com/",
#"http://www.google.com/",
#"http://www.google.com/",
nil];
The action
- (IBAction) changeProductWeb:(NSString *)str{
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
This is the only bit the crashes the program. When I remove it it wont crash.

You probably need to call [webView retain] at some point. You would get a SIGABART like that if your webView is not being held onto by your class.
A simple solution is to use this in your .h file:
#property (nonatomic,retain) UIWebView *webView;
Then in your .m file go:
#synthesize webView;
Then, wherever you refer to your webview, make sure to add "self"
[self.webView loadRequest:requestObj];

Related

Objective-C How to play a sound when a cell's button is tapped?

I have a button on each cell and while the button is tapped I want to play different audio file from array for each cell.
I don't know how to implement an array of sounds/audio files for my table view cells.
I have a button outlet and action on my UITableViewCell. But I am not sure how to initialise my AVaudioplayer in tableview.m specifically in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell22";
Below I've inserted an image of the project I want to achieve.
image1
You can try like this:
Import AVFoundation.framework
Place this code in didSelectRowAtIndexPath method
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"your sound name" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[player play];
Note: Add your audio files to project and then take the names into one array. Then you can use your audio names based on indexPath.
If you want to play sound on button click which is in tableViewCell then add tag as indexPath to that button and add action to button like this in cellForRowAtIndexPath method
button.tag = indexPath.row;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
and paste the same code in button action. but use sender.tag instead of indexPath.row
-(void)buttonPressed:(UIButton *)sender
{
NSString *soundName = [yourArray objectAtIndex:sender.tag];
NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; [player play];
}
check now i updated code :its playing sound
1.create property in .h or .m file for ur AVAudioPlayer . like
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
declare <AVAudioPlayerDelegate> in .m file and
#synthesize audioPlayer;
3.implement this code in :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
yourSoundArray=[NSArray arrayWithObjects:#"sss",#"sss",#"sss",#"sss",#"sss",#"sss", nil];//dont include formate type
NSString *audioPath = [[NSBundle mainBundle] pathForResource: [yourSoundArray objectAtIndex:indexPath.row] ofType:#"mp3"];
NSLog(#"%#",audioPath);
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError];
audioPlayer.delegate=self;
if (!audioError) {
NSLog(#"playing!");
audioPlayer play];
}
else {
NSLog(#"Error!");
}
4.
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"%d",playing the audio);
}
happy coding its working please check :
Mate You need to use two things mostly
1st import AVFounfation.framwork
2nd Do code id "didSelectRowAtIndexPath"
for more details
Xcode 6 - Press button to play sound
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"%#",selectedCell.textLabel.text);
AVAudioPlayer *audioPlayer101;
if ([_detailPhrases isEqualToString:#"Basics"]){
NSArray *soundArray = [NSArray arrayWithObjects:#"cellTapSound", #"Second", nil];
NSString *audioPath = [[NSBundle mainBundle] pathForResource: [soundArray objectAtIndex:indexPath.row] ofType:#".mp3"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError];
if (!audioError) {
[audioPlayer101 play];
NSLog(#"playing!");
}
else {
NSLog(#"Error!");
}
}
}

passing NSURL and keeping it

I am passing a NSURL from my pickerviewcontroller to my mainviewcontroller. The url is dependent on the user choice in pickerviewcontroller. It works fine, but the user has to repetedly go to the pickerviewcontroller every time the app is restarted to make the choice.
Maybe the code will explain the problem better: This is the pickerviewcontrollers relevant parts...
//The save button on the pickerviewcontroller
- (IBAction)selectedRow:(id)sender {
MainViewController *vc1 = [self.storyboard
instantiateViewControllerWithIdentifier:#"webview"];
vc1.destinationweb = selectedRow;
[self presentViewController:vc1 animated:YES completion:nil];
Once in MainViewController the destinationweb log shows the address of the user choice in pickerview. But If I turn off the app or go forwards in the app (I have another view controller) and then back to MainViewController - it shows destinationweb = null
This is the MainViewController relevant parts:
- (void)viewDidLoad {
{
//webView.hidden = YES;
self.webView.delegate = self;
NSURL *url = destinationweb;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
NSLog(#"destinationweb shows= %#", destinationweb);
}
[super viewDidLoad];
}
I have no idea what I am supposed to do to make the MainViewController remember the url until another row in picker view is selected??
Just save the url string in user defaults and retrieve the url string in view controller
- (IBAction)selectedRow:(id)sender
{
MainViewController *vc1 = [self.storyboard
instantiateViewControllerWithIdentifier:#"webview"];
//Instead of passing it just save the url string in user defaults
NSString *urlString = [selectedRow absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:#"UserURL"];
//vc1.destinationweb = selectedRow;
[self presentViewController:vc1 animated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//webView.hidden = YES;
self.webView.delegate = self;
NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:#"UserURL"];
NSURL *url = [NSURL URLWithString:urlString ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
NSLog(#"destinationweb shows= %#", destinationweb);
}
if you want to keep the value once the app is restarded, you should storage the value (url) in a file, so when you restart it you can read it from there.

How to open a PDF within a Cocoa Application

I would like to open a PDF within a Cocoa application. I tried the following to open the PDF using Preview.
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:#"filepath"]];
But I would like to open the PDF attached to a window, similar to Xcode's Help window. How do you achieve this using PDFKit?
I imported QuartzFrameWork, and added a PDFView in the xib. The following code did the job for me:
NSURL *url = [NSURL fileURLWithPath:#"filePath"]
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:url];
[_pdfView setDocument:pdfDoc];
You can use PDFView for opening PDF files within your application.
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/PDFKitGuide/PDFKit_Prog_Intro/PDFKit_Prog_Intro.html#//apple_ref/doc/uid/TP40001863-CH203-SW1
in .h file
#interface MyViewController:UIViewController <UIDocumentInteractionControllerDelegate>
#property(nonatomic, retain) UIDocumentInteractionController *documentInteractionController;
in .m file
#synthesize documentInteractionController;
- (void)viewDidLoad{
[super viewDidLoad];
documentInteractionController = [[UIDocumentInteractionController alloc] init];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
-(void) openPdf{
NSString *filePath=filePath = [[NSBundle bundleForClass:[self class]] pathForResource:#"filename" ofType:#"pdf"];
NSURL *URL = [NSURL fileURLWithPath:filePath];
if (URL) {
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:YES];
}
}

AVAudioPlayer causing memory leak

Not sure if this is a simulator issue or I am missing something, the following code gives me a memory leak although theAudio is released as in dealloc. I am playing a .wav file and the sound will change depending on the value of an integer. The App runs ok but 16 bit leak is flagged whwn I test this in xcode
Any suggestion is greatly appreciated:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface Water : UIViewController
<AVAudioPlayerDelegate>
{
}
#property (nonatomic,retain) AVAudioPlayer *theAudio;
-(IBAction) goButMenu: (id) sender;
-(IBAction) goDrinkMenu: (id) sender;
-(IBAction) playwater;
#end
#implementation Water
#synthesize theAudio;
-(IBAction) goDrinkMenu: (id) sender{
ButDrinkMenu *butdrinkmenu1 = [[ButDrinkMenu alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:butdrinkmenu1 animated:YES];
}
-(void) viewDidLoad {
if (BoyOrGirl == 1) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Applause" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
else {
NSString *path = [[NSBundle mainBundle] pathForResource:#"bongo" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
-(IBAction) playwater{
if (BoyOrGirl == 1) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Applause" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
else {
NSString *path = [[NSBundle mainBundle] pathForResource:#"bongo" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
Definitely test on the device directly; testing for leaks with the simulator may return inaccurate results.
That said, there are several memory-related concerns with this code sample. You may wish to include your dealloc method here as that may offer some insight, but here's a problem (that appears in several of your allocations):
Your AVAudioPlayer isn't being assigned to your property, nor is it being released in scope. Ideally a player allocation would look more like:
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc] initWithContents....
[myPlayer setDelegate:self];
[self setTheAudio:myPlayer];
[myPlayer release];
As opposed to how you've implemented it, which assigns the player directly the instance variable:
theAudio = [[AVAudioPlayer alloc] initWith...
Because you assign the player directly to your instance variable, your player isn't being retained. This is likely to result in either premature release and a dangling pointer if you try and balance your calls properly, or it will result in leaking.

Display a webpage inside a UIWebView

I try to display a simple webpage inside my UIWebView without a Nib. The problem is when I click on my buttun a new page blanck page appear but nothing is display.
Did I miss something?
- (void)loadView {
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.webView = web;
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [[[NSURL alloc] initWithString:urlAddress] autorelease];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[topView addSubview:self.webView];
[web release];
}
thank you,
If this is the exact code you're using then it can't work: the webView added to topView that is never put on screen anywhere.
You probably want to add the webView to the controller view, but a better place to do that might be viewDidLoad, where self.view can be used safely.
This code works for me:
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[[UIWebView alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [[[NSURL alloc] initWithString:urlAddress] autorelease];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view addSubview:self.webView];
}
Using the Storyboard -
Step 1: Drag and drop a UIWebView in the View
Step 2: Then go to the .h file (for that particular view) and create an IBOutlet of a UIWebView. For example-
// MainViewController.h
#interface MainViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *myWebView;
#end
Step 3: Go to the storyboard and create a connection from outlet, myWebView (This can be found in the inspector area of Xcode) to the UIWebView by doing a control drag.
Step 4: Now when we have the connection, we just need to go to the .m (for that particular view) and add the following code -
//MainViewController.m
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlNameInString = #"https://www.google.com";
NSURL *url = [NSURL URLWithString:urlNameInString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:urlRequest];
}
#end
Here is My Solution.
Create a ViewController with WebView in Interface Builder and connect webview as IBOutlet
This code is simple and works fine
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *request = [NSURLRequest requestWithURL:#"http://www.google.com.ua"];
[webView loadRequest:request];
}