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,
Related
I am working on a native cum web iOS app. When i try to load Urls in UIWebView on any button press inside WebView i am unable to get the next URL to be loaded. Can anyone suggest anything for this? Thanks in advance.
Here is my Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *myString = [[request URL] absoluteString];
[myString lowercaseString];
NSLog(#"%#",myString)
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:#"X"]!=nil;
if (hasReferer)
{
return YES;
}
else
{
// relaunch with a modified request
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:X forHTTPHeaderField:#"X"];
[request setValue:Y forHTTPHeaderField:#"Y"];
[loadingWebView loadRequest:request];
});
});
return NO;
}
return 0;
}
set delegate
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error;
see in this method what it returns
and make sure you are passing correct url ie stringByAddingPercentEscapesUsingEncoding
How can i get the alert in click of submit ?
i am using WebView..
Here is the code
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[webView setDelegate:self];
NSString *urlAddress = #“sssssss”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
Thanks in Advance..
Check this:
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType {
NSString *fragment, *scheme;
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
//1
[webView stopLoading];
fragment = [[request URL] fragment];
scheme = [[request URL] scheme];
if ([scheme isEqualToString: #"file"] && [self respondsToSelector: NSSelectorFromString(fragment)]) {
[self performSelector: NSSelectorFromString(fragment)];
return NO;
}
[[UIApplication sharedApplication] openURL: [request URL]];
}
return YES;
}
You need to set delegate to WebView.
Hope this helps.
1 - Add a IBAction to your submit button.
2 - Inside the method that gets called use UIAlertController to create a alert that will be displayed when the button is clicked.
I'm trying to override a url with the following code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlString = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_udazzWebView loadRequest:urlRequest];
NSLog(#"log");
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"log2");
//use NSURLRequest object request , to manage the request.
NSURL *urL=request.URL;
NSString *urlStr=[urL absoluteString];
NSLog(#"URLL %#",urlStr);
if([urlStr isEqualToString:#"PostPicPopUp"]){
NSLog(#"log3");
}
return YES;
}
Log2 doesn't appear in the console. I'm guessing it has something to do with replacing NSURLRequest in the viewDidLoad, but I don't know how to do it.
It seems to me you are just missing UIWebView delegates init like
- (void)initWebViewWithRect:(CGRect)rect {
self.webView = [[UIWebView alloc] initWithFrame:rect];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.webView.opaque=NO;
self.webView.userInteractionEnabled=YES;
self.webView.delegate=self;
[self cleanSubViews];
}
You UIWebView should be defined like
#interface MXMBaseWebView()<UIWebViewDelegate, UIScrollViewDelegate, MXMWebViewProgressDelegate> {
CAGradientLayer *shadowLayer;
MXMWebViewProgressView *_progressView;
MXMWebViewProgress *_progressProxy;
UIRefreshControl *_refreshControl;
}
#property(nonatomic, strong) UIWebView *webView;
#end
so you will do calls like
[self.webView loadRequest: [NSURLRequest requestWithURL:url]];
At this point everything should work properly.
I'm trying to use the UITextView delegate method(textView:shouldInteractWithURL:inRange:) to override the default behavior of links in my textView opening in Safari.
I know I should create the webView I want to instantiate and pass the URL to it, but I don't know how to pass the URL to the webView.
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
//Do something with the URL
NSURL *url = [[NSURL alloc] initWithString:self.sourceInformation.text.?];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
WebViewController *sourceWebViewController = [[self storyboard] instantiateViewControllerWithIdentifier:#"WebViewController"];
[sourceWebViewController.webView loadRequest:request];
sourceWebViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
return NO;
}
Is there some property on UITextView that hold the URL? I looked at the header file but didn't see one.
If anyone can help, thanks.
Try this,
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
[self performSelector:#selector(loadWebView) withObject:nil afterDelay:1] ;
return NO;
}
-(void)loadWebView
{
NSURL *url = [[NSURL alloc] initWithString:self.sourceInformation.text.?];
if([[UIApplicaiton sharedApplication] canOpenURL:url])
{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
WebViewController *sourceWebViewController = [[self storyboard] instantiateViewControllerWithIdentifier:#"WebViewController"];
[sourceWebViewController.webView loadRequest:request];
sourceWebViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
}
else
{
NSLog(#"Can not open the URL, check the URL") ;
}
}
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
}