Multiple UIwebViews on Xcode - ios

I have about +3 UIwebViews, how do I code each one to go to different URLs.
Here the code from my ViewController.m file
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=#"http://test.bithumor.co/test22.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
// 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
How can I keep the same exact code for each UIwebViews but for each one to go to different webpages?
Example:
UIwebView1 = http://example.com/1
UIwebView2 = http://example.com/2
UIwebView1 = http://example.com/3

You have two choices, you can create three instances of a UIWebView with a URL & request for each or you can re-use the same one and just keep re-assinging it's URL request then calling it again.
If you want to create three separate UIWebView instances you'll just do wha you have except twice more, though adding each one to your current view will just give you the last one being visible.
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=#"http://test.bithumor.co/test22.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url2=#"http://nextUrl";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
UIWebView *webview3=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url3=#"http://anotherNexturl";
NSURL *nsurl3=[NSURL URLWithString:url3];
NSURLRequest *nsrequest3=[NSURLRequest requestWithURL:nsurl3];
[webview3 loadRequest:nsrequest3];
Or, depending on how you're switching between your web views you can just re-assign the NSURLRequest and re use the same instance of the UIWebView
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=#"http://test.bithumor.co/test22.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
NSString *url2=#"http://nextUrl";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview loadRequest:nsrequest2];
NSString *url3=#"http://nextUrl";
NSURL *nsurl3=[NSURL URLWithString:url3];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl3];
[webview loadRequest:nsrequest3];

Related

How to load two urls in webview Together in iOS?

i am loading url in webview.
i have two types of data to display
1)text(html string)
2)url
here is my code
To display text
[self.webView loadHTMLString:[self.arr objectAtIndex:indexPath.row][#"text"] baseURL:nil];
To display url
NSString *str = [NSString stringWithFormat:#"%#%#",URL,strpdf];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
issue is when i loading both data,data is overwrite.
i want to display both data in webview
but right now only text or image is displaying.
what is solution for this.
My demo is below:
#import "ViewController.h"
#interface ViewController ()<UIWebViewDelegate>
#property(nonatomic, strong) UIWebView *webView;
#property (nonatomic, copy) NSString *html_str;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI ];
}
- (void)initUI {
_html_str = #"I love google..."; // the html text you want
CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
_webView = [[UIWebView alloc] initWithFrame:frame];
_webView.delegate = self;
NSString *str = [NSString stringWithFormat:#"http://chsezhsan195030.e-fae.cn/"];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
[self.view addSubview:self.webView];
}
#pragma mark - webview delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script_str = [NSString stringWithFormat:#"var body = document.getElementsByTagName('body')[0];\
var script = document.createElement('div');\
script.innerText = '12345678'%#;\
script.style.width = window.innerWidth + 'px';\
script.style.height = '300px';\
script.style.backgroundColor = '#eeeeee';\
body.appendChild(script); \
body.insertBefore(script, body.childNodes[0]);",_html_str];
NSString *function = [NSString stringWithFormat: #"var script = document.createElement('script');\
script.type = 'text/javascript';\
script.text = \'function myFunction() { \
%# };'\
document.getElementsByTagName('head')[0].appendChild(script);", script_str];
[self.webView stringByEvaluatingJavaScriptFromString:function];
[self.webView stringByEvaluatingJavaScriptFromString:#"myFunction()"];
}
#end
But you should attention most of the url is prevented for inject js code,especially the https url, so you can not insert the html text above the google's index page.

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
{
}

Load Different HTML in iOS UIWebView for iPhone and iPad

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.

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];
}

Screen Orientation Xcode 4.3 iOS5.1

I am trying to have this view rotate, so that when a youtube video is played, it can be viewed full screen.
The view is embedded inside both a navigation view controller, and a tabBar view controller.
Using Xcode 4.3 target deployment is iOS 5.1, testing on both a tethered iPhone 4, and the simulator on iPhone.
I have added:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
To my view controller, which should allow the screen to rotate based on the device orientation.
Also note: I have landscape enabled in my info settings, and verified in my plist.
This is my first time posting to stackoverflow. I hope the above is clear.
Here is the full .m for reference:
// Created by Jacob Topping on 12-03-20.
// Copyright (c) 2012 The Melon Inc. All rights reserved.
//
#import "WebOneViewController.h"
#interface WebOneViewController ()
#end
#implementation WebOneViewController
#synthesize webOne;
#synthesize rotateWeb;
#synthesize follow;
#synthesize earnestWeb;
#synthesize ronWeb;
#synthesize richardWeb;
#synthesize davidWeb;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSString *david = #"http://www.youtube.com/embed/YSbQ1SDwtgM";
NSURL *url7 = [NSURL URLWithString:david];
NSURLRequest *davidUrl = [NSURLRequest requestWithURL:url7];
[davidWeb loadRequest:davidUrl];
NSString *richard = #"http://www.youtube.com/embed/6jL_1kJ6pno";
NSURL *url6 = [NSURL URLWithString:richard];
NSURLRequest *richardUrl = [NSURLRequest requestWithURL:url6];
[richardWeb loadRequest:richardUrl];
NSString *ron = #"http://www.youtube.com/embed/E0EmDSg3YXY";
NSURL *url5 = [NSURL URLWithString:ron];
NSURLRequest *ronUrl = [NSURLRequest requestWithURL:url5];
[ronWeb loadRequest:ronUrl];
NSString *earnest = #"http://www.youtube.com/embed/oTG7JtdSpT8";
NSURL *url4 = [NSURL URLWithString:earnest];
NSURLRequest *earnestUrl = [NSURLRequest requestWithURL:url4];
[earnestWeb loadRequest:earnestUrl];
NSString *website3 = #"http://twitter.com/search/?src=hash&q=%23creb13";
NSURL *url3 = [NSURL URLWithString:website3];
NSURLRequest *requestUrl3 = [NSURLRequest requestWithURL:url3];
[follow loadRequest:requestUrl3];
NSString *website2 = #"http://themeloninc.com/The_Melon_Inc./app/Slider/demo/rotate.php";
NSURL *url2 = [NSURL URLWithString:website2];
NSURLRequest *requestUrl2 = [NSURLRequest requestWithURL:url2];
[rotateWeb loadRequest:requestUrl2];
NSString *website = #"http://www.youtube.com/embed/J2EWRG-epes";
NSURL *url = [NSURL URLWithString:website];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webOne loadRequest:requestUrl];
//<iframe width="560" height="315" src="http://www.youtube.com/embed/J2EWRG-epes" frameborder="0" allowfullscreen></iframe>
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320,1150)];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)viewDidUnload
{
[self setRichardWeb:nil];
[self setRonWeb:nil];
[self setEarnestWeb:nil];
[self setFollow:nil];
[self setRotateWeb:nil];
[self setWebOne:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#end

Resources