Basic url not working in WebView - ios

I am learing WebView.
when I do something like
NSString *url = #"www.google.com";
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
it fails.
But when I type www.google.com (or even google.com) in standard browser, it works fine.
I also noticed that after loading the page, the url text field in standard browser changes the link from www.google.com to https:// www. google.co.in/?gws_rd=ssl
In above code when I set NSString *url = #"https://www.google.co.in/?gws_rd=ssl" it works fine
So how do I implement my WebView view so that if should work like a standard browser it terms of above context

UIWebView always needs http or https when starting request, if you click on links inside the webView it will handle it itself. So here is how to handle it:
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *addressBar = [[UITextField alloc] init];
[addressBar setDelegate:self];
[self loadRequestFromString:#"www.google.com"];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self loadRequestFromString:textField.text];
[textField resignFirstResponder];
return YES;
}
- (void)loadRequestFromString:(NSString *)urlString
{
NSURL *webpageUrl;
if ([urlString hasPrefix:#"http://"] || [urlString hasPrefix:#"https://"]) {
webpageUrl = [NSURL URLWithString:urlString];
} else if ([urlString containsString:#" "] || ![urlString containsString:#"."]) {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com/search?q=%#", [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
} else {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", urlString]];
}
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
[_webView loadRequest:urlRequest];
}

to open any URL in UIWebView you should add url with protocol
eg #"http://www.google.com".
[NSURL URLWithString:#"http://www.google.com"];
then it will work.

Related

Read and Redirect webpage by NFC tag response in objective c

Im struggled with redirect to webpage after read NFC scan by objective C.
Here is my code
- (void) readerSession:(nonnull NFCNDEFReaderSession *)session didDetectNDEFs:(nonnull NSArray<NFCNDEFMessage *> *)messages {
for (NFCNDEFMessage *message in messages) {
for (NFCNDEFPayload *payload in message.records) {
NSLog(#"Payload data:%#",payload.payload);
}
}
}
I got solution by using VYNFC kit,
https://github.com/vinceyuan/VYNFCKit
if ([parsedPayload isKindOfClass:[VYNFCNDEFURIPayload class]]) {
text = #"[URI payload]\n";
text = [NSString stringWithFormat:#"%#%#", text, ((VYNFCNDEFURIPayload *)parsedPayload).URIString];
urlString = ((VYNFCNDEFURIPayload *)parsedPayload).URIString;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
}

How can i get the alert in click of submit?

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.

shouldStartLoadWithRequest not working in iOS

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.

How do I extract a URL within a UITextView in Xcode?

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") ;
}
}

Get current URL in UIWebview but it not respond

I create app to use UIWebview and show log to url if touch in content in UIWebview.
this is my code
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url1 =[NSURL URLWithString:#"MyWebSite"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
[_webView1 loadRequest:request1];
NSURL *url2 =[NSURL URLWithString:#"MyWebsite2"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
[_webView2 loadRequest:request2];//url menu 2
NSString *currentURL = [_webView1 stringByEvaluatingJavaScriptFromString:#"document.title"];
NSLog(#"%#",currentURL);
}
but I touch the content log is not print and change webview log is print (null)
sorry for my poor English.
You can not get title of a website before you receive data from the URL.
So
Set your webview delegate to self
Then in
- webViewDidFinishLoad: to get title

Resources