UIWebview loadRequest not loading when called from another controller - ios

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];
}

Related

WebView will not load?

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.

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

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.

UITableView Push to URL help

I am trying to push to a new view and send a url to the webview in that view but it's not getting the url request. storyLink is declared earlier on, but even if I try to request http://www.google.com it doesn't even work either, so I know the problem is it's not accepting the url for some reason.
DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil]autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
[newviewController.rssview loadRequest:request];
[self.navigationController pushViewController:newviewController animated:YES];
Better you declare a property in next view for NSURL and set the value from the parent view.
For example, set following code in your DetailViewController.h file
#interface DetailViewController : UIViewController
{
NSURL *request;
}
#property (nonatomic, assign) NSURL *request;
In your DetailViewController.m file...
#synthesize request;
And modify your current code as below...
DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil]autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
newviewController.request = request;
//[newviewController.rssview loadRequest:request];
[self.navigationController pushViewController:newviewController animated:YES];
In reference to your code...:
Following are some missing things that you have to modify in your code and it will start working fine.
Set delegate for UIWebview control.
self.rssview.delegate = self;
[self.rssview loadRequest:self.request];
In your DetailViewController.h, modify following lines.
#interface DetailViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *rssview;
NSURLRequest *request;
}
#property(nonatomic, retain, readonly)UIWebView *rssview;
#property (nonatomic, assign) NSURLRequest *request;
#end
In DetailViewController.m add delegate method of UIWebview as given below...
- (BOOL)webView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
Replace following code in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: #"link"];
// clean up the link - get rid of spaces, returns, and tabs...
storyLink = [storyLink stringByReplacingOccurrencesOfString:#" " withString:#""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:#"\n" withString:#""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:#" " withString:#""];
storyLink = [storyLink stringByAddingPercentEscapesUsingEncoding: NSStringEncodingConversionAllowLossy];
NSLog(#"link: %#", storyLink);
if(indexPath.row==0)
{
DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil]autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
//[newviewController.rssview loadRequest:request];
newviewController.request = request;
[self.navigationController pushViewController:newviewController animated:YES];
}
else
{
DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil]autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
//[newviewController.rssview loadRequest:request];
newviewController.request = request;
[self.navigationController pushViewController:newviewController animated:YES];
}
}

Resources