Load Different HTML in iOS UIWebView for iPhone and iPad - ios

I can get both iPhone and iPad to load the same HTML file, however I would like to load a more optimized page based on which device the program is loading on.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_webView loadHTMLString:html baseURL:baseURL];
// disable scrolling
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.bounces = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have tried to create a new outlet, but I seem to be running into overlapping code.
How can I load different HTML files for iPad and iPhone with a universal build?

Just use one web view. All you need is two different HTML files.
Then you code can be something like:
NSURL *url;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
url = [[NSBundle mainBundle] URLForResource:#"index-iphone" withExtension:#"html"];
} else {
url = [[NSBundle mainBundle] URLForResource:#"index-ipad" withExtension:#"html"];
}
This assumes you have both an index-iphone.html and a index-ipad.html file in your app bundle.

Related

iOS web browser Google search

The assignment was to take anything with a space in the search query, assume it is a search criteria, and run it through a Google programmatically. Here is the code:
NSString *URLString = textField.text;
if ([URLString rangeOfString:#" "].location > 0) {
NSString *searchString = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *searchURL =[NSURL URLWithString:[NSString stringWithFormat:#"http://google.com/search?q=%#",searchString]];
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:searchURL];
[self.webview loadRequest:searchRequest];
}
I'm sure it's sloppy, but I was just curious as to why it's not functional. I took it through the debugger, and everything up to "searchString" seems to go well. I'm at a loss.
Let me know if I need to add more code.
I created a project using Xcode 6.3, storyboards, and ran in the iPhone 6 simulator and it works just fine. Here are two screenshots from the simulator:
And here's the code I used. It's slightly different from yours and I also included my whole VC:
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UITextField *searchText;
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)googleItButtonPress:(id)sender {
NSString *searchString = self.searchText.text;
if ([searchString rangeOfString:#" "].location > 0) {
NSString *searchStringModified = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *searchURL =[NSURL URLWithString:[NSString stringWithFormat:#"http://google.com/search?q=%#",searchStringModified]];
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:searchURL];
[self.webView loadRequest:searchRequest];
}
}
#end
I'm also getting this when I print the value of my modified search string if I set a breakpoint for the debugger:
(lldb) po searchStringModified
google+stuff
Looks like you're good to go. In this case just use storyboards and make sure your UIWebView is connected to your VC code.
Not sure what happened, but I suffered a case of spontaneous resolution. Thanks, everyone, for your time and advice.
Check this thing this work fine for me
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *URLString = #"HEllo World";
if ([URLString rangeOfString:#" "].location > 0) {
NSString *searchString = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *searchURL =[NSURL URLWithString:[NSString stringWithFormat:#"http://google.com/search?q=%#",searchString]];
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:searchURL];
[webview loadRequest:searchRequest];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"Started Loading");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}

Audio doesn't play in iOS 8

I have this code to get the audio inside my project and supposed to play audio:
#import "ViewController.h"
#import <AudioToolbox/AudioServices.h>
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self playAudio];
}
- (void)playAudio {
[self playSound:#"Splash" :#"wav"];
}
- (void)playSound :(NSString *)fName :(NSString *) ext{
SystemSoundID audioEffect;
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
NSURL *pathURL = [NSURL fileURLWithPath: path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else {
NSLog(#"error, file not found: %#", path);
}
}
#end
I'm using iOS 8 with iPhone 4s simulator, and this code doesn't work and I not hear the audio, why?

Locate a html file in UIWebView

I have a NotificationVC containing table view having 10 cells index 0 to 9. on click of each cell NotificationWebVC should open up that has a UIWebView that loads diff. html depending upon which cell is selected in NotificationVC. Right now my code works only for index=0 , but not for other indexes , seems bit weird.
NotificationWebVC
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:#"html" inDirectory:nil] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
Please suggest. _resourceName is a property having the name of html file.
try this code
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:#"html" inDirectory:nil] ;
[_webView loadHTMLString:htmlFile baseURL:nil];
_webView.delegate=(id)self;
First you need to make an array containing your 10 urls. Then when a user tap on each cell, use the didSelectRowAtIndexPath delegate method.
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:[resources objectAtIndex:indexPath.row] ofType:#"html" inDirectory:nil] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
Add line of code to NotificationWebVC.h
#property (nonatomic,retain)NSString *resopnseString;
then in .m class synthise it
#synthesize resopnseString;
after this go to didSelectMethod of tablevew datasouce in NotificationVC.m
there you add the following code
NotificationWebVC *notif=[[NotificationWebVC alloc]init];
notif.resopnseString=[resources objectAtIndex:indexPath.row];
//here it may present view or push view or what ever it be
After doing all this now you shift to NotificationWebVC.m
add the following code to in viewDidLoad method
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:resopnseString ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
after doing all this pl z be sure to add all 10 HTML file in your project
and try to check that you are getting correct html file on select in table from
be sure you are passing correct name of file in responsString
[resources objectAtIndex:indexPath.row];
or better put
NSLog(#"%#",[resources objectAtIndex:indexPath.row]);
to it check it.
i think this code will help you
// docfileName should be your file name call this method in tableview delegate didSelectrow
-(void)loadLocaldataonWebview :(NSString *)docfileName
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:docfileName ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
}
then in viewDidload add the following code
-(void)viewDidLoad{
documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, yAxis, kWidth, 616);
documentsWebView.backgroundColor=[UIColor clearColor];
[self.view addSubview:documentsWebView];
}

error when add HTML path in ios

I have this error:
"No visible #interface for 'NSBundle declares the selector'pathforresource: of type" .
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize WebView1;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *localFilePath = [[NSBundle mainBundle] pathForResoursce:#"www/index" ofType:#"html"] ;
NSURLRequest *localRequest = [NSURLRequest requestWithURL:
[NSURL fileURLWithPath:localFilePath]] ;
[WebView1 loadRequest:localRequest] ;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface ViewController : UIViewController
#property (nonatomic,retain) IBOutlet UIWebView *WebView1;
#end
Check the name of the function because you misspelled:
NSString *localFilePath = [[NSBundle mainBundle] pathForResoursce:#"www/index" ofType:#"html"] ;
should be :
NSString *localFilePath = [[NSBundle mainBundle] pathForResource:#"www/index" ofType:#"html"] ;
And then check the name of the html file . It might be index.html.
If you have difficulties in finding the problem you said pathForResoursce: instead of pathForResource:

My buttons play all the same sound?

Here is my MainViewController.m
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize audioPlayer;
#synthesize soundsArray;
-(void)prepareSounds
{
NSString *filepath= [[NSBundle mainBundle] pathForResource:#"Sounds" ofType:#"plist"];
self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
}
- (IBAction)playSound:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
- (IBAction)playSound2:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -2)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
- (IBAction)playSound3:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -3)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
- (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.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
#end
My MainViewController.h
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
AVAudioPlayer *audioPlayer;
NSArray *soundsArray;
}
#property(nonatomic, retain) AVAudioPlayer *audioPlayer;
#property(nonatomic, retain) NSArray *soundsArray;
-(void)prepareSounds;
- (IBAction)playSound:(id)sender;
- (IBAction)playSound2:(id)sender;
- (IBAction)playSound3:(id)sender;
#end
In the 'Supporting Files' folder I have an array of strings with the name of the sound files I want to play, and in the 'Supporting Files' folder I have a folder named 'Sounds', which contains the sound files.
All of my buttons play the same sound. Can someone please provide some insight. Thanks!
I think the problem may be in this line playSound:
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)]
which is repeated in playSound2 and playSound3 with "buttonPressed.tag -2" and "buttonPressed.tag -3".
If your buttonPressed.tags are set to 1, 2, and 3, then each time "buttonPressed.tag -X" is likely evaluating to 0, and playing the sound of the first file in the array.
You are repeating the code to do the same task.
Add all your buttons IBAction to a single method (say it as playSound)
Implement the method like:
- (IBAction)playSound:(UIButton *)sender
{
NSString *soundName = [soundsArray objectAtIndex:(sender.tag -1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
There is no need of writing same code for each individual button.
SOLVED: prepareSounds() was never called. Here is the working code:
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize audioPlayer;
#synthesize soundsArray;
-(void)prepareSounds
{
NSString *filepath= [[NSBundle mainBundle] pathForResource:#"Sound" ofType:#"plist"];
self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
}
- (void)stopAudio
{
if (audioPlayer!= nil) {
[audioPlayer stop];
//do some task for changing the Image i.e setting the default image
}
}
- (IBAction)playSound:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
NSString *soundName = [soundsArray objectAtIndex:(btn.tag - 1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
if([audioPlayer isPlaying])
{
[self stopAudio];
}
[self.audioPlayer play];
}
- (void)viewDidLoad
{
[self prepareSounds];
[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.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
#end

Resources