webView ShouldStartLoadRequest method not working properly - ios

I am loading a webview with some url using:
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://abc-ecatalogue.xyz-ar.com/"]]];
then i am checking if the home page is being loaded then i dont want the navigation bar for this i am using:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
// Here you can check the URL
NSURL *url = [request URL];
NSString*str=#"http://abc-ecatalogue.xyz-ar.com";
if ([url.absoluteString isEqualToString:#"http://havells-ecatalogue.adstuck-ar.com/"])
{
self.navigationController.navigationBarHidden = YES;
NSLog(#"hello");
return NO;
}
else
{
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:self action:#selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
}
return YES;
}
but when the control goes inside the loop the web view is not loaded and the log which i have printed is continously printed.

You have self.navigationController.navigationBarHidden = NO; in both the if & else part. You should change it to YES in the if section.EDIT:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
// Here you can check the URL
NSURL *url = [request URL];
NSString*str=#"http://abc-ecatalogue.xyz-ar.com";
if ([url.absoluteString isEqualToString:#"http://havells-ecatalogue.adstuck-ar.com/"]) {
self.navigationController.navigationBarHidden = YES;
NSLog(#"hello");
} else {
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:self action:#selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
}
return YES;
}

Related

display the images and audio

I am developing an app.I have a problem while developing the app.
The problem is:
I have a UIImageView and audio.I have 20 images in an array .so i need to display the 20 images in this UIImageView within time interval.Along with the image i need to set the audio.I need to view in same time, ie 20 images and audio should come at same time.
my code as below.
alphabetsStoreArray = [[NSMutableArray alloc]initWithObjects:#"a.png",#"b.png",#"c.png",#"d.png",#"e.png",#"f.png",#"g.png",#"h.png",#"i.png",#"j.png",#"k.png",#"l.png",#"m.png",#"n.png",#"o.png",#"p.png",#"q.png",#"r.png",#"s.png",#"t.png",#"u.png",#"v.png",#"w.png",#"x.png",#"y.png",#"z.png", nil];
commonFunctionObject = [[SpeechCommonFunctions alloc]init];
commonFunctionObject.isRecordComplete = NO;
counter = 0;
isMicPresent = YES;
_confirmationPopupView.hidden = true;
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(repeatActionFire) userInfo:nil repeats:NO];
// Do any additional setup after loading the view.
}
-(void)repeatActionFire
{
if(counter >= alphabetsStoreArray.count)
{
NSLog(#"finished");
[_alphabetsShowImageView removeFromSuperview];
[_speakerOrMicImageView removeFromSuperview];
UIImageView *congratzView = [[UIImageView alloc]initWithFrame:self.view.frame];
congratzView.image = [UIImage imageNamed:#"congratulation.png"];
[self.view addSubview:congratzView];
[self performSelector:#selector(navNew) withObject:nil afterDelay:3];
}
else
{
[commonFunctionObject textToSpeechAction:alphabetsStoreArray :counter :_alphabetsShowImageView :_speakerOrMicImageView :isMicPresent];
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(ActionToCkeckRecordCompletion) userInfo:nil repeats:YES];
}
}
-(void)ActionToCkeckRecordCompletion
{
if(commonFunctionObject.isRecordComplete)
{
_confirmationPopupView.hidden = false;
}
}
-(void)navNew
{
v11=[self.storyboard instantiateViewControllerWithIdentifier:#"maincontroller"];
[self presentViewController:v11 animated:NO completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
} */
-(IBAction)homebut:(id)sender{
UIAlertController * alert=[UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* Home = [UIAlertAction
actionWithTitle:#"RETURN TO HOME"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
v11=[self.storyboard instantiateViewControllerWithIdentifier:#"maincontroller"];
[self presentViewController:v11 animated:NO completion:nil];
}];
UIAlertAction* Cancel = [UIAlertAction
actionWithTitle:#"CANCEL"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:Cancel];
[alert addAction:Home];
[self presentViewController:alert animated:YES completion:nil];
}
- (IBAction)playButtonAction:(id)sender
{
[commonFunctionObject recordPlayAction];
}
- (IBAction)nextButtonAction:(id)sender
{
counter+=1;
_confirmationPopupView.hidden = true;
commonFunctionObject.isRecordComplete = NO;
if(commonFunctionObject.player.playing){[commonFunctionObject.player stop];}
[self repeatActionFire];
}
- (IBAction)retryButtonAction:(id)sender
{
_confirmationPopupView.hidden = true;
commonFunctionObject.isRecordComplete = NO;
if(commonFunctionObject.player.playing){[commonFunctionObject.player stop];}
[self repeatActionFire];
}
how to do?
Try this, May be you need more than this, but here is a start to get you in the right direction.
-(IBAction)btnclicked:(id)sender {
NSMutableArray *images = [[NSMutableArray alloc]init];
//all alloc does is allocate memory. The -init method is crucial to
actually initialize the object into a working state.
for (int imagenumber = 1; imagenumber <= 45; imagenumber++) {
NSMutableString *myString = [NSMutableString stringWithFormat:#"%i.png", imagenumber];
[images addObject:[UIImage imageNamed:myString]];
}
CGRect frame = CGRectMake(0.0, 0.0, 320, 460);
UIImageView *imageview = [[UIImageView alloc] initWithFrame:frame];
imageview.contentMode = UIViewContentModeScaleToFill;
NSURL *url = [NSURL fileURLWithPath:[NSString
stringWithFormat:#"%#/giggity stick around.wav", [[NSBundle mainBundle]
resourcePath]]];
soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:nil];
soundPlayer.volume = 1;
[soundPlayer play];
imageview.animationImages = images;
imageview.animationDuration = 3.2;
imageview.animationRepeatCount = 1;
[imageview startAnimating];
[self.view addSubview:imageview];
}
Use the AVFoundation to play the audio.

How to load cordova webview in my viewcontroller?

My cordova library was old and had to update it because of iOS11. I have updated it to latest version 4.4.0. CDVViewController is totally different now which enables webviewEngine that takes UIWebview or WKWebview. How do I initiate Cordova webview in my native app. Previously, I loaded it like this -
- (void) viewDidLoad {
[super viewDidLoad];
CDVViewController *viewController = [CDVViewController new];
viewController.wwwFolderName = #"www";
viewController.startPage = #"blank.html";
viewController.view.frame = self.webView.frame;
[self.webView removeFromSuperview];
self.webView = nil;
[self.view addSubview:viewController.view];
self.webView = viewController.webView;
viewController.webView.delegate = self;
self.cordovaWebViewController = viewController;
self.webView.scalesPageToFit = YES;
if (self.url)
[self.webView loadRequest: [NSURLRequest requestWithURL: self.url]];
}
And used webview delegates as
#pragma mark Webview Delegate
- (void) webViewDidStartLoad: (UIWebView *) webViewLocal {
[self.spinner startAnimating];
[self.cordovaWebViewController webViewDidStartLoad:webViewLocal];
}
- (void) webViewDidFinishLoad: (UIWebView *) webViewLocal {
[self.spinner stopAnimating];
[self.cordovaWebViewController webViewDidFinishLoad:webViewLocal];
}
- (void) webView: (UIWebView *) webView didFailLoadWithError: (NSError *) error {
[self.spinner stopAnimating];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([request.URL.absoluteString hasSuffix: #"link_restart_movie_file"]) {
[self loadItem];
return NO;
}
[self.webView loadRequest: request];
return NO;
}
[self.cordovaWebViewController webView:self.webView shouldStartLoadWithRequest:request navigationType:navigationType];
return YES;
}
How do I use WebviewEngine to load in my viewcontroller? Is there any tutorial, that explains the usage of CDVViewController? Please help! Thanks!

Unable to show the activity indicator iOS

I can't get to display the activity spinner on UIWebView
Need to show UIActivityIndicatorView while the page is being loaded.
UIActivityIndicatorView is declared but it's never shown on the UIWebView
Please assist.
The code is as follows :
#import <QuartzCore/QuartzCore.h>
#import "MyScreen"
#import "Reachability.h"
#interface MyScreen ()
#end
#implementation MyScreen
{;
UIView *loadingView;
}
#synthesize gotoMainMenu;
- (void)viewDidLoad {
[super viewDidLoad];
loadingView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
loadingView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
loadingView.layer.cornerRadius = 5;
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(loadingView.frame.size.width / 2.0, 35);
[activityView startAnimating];
activityView.tag = 100;
[loadingView addSubview:activityView];
UILabel* lblLoading = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, 80, 30)];
lblLoading.text = #"Loading...";
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont fontWithName:lblLoading.font.fontName size:15];
lblLoading.textAlignment = NSTextAlignmentCenter;
[loadingView addSubview:lblLoading];
[self.view addSubview:loadingView];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:true];
self.navigationController.navigationBar.hidden=false;
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus != NotReachable)
{
NSLog(#"Network is Available");
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Network Error" message:#"Please check internet connectivity!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil , nil];
[alertView show];
[self.navigationController popViewControllerAnimated:YES];
}
}
-(id) init
{
if([super init])
{
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, -64.00, self.view.frame.size.width, self.view.frame.size.height+64.0)];
webView.scalesPageToFit = YES;
webView.userInteractionEnabled = YES;
url = [[NSURL alloc] initWithString:#"http://theURL/"];
req = [[NSURLRequest alloc] initWithURL:url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:5.0];
[webView loadRequest:theRequest];
[webView loadRequest:req];
[self.view addSubview:webView];
}
return self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[loadingView setHidden:YES];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[loadingView setHidden:NO];
}
#end
You should not use "self.view" in your init function. When you use it, the view is loaded and viewDidLoad will be invoked before you add webView. So you webView is added after your loading view.
Just try to move this line:
[self.view addSubview:webView];
After:
[self.view addSubview:loadingView];

Ultra beginner. Issues with hiding UIBarButton

I dived into learning apps development couple days ago and faced my first problem. Basically what I'm trying achieve is to hide and disable back button when there is no UIWebView history. Here is my code:
-(IBAction)backButtonPressed:(id)sender {
[webView goBack];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:#"http://nearom.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
}
- (void) backButton{
if(webView.canGoBack == YES)
{
back.enabled = YES;
back.width = 0;
back.title = #"back";
}
else {
back.enabled = NO;
back.width = 0.01;
back.title = nil;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView{
[self backButton];
}
But backButton method isn't working. Could anybody explain what I'm doing wrong?
You might want to add more of your code. The only strange thing I have seen is that you use a parameter width. What do you want to do with that?
What I can not see from your code is if you have assigned the webviewDelegate to self so that webViewDidFinishLoad gets called.
Assuming you have a Navigation Controller, you should do the following:
self.navigationController.navigationItem.hidesBackButton = TRUE;

UIAlertView dismiss on return

I'm having a problem with my search alert when using the search-tab. When someone is typing their search-string and hit return it will only dismiss the keyboard and you still have to tab the "Search" (Zoek) button do actually start a websearch.
Here is my code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
[searchview addSubview:indicator];
NSString *urlAddress= #"http://www.sportdirect.com/articles?search=";
urlAddress =[urlAddress stringByAppendingString:inputText];
NSURL *url= [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
[searchview loadRequest:requestObj];
[[searchview scrollView] setBounces: NO];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
[TestFlight passCheckpoint:#"Voerde zoekopdracht uit"];
}
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[_message dismissWithClickedButtonIndex:(0) animated:YES];
NSString *inputText = [[_message textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
[searchview addSubview:indicator];
NSString *urlAddress= #"http://www.sportdirect.com/articles?search=";
urlAddress =[urlAddress stringByAppendingString:inputText];
NSURL *url= [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
[searchview loadRequest:requestObj];
[[searchview scrollView] setBounces: NO];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loading) userInfo:nil repeats:YES];
[TestFlight passCheckpoint:#"Voerde zoekopdracht uit"];
}
[textField resignFirstResponder];
return NO; // We do not want UITextField to insert line-breaks.
}
Does anyone know how to actually do a search as soon as the return button on the keyboard is tabbed?
Thanks in advance!
Your class needs to implement the UITextFieldDelegate to get the keyboard return callback. As suggested by borrden, this can be done as follows
[message textFieldAtIndex:0].delegate = self;
Then implement this method,
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Dismiss the alert view
// Initiate the search
return YES;
}
Use this UIAlertView method. Refer Apple docs
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
Call this on your alert view instance when required to dismiss it programmatically. You would require to keep message as an ivar to access it when required.
Hope that helps!
You can do this in UITextField delegate method:
your class must implement UITextFieldDelegate. set textField.delegate = self;
After implement this method:
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
// here you can write what you want, for example search
[textField resignFirstResponder];
return NO; // We do not want UITextField to insert line-breaks.
}

Resources