UIWebView and bookmarklet loading - iphone

I try to load bookmarklet (Readability) in UIWebView.
But I have some problems...
Could you tell me, what's wrong here?
The article loads, but not in readability.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *searchURL = #"(function(){window.baseUrl='https://www.readability.com';window.readabilityToke‌​n='';var s=document.createElement('script');s.setAttribute('type','text/javascript');s.se‌​tAttribute('charset','UTF-8');s.setAttribute('src',baseUrl+'/bookmarklet/read.js');document.documentElemen‌​t.appendChild(s);})()";
//NSURL *url = [NSURL URLWithString:[searchURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURL *article = [NSURL URLWithString:#"http://www.nytimes.com/2013/01/12/world/asia/us-can-speed-afghan-exit-obama-says.html?hp&_r=0"];
// NSURL *url = [NSURL URLWithString:searchURL];
[webview loadRequest:[NSURLRequest requestWithURL:article]];
//[webview loadRequest:[NSURLRequest requestWithURL:url]];
[webview stringByEvaluatingJavaScriptFromString:searchURL];
// Do any additional setup after loading the view from its nib.
}
I will be thankful for help!

Related

IOS - UIWebview - Get the URL of the link clicked and edit the url

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
First I initialize the webview with the following url:
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
When I click to a link, for example www.website.com/contact.php
Then I edit the new url with www.website.com/contact.php*?id=12212323*
I need to keep the id parameter in all the urls.
The UIWebViewDelegateProtocol should suit your requirements.
In particular the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
this is a code to get the current URL
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSURL *currentURL = [requestObj URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
but to create a URL bar hidden or not is a lot of code, you can start from this tutorial on YouTube is simple and work very well, is possible give you other idea to make a good job ;)
Web Browser Tutorial
hope this help you

Get updated url from uiwebview

I have a uiwebview I open http://google.com in it. Then I type something in google search bar and search for something. I want to get that updated url that is generated as a result of my search on google.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUrlAddress:#"http://google.com.pk"];
// Do any additional setup after loading the view from its nib.
NSURL *url = [NSURL URLWithString:self.urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[appDelegate showActivityView:self.view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[appDelegate hideActivityView];
}
Try this
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
Hope it will work for you
Below is a correct answer but its not perfectly working for Facebook and youtube
NSSTRing * currentURL = webView.request.URL.absoluteString;
If anyone knows a better answer, please share.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = [NSString stringWithFormat:#"http://www.google.com/pk];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
try this delegate method :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
you can get hold of request and nslog that request .

Getting application crash during opening a local pdf file on UIWebView

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType: #"pdf"];
NSLog(#"%#", path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_webView loadRequest:request];
}
just use following code for open PDF in web view
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
[self.webVctr loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
And check it out the PDF is exist or not?
This thinks may helping to you
Happy codeing..
Try this:
in the
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
It was some other issue not related to the pdf loading over web view. I cross checked and found the actual cause of crash. Anyways thanks guys for the help

uiwebview not loading the web page

in my program uiwebview is not loading the url address.when i nslogged the request object is not null.however when i nslog the webview.request it returns null.what may be the reason it is not loading
- (void)viewDidLoad {
[super viewDidLoad];
self.web = [[UIWebView alloc] init];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
NSLog(#"%#",requestObj);
[self.web loadRequest:requestObj];
[web setFrame:CGRectMake(0, 0, 320, 370)];
NSLog(#"%# %#",self.web,self.web.request);
}
the nslog results are
<NSURLRequest http://www.google.com>
<UIWebView: 0xbb17ff0; frame = (0 0; 320 370); layer = <CALayer: 0xbb12f20>> (null)
as per suggestions from this site i changed it from IB and made it to code .i made the class confirm to uiwebview delegate..both the webviewdidstart and webview did finish are being called these are the nslog outputs from these methods
webviewdidstart
webView-----><NSMutableURLRequest >
webView-----><NSMutableURLRequest http://www.google.com>
webview did finish
webView-finished----><NSMutableURLRequest http://www.google.com>
webView-finished----><NSMutableURLRequest http://www.google.com>
still nothing is being called and i think both these methods are called twice
This is a working code, test this out in your application and let us know the outcome
in .h file, add UIWebViewDelegate
in .m file's viewDidLoad, add this:
UIWebView *webview=[[UIWebView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,460.0)];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
webview.delegate = self;
[webview loadRequest:requestObj];
[self.view addSubview:webview];
Now check in the delegates if your page is loading or not:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"page is loading");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"finished loading");
}
try like this
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];

website not upload using webview

ihave write the following code
but it does nothing in consloe it shows Unknown scheme, doing nothing
[webview setDeleagte self];
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:sourcelinkStr]]];
NSString *strURL = #"https://facebook.com";
NSURL *url = [NSURL URLWithString:strURL];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:url]];
now ,
we have to define Methods For LoadRequest
thanks..