WebView not loading? - ios

I am new to Objective c. I am learning about segue's and PrepareForSegue. So I have a table view with random links that the user has saved. I want to allow the user to click on the link and be taken to a web view to view the webpage. However, every time I click on a link the webpage is empty.
Table View. m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showArticle"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *urlString = [NSString stringWithFormat:#"http://%#", _toDoItems[indexPath.row]];
[[segue destinationViewController] setUrl:urlString];
WebView.m
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *URL = [NSURL URLWithString:[self.url
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://%#", request]]]];
// Do any additional setup after loading the view.
}

Add these keys to your info.plist. In ios 9 you must use https:// instead of http://
Here is the link for configuring App Transport Security
App Transport Security
Make sure your urlString return by this line must not be nil. It must be a valid url working in your browser:
NSString *urlString = [NSString stringWithFormat:#"http://%#", _toDoItems[indexPath.row]];

Related

Detecting onclick on an ASP page on webView Objective C

I am using Classic ASP for my server page. When the following URL is loaded on webView. I will detect the link when user click on the webView using - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
If classbook.asp is detected when a new URL link is clicked on the webView, I will trigger a segue to load a new VC and load the new webView
However there is another google map link on the webView trigger by the following Classic ASP onclick code.
How do I detect when this google map link is clicked on the webView? Currently, the above method which is used to detect URL links cannot detect onclick
Server Classic ASP
<div id="map" onclick="window.open('http://maps.google.com/maps?q=<%=sLat%>,<%=sLongi%>(<%=sFCName%>)', '_system');"></div>
Objective C
#interface ClassesDet (){
AppDelegate *appDelegate;
NSString *sURL;
NSString *sURLFav;
}
#end
#implementation ClassesDet
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
sURL = #"https://www.share-fitness.com/apps/class_det.asp?classcode=CLS100032&access=C&memcode=SF100035&catcode=YG&fccode=KL00059&dtpclass=24/04/2018&tpfrom=20:15&fav=false&lang=EN&encode=20a728210f0869f04aab6cf1682881816a36f0bd47cc894ac4d8dd22dd0ded24"
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ClassBook *target = segue.destinationViewController;
if ([segue.identifier isEqualToString:#"ClassBook"]) {
NSLog(#" To ClassBook sURL : %#", sURL);
target.urlString = sURL;
} else {
NSLog(#" Else to Other VC and the sURL : %#", sURL);
target.urlString = sURL;
}
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
NSString *theString = [URL absoluteString];
NSRange match;
NSLog(#"The URL Clicked : %#", sURL);
match = [theString rangeOfString: #"classbook.asp"];
//---If the URL string does not contain classbook.asp meaning it is loading the webview.
if (match.location == NSNotFound) {
NSLog(#"Load the webView with this sURL : %#", sURL);
return YES; //---Load webview, the URL will be sURL nothing to do with theString
} else {
sURL = theString;
NSLog(#"Trigger the Segue ClassBook and pass the sURL : %# Over ",sURL );
//---Calling the following method will trigger the segue and redirect to another viewController.
[self performSegueWithIdentifier:#"ClassBook" sender:self];
return NO; //---Don't load the webview
}
}
#end
Check out this answer. The problem you're having is that shouldStartLoadWithRequest: doesn't get called on JavaScript window.open(). The answer at the link has one way of making sure your code gets called. I was thinking one of the delegate methods would catch this but now I don't see it. Perhaps I was thinking of WKWebView.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([webView.request.URL.absoluteString containsString:#"http://maps.google.com/maps?q"]) {
//Your logic if require
}
else {
//Your logic if require
}
return YES;
}

why does NOT UIWebView load the content in the first time

I have a UITableView within a UINavigationController,when I select a cell in the table view,it should slide to a UIWebView
//code in table view delegate(tableviewcontroller)
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSURL *url = [NSURL URLWithString:#"https://www.baidu.com/"];
self.webViewController.title = #"UIWebViewTest";
self.webViewController.URL = url;
[self.navigationController
pushViewController:self.webViewController animated:YES];
}
self.webViewController was initialized by
- (void)viewDidLoad {
[super viewDidLoad];
//..
//other code
//..
YGGWebViewController *wvc = [self.storyboard
instantiateViewControllerWithIdentifier:#"wvc"];
self.webViewController = wvc;
}
in the YGGWebViewController, I have a NSURL *URL property and override the setter
- (void)setURL:(NSURL *)URL{
_URL = URL;
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
}
besides,YGGWebViewController also have a UIWebView *webView property linked to a UIWebView in storyboard.
what odd is the web view does not display anything when it slides to the screen in the first time,but it will show the website in the second time(go back to the table view by navigation bar,and select a cell again).
Environment
xcode-7.0
deployment target-7.0
OS X Yosemite-10.10.5
P.S
if I move the code
if (_URL) {
NSURLRequest *req = [NSURLRequest requestWithURL:_URL];
[self.webView loadRequest:req];
}
to viewDidLoad in YGGWebViewController,it works right.but why?

iOS UIWebViewDelegates not called

I have a UIViewController class named WebScreenViewController, which is having an UIWebView. Here is the class implementation file:
#implementation WebScreenViewController
#synthesize urlString;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
// Do any additional setup after loading the view.
[self gotoUrl:self.urlString];
//[self gotoUrl:#"http://www.google.com"];
}
#pragma mark - UIWebView Delegates
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
NSString* urlStr = url.absoluteString;
NSLog(#"shouldStartLoadWithRequest. URLStr = %#", urlStr);
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* html = [webView stringByEvaluatingJavaScriptFromString:#"document.body"];
NSLog(#"webViewDidFinishLoad: HTML string = %#", html);
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"didFailLoadWithError. ERROR: %#", error);
}
#pragma mark - Member Methods
-(void)gotoUrl:(NSString*)urlStr
{
NSLog(#"Calling website %# in UIWebView...", urlStr);
NSURL* url = [NSURL URLWithString:urlStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
#end
The above code work well on one of my testing device but did not work on another. It worked on my iPod Touch with the latest iOS 8.1.2. But it didn't work on my iPad3 with iOS version 7.0. When I run it on my iPad3, it will call the gotoUrl: method and the log "Calling website %# in UIWebView..." will be displayed. And that's it. I receive no more logs from the delegates. To check the internet connections, I tried using Safari on the same device and it was working.
Check whether you've connected the web view's outlet correctly in iPad storyboard if you're using different storyboards for iPhone & iPad.
Also check that the web view is not nil.
Hope this helps..
NSString * strURL = [NSString stringWithFormat:#"http://www.google.com/"];
NSURL *loadURL=[NSURL URLWithString:strURL];
NSURLRequest *loadRequest = [NSURLRequest requestWithURL:loadURL];
[webView loadRequest:loadRequest];
webView.delegate=self;
Try this way in your view did load or if you have URL you can replace first line like this
NSString * strURL = [NSString stringWithFormat:self.urlString];

Unable to display URL in WebView from another view controller

I have a UITableViewController that needs to open a web view.
In my function I have:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:#"LinkViewController_iPhone" bundle:nil];
[linkViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
[self.navigationController pushViewController:linkViewController animated:YES];
}
I have connected all outlets and am not getting any warnings however I do not see the page pull up.
However if I go in the actual LinkViewController file and do something like:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
everything seems fine. I do not understand why ?
You should add a URL property to your LinkViewController. Then in your table view controller you do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:#"LinkViewController_iPhone" bundle:nil];
linkViewController.URL = [NSURL URLWithString:#"http://www.google.com"];
[self.navigationController pushViewController:linkViewController animated:YES];
}
Now in your LinkViewController you do:
- (void)viewDidLoad {
[super viewDidLoad];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.URL];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
There are two problems with what you had originally:
In the table view controller, you were accessing the webView property before it was initialized. This is why nothing happened.
You were exposing far too much implementation detail of the LinkViewController to the outside world. If you use LinkViewController in many places in your app, you are requiring every user of the class to know to create a URL, create a request, and then tell the internal web view to start loading. All of that logic belongs inside the LinkViewController. Now each user of the class only needs to know to set a simple URL property.

Xcode table view and segue, variable not delivered

I'm trying to make my app display a different webpage for every row of a tableview, but running it, when I tap a row it display a white page!
This is the code of the table view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowPDF"]) {
Tab2_ItemViewController *vc = [segue destinationViewController];
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
NSString *stringaProva = [dataArrayLink objectAtIndex:selectedIndex];
vc.selectedItem = stringaProva;
}
}
#end
And this is the code for the subview:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:selectedItem];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:httpRequest];
}
If I write a url manually instead of NSURL *url = [NSURL URLWithString:selectedItem]; it works with no problem, so I'm guessing the problem is that the segue doesn't deliver the right variable... Can someone help?
Check stringaProva has value before passing to selectedItem
Use like this:
if(self.selectedItem)
NSURL *url = [NSURL URLWithString:self.selectedItem];
else
NSLog(#"selectedItem has no value");

Resources