- (BOOL)webview: doesn't run in objective c xcode - objective-c

I am trying to get this to run... I have put in NSLogs, but nothing appears in the debugger when I change page.
Here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize webview;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webview.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"menu" ofType:#"html" inDirectory:#"pages"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
NSLog(#"%#",[[request URL] absoluteString]); //this log appears when the ipad simulator starts
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
This section below doesn't seem to run (no log appears in the debugger!)
- (BOOL)webview:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"%#",[[request URL] absoluteString]);
NSString *absoluteUrl = [[request URL] absoluteString];
if ([absoluteUrl isEqualToString:#"didtap://button1"]) {
NSLog(#"Button tapped");
return NO;
}
return YES;
}

Check the capitalization of your delegate method; it should be:
- (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
i.e. webView, not webview.

Related

Objective C - When tapping the tab bar item again, how to reload web view again?

I really need help please. Initially, by tapping on a tab bar item, I will load a webview on Gym VC user will then scroll down the web view
What I am trying to accomplish is when user tap on the same tab bar item again, the webView will reload again. I managed to call the method handleRefresh from MyTabBarController But the method doesn't do anything at all. Any guidance is much appreciated.
Currently I have the following code
MyTabBarController.m
#import "GymNavigationController.h"
#import "Gym.h"
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[GymNavigationController class]]){
Gym *myGym = [[Gym alloc] init];
[myGym handleRefresh:nil];
}
}
Gym.m
#import "Gym.h"
#import "AppDelegate.h"
#import "GymDet.h"
#import <QuartzCore/QuartzCore.h>
#interface Gym () {
AppDelegate *appDelegate;
NSString *sURL, *sRefresh;
}
#end
#implementation Gym
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp?"];
NSLog(#" The sURL to be load for the current page : %# ", sURL);
sRefresh = sURL;
[defaults setObject:sRefresh forKey:#"txtRefresh"];
[defaults synchronize];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//[self handleRefresh:nil];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
//--- Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sRefresh = [defaults objectForKey:#"txtRefresh"];
NSURL *url = [NSURL URLWithString:sRefresh];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
First i would like to suggest why you're initiating a new Gym object every time tabbar is tapped you should take a reference of the Gym object and initialise it if its nil then process ahead with the reference, but it maybe your requirement and if it is i would like to suggest you to create a BOOL variable lets say isUpdateRequire and when you're instantiating your viewcontroller assign isUpdateRequire = true. Then in the end of view did load or view will appear (according to your need) check
if isUpdateRequire {
[self handleRefresh:nil]
}
Alternatively you can create protocols in your webviewcontroller and assign its delegate to your tabbarcontroller and fire the delegate method when ever required.
and if you don't want the method to call when you come back to vc simply put this condition in viewWillAppear
if self.isMovingToParentViewController {
[self handleRefresh:nil]
}

AVAudioPlayer iOS no sound

Seems to be similar questions asked but none of the solutions seem to work for me. Cant for the life of me understand why my code doesnt work so here it is.
I dont get any errors but also get no sound.
#interface MainApp ()
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
-(void)playSoundFX:(NSString*) soundFile;
#end
#implementation MainApp
- (void)viewDidLoad {
[super viewDidLoad];
// Play sound
[self playSoundFX:#“sound.mp3"];
}
// Play sound
- (void)playSoundFX:(NSString*)soundFile {
// Setting up path to file url
NSBundle* bundle = [NSBundle mainBundle];
NSString* bundleDirectory = (NSString*)[bundle bundlePath];
NSURL *url = [NSURL fileURLWithPath:[bundleDirectory stringByAppendingPathComponent:soundFile]];
// Make an audioPlayer object and play file
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer setVolume:100.0f];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
}
#end
*********vvvvvvvvv Amended Code that works vvvvvvvvv**********
#interface MainApp ()
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
-(void)playSoundFX:(NSString*) soundFile;
#end
#implementation MainApp
- (void)viewDidLoad {
[super viewDidLoad];
// Play sound
[self playSoundFX:#“sound.mp3"];
}
// Play sound
- (void)playSoundFX:(NSString*)soundFile {
// Setting up path to file url
NSBundle* bundle = [NSBundle mainBundle];
NSString* bundleDirectory = (NSString*)[bundle bundlePath];
NSURL *url = [NSURL fileURLWithPath:[bundleDirectory stringByAppendingPathComponent:soundFile]];
// Make an audioPlayer object and play file
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[self.audioPlayer setVolume:100.0f];
if (self.audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[self.audioPlayer play];
}
#end
I am execute your code on my iPhone. It is work properly.Please Select a device and run your code. I hope it will execute properly with sound.
You got to segregate the functionalities..
Loading audio file in viewDidLoad and play it on play action, some thing like this
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
{
AVAudioPlayer *_audioPlayer;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:#"%#/sound.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playSoundTapped:(id)sender
{
// When button is tapped, play sound
[_audioPlayer play];
}

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.

UIWebView not opening requested url from other view controller Objective C

mainViewController.h
#import "ECSlidingViewController.h"
-(void)Loadwebview:(NSURL *)url;
mainViewController.m
-(void)Loadwebview:(NSURL *)url
{
// NSURL *testURL = [NSURL URLWithString:#"http://www.google.com"];
NSLog(#"loadwebview: %#", url);
[webViewBox loadRequest:[NSURLRequest requestWithURL:url]];
}
leftViewController.h
#import "mainViewController.h"
#import "ECSlidingViewController.h"
leftViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlString = [[dao libraryItemAtIndex:indexPath.row] valueForKey:#"url"];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"]; //urlString];
NSLog(#"selected url: %#", url);
mainViewController *mvc = [[mainViewController alloc]init];
[mvc Loadwebview:url];
//NSLog(#"%#", [[dao libraryItemAtIndex:indexPath.row] valueForKey:#"url"]);
[self.slidingViewController resetTopView]; //it will show mainViewController
}
This code is not working... OutPut steps are,
Log shows :
Selected url: http://www.google.com
loadwebview: http://www.google.com
which means I am passing url from leftViewController.m to mainViewController.m and calling loadwebview function properly. But the issue is that webViewBox is not opening requested url.
:-(

Iphone App crashing on launch

My simple iphone app is crashing on launch, it says "the application downloadText quit unexcpectedly" None of these windows that pop up when a mac app crashes and has a send to Apple button. My .h is below and I would greatly appreciate it if anyone could give me a hand as to what's wrong?
#import "downloadTextViewController.h"
#implementation downloadTextViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSString *myPath = [self saveFilePath];
NSLog(myPath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists)
{
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
textView.text = [values objectAtIndex:0];
[values release];
}
// notification
UIApplication *myApp = [UIApplication sharedApplication];
// add yourself to the dispatch table
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:myApp];
[super viewDidLoad];
}
- (IBAction)fetchData {
/// Show activityIndicator / progressView
NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://simpsonatyapps.com/exampletext.txt"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:1.0];
NSURLConnection *downloadConnection = [[NSURLConnection alloc] initWithRequest:downloadRequest delegate:self];
if (downloadConnection)
downloadedData = [[NSMutableData data] retain];
else {
/// Error message
}
}
- (void)connection:(NSURLConnection *)downloadConnection didReceiveData:(NSData *)data {
[downloadedData appendData:data];
NSString *file = [[NSString alloc] initWithData:downloadedData encoding:NSUTF8StringEncoding];
textView.text = file;
/// Remove activityIndicator / progressView
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
}
- (NSString *) saveFilePath
{
NSArray *pathArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:#"savedddata.plist"];
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSArray *values = [[NSArray alloc] initWithObjects:textView.text,nil];
[values writeToFile:[self saveFilePath] atomically:YES];
[values release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
#end
Edit:
The console comes up with:
21/03/10 2:32:19 PM downloadText[3548] Stack:
( 8307803, 2474450491, 8466881, 2787944, 2786485, 25429108, 8210735, 25423659,
25431927, 24117515, 24111079, 24110797, 8337, 23594443, 23632310, 23620404,
23602815, 23629921, 134489, 8092544, 8088648, 23596565, 23633839, 8252 )
Check the Console for exception reports. [Spotlight search for "Console" application if you're lazy. ;-] Any stack trace there to provide clues?
Run in the simulator in debug mode. Set a breakpoint at the start of viewDidLoad, plus anywhere in your code from any stack trace left in the Console.
Does putting the call to "[super viewDidLoad]" first (instead of last) help? If that's doing any work, it might be tripping up your viewDidLoad. Check the Console output first though -- I tend to make code changes only when I understand what's going wrong, or I can use it to rule out potential causes.