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.
Related
I am rookie to iOS. In my project i need to add thumbnail image of web pages in image view.I created a NSObject class for that. I initiated web view in that class but it won't call the UIWebView Delegates.I posted the code here. If any one have solution or the library file for thumbnail processing that might be useful.Thank you.
#interface ThumbNail()<UIWebViewDelegate>
{
UIImageView *thumbNailImageView;
UIWebView *mockUpWebView;
UIImage *viewImage;
}
#end
#implementation ThumbNail
-(void)webViewDidFinishLoad:(UIWebView *)webView {
thumbNailImageView.image = [self captureScreen:webView];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(#"Errorrr.............");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Got it");
return YES;
}
-(UIImage*)captureScreen:(UIWebView*) viewToCapture{
CGSize overallSize = CGSizeMake(mockUpWebView.frame.size.width, mockUpWebView.frame.size.width);
UIGraphicsBeginImageContext(viewToCapture.scrollView.contentSize);
viewToCapture.bounds = CGRectMake(0, 0, overallSize.width, overallSize.height);
while (viewToCapture.loading) {
[NSThread sleepForTimeInterval:0.1];
}
[viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
-(void)getThumbNailForImageView:(UIImageView *)imageView{
thumbNailImageView = imageView;
NSString *urlAddress = #"https://in.yahoo.com/?p=us";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
CGRect bounds = [[UIScreen mainScreen] bounds];
mockUpWebView = [[UIWebView alloc]initWithFrame:bounds];
NSLog(#"bounds = %#",NSStringFromCGRect(bounds));
mockUpWebView.delegate = self;
[mockUpWebView loadRequest:requestObj];
}
#end
I am trying to get thumbnail image by calling getThumbNailForImageView.
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];
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
{
}
My code is working fine for iOS 7 but crashes in iOS 6. My app is working fine but the problem occurs when i click on a tab else its working fine.
Checked the link :EXC_BAD_ACCESS While switching tabControllers but no luck.
Below is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView = [[UIWebView alloc] init];
self.view.backgroundColor = [UIColor colorWithPatternImage:[JLTBackgroundHelper getBackgroundImageForView]];
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor clearColor];
UIViewController *rootVC = [[UIViewController alloc] init];
rootVC.title = #"Contact JLT Sport";
rootVC.view = self.webView;
self.viewControllers = #[rootVC];
NSString *fileName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad-specific
fileName = #"contact_ipad";
} else {
// iPhone-specific
fileName = #"contact";
}
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:#"html" inDirectory:#"Assets/web"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate= self;
}
//send external URL requests to Safari
- (BOOL) webView:(UIWebView*)wv shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navType
{
if (navType == UIWebViewNavigationTypeLinkClicked
&& [request.URL.scheme hasPrefix:#"http"])
{
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
#end
Plese guide/suggest.
try this :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView = [[UIWebView alloc] init];
self.view.backgroundColor = [UIColor colorWithPatternImage:[JLTBackgroundHelper getBackgroundImageForView]];
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor clearColor];
UIViewController *rootVC = [[UIViewController alloc] init];
rootVC.title = #"Contact JLT Sport";
rootVC.view = self.webView;
self.viewControllers = #[rootVC];
NSString *fileName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad-specific
fileName = #"contact_ipad";
} else {
// iPhone-specific
fileName = #"contact";
}
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:fileName ofType:#"html" inDirectory:#"Assets/web"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.delegate= self;
}
if above solution does not work then try this:
Debug with Instrument
Go to Product >> Profile >> All > Zoombies
Press "Record" button on top left corner
Perform your task and when application get EXC_BAD_ACCESS the instrument give that line where the application was being crash.
Also debug in this method to:[JLTBackgroundHelper getBackgroundImageForView];
**Make sure that all variable is being get memory before use it.**
May be the resin is that some method is depreciated in iOS 7.
These are the possible scenario of application is being crash and its solution.
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