Create and Present modal view controller programmatically - ios

I am trying to programmatically declare a modal view controller from a view controller launched using storyboard. I would expect to see a blank view coming up but instead I only see the webview from the first controller.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
_viewWeb.delegate=self;
[_viewWeb loadRequest:requestObj];
modalViewController=[[UIViewController alloc] init];
[self presentViewController:modalViewController animated:YES completion:nil];
}

You're doing this too soon. There's no interface yet, in viewDidLoad. Put that code into viewDidAppear: instead, and see what happens.

Related

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.

UIWebView Image Upload reloading webview Objective c

I am using UIWebview to show user profile details. Now when trying to upload the image whole Webview reloading and showing back to the same stage before uploading.
UIWebView *ProfileCellWebview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, cell.layer.frame.size.width, (MainFrame.size.height - 210))];
[ProfileCellWebview setBackgroundColor:[UIColor clearColor]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:USERPROFILEPUBLIC,self.appDelegate.currentUser.userId]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[ProfileCellWebview setDelegate:self];
[ProfileCellWebview loadRequest:requestObj];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[cell addSubview:ProfileCellWebview];
Where i am doing wrong
Implement the code to load the url in viewDidLoad. Hope you write the code in viewWillApear or viewDidApper.

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

How to redirect UIWebview?

I am new to iPhone development and I want to move to UIWebview based on the URL when the user clicks on a particular row (Google,Facebook,Yahoo). I want the page to redirect to the UIWebview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"URL"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
statuses = [parser objectWithString:json_string error:nil];
if (indexPath.row == 0) {
//What do i write in here
} else if(indexPath.row == 1) {
} else if(indexPath.row == 2) {
} else {
}
}
Can anyone help me?
If a webView is managed by the same ViewController (so the ViewController has a property like this):
#property (nonatomic, retain) UIWebView *webView;
then you can open a link like so:
NSURL *url = [NSURL URLWithString:#"http://your-url-goes-here.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
However if the webView is managed by another ViewController than you have to pass a link to that ViewController and then trigger a segue.
A common approach is to set viewController's property to pass a link.
For example in WebViewController.h
...
#property (nonatomic, copy) NSString *linkToOpen; //My fantasy isn't really working right now :)
...
in WebViewController.m
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[[NSURL alloc] initWithString:self.linkToOpen] autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
...
And a segue from your ViewController to the WebViewController has an identifer #"myTestSegue" then you can do it like so:
WebViewController *webViewController = [[WebViewController alloc] initWithStyle:/*I don't honestly remember what to write here, sorry :(*/];
webViewController.linkToOpen = #"your link goes here. You can assign it inside a switch operator.";
[self.navigationController pushViewController:webViewController animated:YES];
[webViewController release];
The code is incomplete and probably incorrect somewhere, but I hope that it will help you to achieve your goals.
You have to create a new controller and navigate to that controller on click. Also you have to pass link to another controller for load the url in to webview.

UIWebView doesnt load URL , iOS

I am trying to load a URL when my app starts.
I dragged an UIWebView Object , on my .nib , i created a property outlet , i connected it to the webview and in my view controller i place the following code :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
However i just get a black screen on the device , like theres no view at all. Any idea?
This is the code I'm using in my application
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *websiteUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[myWebView loadRequest:urlRequest];
}
This is short one
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
}
Restart your IOS simulator.
It is really not evident, but check, at first, any site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.
By Amit

Resources