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;
}
Related
I am new to Objective-C. So I have a table view that leads to a webView. Some how the WebView won't load. In my view controller for the webView I have
NSURL *URL = [NSURL URLWithString:[self.url
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
The TableView controller has
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"showArticle" sender:nil];
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showArticle"]) {
I think I am missing something under prepareForSegue but I don't know.
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 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,
When my web viewDidLoad fires it correctly sets the starting URL for my web view and logs "Load Request: http://xxxxxx.dev/login". Just as you would expect.
When calling loadRequestFromString from SidebarViewController, the webview does not change to the correct URL. However, thanks to NSLog I can see that it is getting to that point just not refreshing the URL "Load Request: http://xxxxxx.dev/this-is-the-url-that-should-load".
SidebarViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *url = self.tableData[indexPath.row][#"url"];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
WebAppViewController *wvc = [[WebAppViewController alloc] initWithNibName:#"webappController" bundle:kNilOptions];
[wvc loadRequestFromString:url];
}
WebAppViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
[self loadRequestFromString:#"http://xxxxxx.dev/login"];
}
.....
- (void)loadRequestFromString:(NSString*)urlString
{
self.webView.delegate = self;
NSLog(#"Load Request: %#", urlString);
NSString *properlyEscapedURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:properlyEscapedURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
I'm developing an iOS app that has 2 VCs, mapVC(using google map SDK) and webVC. Selecting an annotation on mapVC, then transition to webVC with corresponding webpage openned.
But loadRequest method in viewDidLoad of webView is too slow to wait.
So I want to preload the web data when selecting a pin on mapView, and then hand over the preloaded data to webVC using loadData method.
But webVC displays only text, no images no CSS.
What should I do to preload full HTML/CSS/images?
any advice appreciated. thanks in advance.
my current source code as follows.
// --- mapViewController ---
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
NSURL *url = [NSURL URLWithString:#"(target URL)"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
receivedData = [NSMutableData dataWithCapacity:0];
NSURLConnection *urlConnection;
urlConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
if (!urlConnection) {
receivedData = nil;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
receivedData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showWeb"]) {
WebViewController *webViewController = [segue destinationViewController];
webViewController.preloadedData = receivedData;
}
}
// --- webViewController ---
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
[self.webView loadData:self.preloadedData
MIMEType:#"text/html"
textEncodingName:#"utf-8"
baseURL:nil];
}
Take a look at ASIWebPageRequest. It allows the download of the entire web page, including all resources.
You have only loaded the HTML-Code but not the Content (e.g. media / images / css). You must pre parse the Response and manually pre load the rest stuff.
NSURLConnection make no look up for URLĀ“s inside the response and no autoload