UIWebView Not open a link but safari does - ios

I have tried to open a link in UIWebView it does not open and throw error.
Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x9a2b020 {NSErrorFailingURLKey=http://epapir.info/apotek1/1402/#/1/, NSErrorFailingURLStringKey=http://epapir.info/apotek1/1402/#/1/}
and show images
Please advise.It open in safari browser in iPhone and working well there.Following is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[activity stopAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}

try something like this..
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication]openURL:url];
}
USe this in the VoewDidappear method though

Just add an NSLog in your didFailLoadWithError and check
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[activity stopAnimating];
NSLog(#"ERROR : %#",error); //Get informed of the error FIRST
}

Related

Maps not being shown in webview- iOS

I have a button in my view. On clicking it leads to a webview in which I want to show the maps. The code I am using is:
- (void)viewWillAppear:(BOOL)animated{
NSString *url = [#"http://maps.apple.com/?q=" stringByAppendingString:self.address];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSURLRequest *metricsRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
_webViewForLocation.delegate = self;
[_webViewForLocation loadRequest:metricsRequest];
NSLog(#"Maps String: %#", url);
}
But the webview is blank. HOw do I show maps in it?
try
- (void)viewDidLoad
{
[super viewDidLoad];
[_webViewForLocation loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://maps.google.com/?q=bangalore"]]]]; // here pass your string
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
the output is
refers this document,

Tabbed bar application buttons blocked when NSUrlSendAssynchronousrequest operation running

I have a tabbed bar application in which one of the buttons loads the content of a webpage to a UIWebView.
The problem I am facing is that while the connection attempting to fetch the data the tab bar buttons are non responsive until either the page is done loading or connection has failed.
Is there a better way to perform this operation so the user can switch out of that tab if he/she changes their mind for example.
Thanks and here is the code:
- (void)loadThrillsSite
{
[self.webView setDelegate:self];
NSString *urlString = #"https://www.Thrills.in";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil) [self.webView loadRequest:request];
else if (error != nil) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Connection Error!" message:#"Failed to connect to server\nPlease check your internet connection and try again" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
[hudImageView removeFromSuperview];
[self.spinner stopAnimating];
[patternView removeFromSuperview];
}
UIWebView's loadRequest method description:
Connects to a given URL by initiating an asynchronous client request.
It already makes a asynchronous call so you do not need to use another block. Just call [self.webView loadRequest:request]; You can handle errors on - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error method.
Other notes: Your url is not working right now and do not do any UI related work like showing an alert view on background thread. Hope it helps.
- (void)loadThrillsSite
{
[self.webView setDelegate:self];
NSString *urlString = #"https://www.Thrills.in";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
[hudImageView removeFromSuperview];
[self.spinner stopAnimating];
[patternView removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Connection Error!" message:#"Failed to connect to server\nPlease check your internet connection and try again" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}

Throwing an error "request body stream exhausted" when submitting the form on .aspx from UIWebView

I am loading one .aspx web form on UIWebView in iPhone and the form is loading successfully. Now the form is having one choose photo button where users can select a photo/image from iPhone gallery and one submit button. After choosing a photo and when I click on submit button on the form it's throwing an error like "request body stream exhausted". I searched in google also but no luck.
Here is the code snippet
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[activity startAnimating];
activity.hidden = NO;
NSURL *url = [NSURL URLWithString:#"http://rest..com:95134/MobileForm.aspx?processName=NewForm"];
theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];//image/jpeg
[theRequest setValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];// "Content-Type" = "text/html; charset=utf-8";
[webView loadRequest:theRequest];
}
#pragma mark
#pragma mark connection Methods
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(#"got auth challange");
if ([challenge previousFailureCount] == 0) {
_authed = YES;
/* SET YOUR credentials, i'm just hard coding them in, tweak as necessary */
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:#"user" password:#"pass" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(#"received response via nsurlconnection %#",response);
/** THIS IS WHERE YOU SET MAKE THE NEW REQUEST TO UIWebView, which will use the new saved auth info **/
[connection cancel];
[webView loadRequest:theRequest];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
activity.hidden =YES;
NSLog(#"err %#", [error localizedDescription]);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Message" message:[error localizedDescription] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
return YES;
}
#pragma mark
#pragma mark webView Methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(#"Did start loading: %# auth:%d", [[request URL] absoluteString], _authed);
if (!_authed) {
_authed = NO;
/* pretty sure i'm leaking here, leave me alone... i just happen to leak sometimes */
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
return NO;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
activity.hidden = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
activity.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
activity.hidden = YES;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Message" message:[error localizedDescription] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
But I could able to upload an image and submit the form from Safari app on device. Please help me. Thanks in advance.

didFailLoadWithError being called for ios7 only

didFailLoadWithError being called only for iOS7.
This method is not called for iOS6.
The error logged is:
ERROR : Error Domain=NSURLErrorDomain Code=-999 "The operation
couldn’t be completed. (NSURLErrorDomain error -999.)"
UserInfo=0xcd0e200
{NSErrorFailingURLKey=bsp.gov.ph/statistics/sdds/exchrate.htm,
NSErrorFailingURLStringKey=bsp.gov.ph/statistics/sdds/exchrate.htm}
The error code -999 apparently is a URL Cancelled request but I am using the same code for iOS6 and works perfectly fine.
Why does it show an error for ios7?
The code I am using is:
- (void)viewDidLoad
{
[super viewDidLoad];
isFirstTime=TRUE;
ratesArray=[[NSArray alloc]initWithObjects:#"In",#"USD",#"JPY",#"GBP",#"HKD",#"CAD",#"SGD",#"AUD",#"SAR",#"THB",#"AED",#"CNY",#"KRW",#"EUR",#"MYR",#"TWD", nil];
NSLog(#"rates.count = %d",ratesArray.count);
defaults = [NSUserDefaults standardUserDefaults];
[self loadServerData];
}
-(void)loadServerData
{
GeneralClass *gen=[GeneralClass retrieveSingleton];
euroRatesArray =[[NSMutableArray alloc]init];
[euroRatesArray addObject:#"EURO"];
usRatesArray =[[NSMutableArray alloc]init];
[usRatesArray addObject:#"US"];
phpRatesArray =[[NSMutableArray alloc]init];
[phpRatesArray addObject:#"PHP"];
self.navigationController.navigationBar.hidden=TRUE;
if ([gen checkNetworkConnection] ) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//Create a URL object.
NSURL *url = [NSURL URLWithString:#"http://www.bsp.gov.ph/statistics/sdds/exchrate.htm"];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webPage loadRequest:requestObj];
} else {
defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:kExchanteRatesKey];
NSArray *contentArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
lblDate.text=[defaults objectForKey:kExchanteRateDate];
if (contentArray.count>0) {
for (int i=1; i<contentArray.count-1; i++) {
RateClass *rate=(RateClass *)[contentArray objectAtIndex:i];
[euroRatesArray addObject:(rate.text2.length>0 ? rate.text2:#"")];
[usRatesArray addObject:(rate.text3.length>0 ? rate.text3:#"")];
[phpRatesArray addObject:(rate.text4.length>0 ? rate.text4:#"")];
}
}
//NSLog(#"euroRatesArray = %#",euroRatesArray);
//NSLog(#"usRatesArray = %#",usRatesArray);
//NSLog(#"phpRatesArray = %#",phpRatesArray);
[activity setHidden:TRUE];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[self hideActivityBar];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[self hideActivityBar];
}
-(void) hideActivityBar
{
[self htmlParse];
}
-(void)htmlParse
{
//NSString *html = [webPage stringByEvaluatingJavaScriptFromString: #"document.getElementById('exchrate_28426').innerHTML"];
//NSString *html = [webPage stringByEvaluatingJavaScriptFromString: #"document.getElementsByClassName('xl6528426')[0].innerHTML"];
NSString *html = [webPage stringByEvaluatingJavaScriptFromString:#"document.body.innerText"];
NSArray *array=[html componentsSeparatedByString:#"\n"];
NSLog(#"array.count = %d",array.count);
int checkRateIndex=1;
int index=212;
NSUserDefaults *standardUserDefaults=[NSUserDefaults standardUserDefaults];
}

Setting a webview delegate for a segue controller?

I have two view controllers connected by a modal segue. The destination controller also has a UIWebView.
Reading the docs I would like to elect the first controller to be responsible for dismissing the webview controller and my action for doing so would be in a webview delegate method.
I'm trying this:
First controller:
#interface ABISignInViewController : UIViewController <UIWebViewDelegate>
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
...
} else if ([[segue identifier] isEqualToString:#"showWebViewForSignUp"]) {
ABIWebBrowserViewController *webViewController = (ABIWebBrowserViewController *)segue.destinationViewController;
[webViewController.webView setDelegate:self];
[webViewController setUrlString:#"https://myurl"];
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"%#", request);
return YES;
}
The second controller fires up and start loading the requested url:
- (void)viewDidAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:self.urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
However the delegate method in the first controller is never called.
Any directions?
Why not pass the URL as an NSString or an NSURL and then let the ViewController with the WebView handle the delegate methods?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
...
} else if ([[segue identifier] isEqualToString:#"showWebViewForSignUp"]) {
ABIWebBrowserViewController *webViewController = (ABIWebBrowserViewController *)segue.destinationViewController;
webViewController.urlString = #"https://myurl";
}
}
WebView ViewController:
- (void)viewDidAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:self.urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"%#", request);
return YES;
}

Resources