I'm automatically generating a NSURL based on the distance of a pan gesture on the screen (long story...). Anyways, I'm having no problem generating the URL. The URL dynamically changes, and then I have an NSTimer that repeats every second which tells a UIWebView to reload with the new URL.
However, although the URL is reported to be changing, the UIWebView will only show the first URL sent to it. I'm also monitoring the website itself, and it's receiving the first URL every second, opposed to the updated ones. My code is pretty simple right now, so I'm not sure what I did wrong? Any help would be appreciated.
In ViewController.h:
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property float slicenumber;
#property NSURL *dynamicUrl;
In ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = #"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=.50&size=native&fmt=png";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.webView reload];
self.webView.scrollView.scrollEnabled = NO;
self.webView.scrollView.bounces = NO;
self.slicenumber=.5;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(UpdateSlice) userInfo:nil repeats:YES];}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
self.slicenumber=self.slicenumber+translation.y/350;
if (self.slicenumber<0) {
self.slicenumber=0;
}
if (self.slicenumber>1) {
self.slicenumber=1;
}
self.sliceloc.text= [NSString stringWithFormat:#"%.2f",self.slicenumber];
NSString *fullURL2 = [NSString stringWithFormat:#"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=%.2f&size=native&fmt=png",self.slicenumber];
self.dynamicUrl = [[NSURL alloc] initWithString:[fullURL2 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"urlString = %#",self.dynamicUrl);}
- (void)UpdateSlice {
[self.webView loadRequest:[NSURLRequest requestWithURL:self.dynamicUrl]];
[self.webView reload];}
Thanks a lot! Let me know if you need any other info.
[self.webView reload] - will reload the current page. This is probably happening before the loadRequest has finished.
Try removing this line.
Also, #joern's comment is correct; the 'event' is the user making a pan gesture. Lose the timer.
Related
I've tried using similar answers to my problem that i've encounter. I'm new to Xcode and I have one controller that contains a uiwebview that loads my website. i have coding to detect if a certain link is pressed. The issue i'm running into is when a certain link is pressed, I need my second controller to act as a pop up and display a different webpage. Everything works and that link is pressed my NSLog says that it in the second controller but the display is black. Here code that i have in the main view controller that loads my webpage:
- (void)viewDidLoad {
[super viewDidLoad];
self.webViewer.delegate=self;
NSString *fullURL = #"https://www.example.com?first_run=true";
NSURL *url = [NSURL URLWithString:fullURL];
//NSLog(#"first page loaded=%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
[_webViewer loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.webViewer.delegate=self;
NSURL *strurl = request.URL;
NSString *urlString = strurl.relativeString;
NSLog(#"link clicked=%#",urlString);
if(//not the right link do this){
}
else{//is the right link do this
NSLog(#"found");
self.popup = [[PopUpController alloc] init];
[self.popup showInView:self.view animated:YES];
return NO;
}
}
#end
then for my second controller it's supposed to load a different webpage but just get a black screen:
- (void)viewDidLoad {
self.popupView.delegate=self;
NSString *popupURL = #"https://www.example2.com";
NSURL *url_popup = [NSURL URLWithString:popupURL];
NSLog(#"popup page loaded=%#",url_popup);
NSURLRequest *requestObj2 = [NSURLRequest requestWithURL: url_popup];
[_popupView loadRequest:requestObj2];
}
- (void)showAnimate{
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.view.alpha = 0;
[UIView animateWithDuration:.25 animations:^{
self.view.alpha = 1;
self.view.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)showInView:(UIView *)aView animated:(BOOL)animated{
CGFloat x = self.view.center.x - (aView.frame.size.width);
CGFloat y = self.view.center.y - (aView.frame.size.height);
[aView setFrame:CGRectMake(x,y,aView.bounds.size.width,aView.bounds.size.height)];
[aView addSubview:self.view];
if (animated) {
[self showAnimate];
}
}
Any help would be extremely appreciated. I've tried for 3 weeks searching and trying different solutions found on this forum but they all didn't work for mine. Not sure if I'm just not instantiating my controllers right or some piece of code that I'm missing. Thank you for your time in advanced.
This line:
self.popup = [[PopUpController alloc] init];
simply creates an instance of PopUpController but doesn't load any of you UI elements with it.
Assuming you have designed PopUpController in storyboards, give it an Identifier such as "MyPopUp"
and then use:
// storyboard reference
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: #"Main" bundle:[NSBundle mainBundle]];
// instantiate PopUpController from the storyboard
self.popup = (PopUpController *)[storyboard instantiateViewControllerWithIdentifier:#"MyPopUp"];
// show it
[self presentViewController:self.popup animated:YES completion:nil];
My app has a section with a daily quote, and this quote is pulled in from a webpage on the internet. I have been trying to add in the quote using a Today Extension, so it could be viewed there, but the extension constantly shows up blank.
I have added the Today Extension File with storyboard, and in the implementation file for the code I have:
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#interface TodayViewController () <NCWidgetProviding>
#end
#implementation TodayViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.title = #"Today's Scripture";
self.preferredContentSize = CGSizeMake(320, 50);
[reader loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.316apps.com/testingdailyreader.html"]cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
// timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(tick) userInfo:nil repeats:YES];
}
#end
However, all I get is a blank row that says 'Unable to Load' in the Notification Center. What am I doing wrong?
Here is how I transitioned from UIWebView to WKWebView.
Note: There is no property like UIWebView that you can drag onto your storyboard, you have to do it programatically.
Make sure you import WebKit/WebKit.h into your header file.
#import <WebKit/WebKit.h>
Here, the implementation:
#import "ViewController.h"
#interface ViewController ()
#property(strong,nonatomic) WKWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.productURL = #"http://www.URL YOU WANT TO VIEW GOES HERE";
NSURL *url = [NSURL URLWithString:self.productURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.316apps.com/testingdailyreader.html"]cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
_webView = [[WKWebView alloc] initWithFrame:self.view.frame];
[_webView loadRequest:request];
_webView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:_webView];
}
#end
In web view my code is`static float i=1.5;
if(i<=7.5)
{
[btnZoom setEnabled:YES];
objWebView.transform = CGAffineTransformMakeScale(1+i,1+i);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.0f];
[UIView commitAnimations];
i=i+1.5;
NSLog(#"i===============%f",i);
}
if(i==7.5)
{
NSLog(#"i===============%f",i);
[btnZoom setEnabled:NO];
i=1.5;
}
objWebview is the object of web view.my scales page to fit property of webview is clicked but I have to zoom in on the button click. (I am new in iPhone)
By this code it is getting zoomed-in, but my webview contents are getting blurred when zoomed in.
There are two ways to zoom your UIWebView by codebase.
Zoom UIScrollView available in UIWebView
This option will applicable from iOS 5.0 and >
- (void)viewDidLoad
{
NSString *urlAddress = #"<website url address here>";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]
[webView loadRequest:requestObj];
webView.delegate = self;
webLookupView.scalesPageToFit = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
CGSize contentSize = theWebView.scrollView.contentSize;
CGSize viewSize = self.view.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
}
Zoom Webpage itself
In UIWebViewDelegate method – webViewDidFinishLoad:
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[theWebView stringByEvaluatingJavaScriptFromString:jsCommand];
Hope this helps you.
I have an app where I would like to push a button and have the user taken to a web view. However, I would like a particular page to load depending on the button that's pushed. Originally I thought about creating a separate web view for each button, but that only caused more issues, which I won't go into. So my question is: How can I load different URL's into a web view, based on whether a particular button is pushed?
I thought about using the prepareForSegue method, but thought that might be over complicating things. Any direction would be great.
so you must create new viewController with UIWebView inside it.
On newViewController create a NSString property named url or somethings like that
example:
#property (retain, nonatomic) NSString *url;
#property (weak, nonatomic) IBOutlet UIWebView *webView;
On buttonTouchUpInside event in oldViewController
UIStoryboard *mainStorybroad = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
newViewController *webViewController = [mainStorybroad instantiateViewControllerWithIdentifier:#"webviewIdentifier"];
webViewController.url = #"url string";
and final, in newViewController you just do:
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
Why don't you link the button in IB to just do a modal transition to the view controller page? Then, in the viewdidload on the new view controller, the code should look something like this:
NSString *fullURL = #"google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
How many buttons do you have? If it's just a couple, then I would just create an extra couple view controllers. If it's a bunch, you could always create a global variable (I can already hear the cringing...) and set the variable equal to the desired URL depending on which button was pushed.
How can I load different URL's into a web view, based on whether a particular button is pushed?
There are two basic approaches to recognizing different buttons:
separate actions: You can create a separate action method for each button, and then just hook up each button to the corresponding action.
single action: You can create a single action for all the buttons, and use some property of the buttons to differentiate between them.
The first one looks like this:
- (IBAction)button1Action:(UIButton*)sender
{
[self.webView loadRequest:[NSURLRequest requestWithURL:url1]];
}
- (IBAction)button2Action:(UIButton*)sender
{
[self.webView loadRequest:[NSURLRequest requestWithURL:url2]];
}
The second looks like:
- (IBAction)buttonsAction:(UIButton*)sender
{
switch (sender.tag) {
case button1Tag:
[self.webView loadRequest:[NSURLRequest requestWithURL:url1]];
break;
case button2Tag:
[self.webView loadRequest:[NSURLRequest requestWithURL:url2]];
break;
}
}
Here is the code for .h and .m File
Create a XIB and Put UIWebView on it and bind it to the class.
For Header File
#import <UIKit/UIKit.h>
#interface NewWebViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
NSString *currentURL;
UIActivityIndicatorView *activityView;
}
#property (nonatomic, copy) NSString *currentURL;
#property (nonatomic, retain) UIActivityIndicatorView *activityView;
#end
For Implementation File (.m)
#import "NewWebViewController.h"
#interface NewWebViewController ()
#end
#implementation NewWebViewController
#synthesize currentURL;
#synthesize activityView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
webView.frame = self.view.frame;
[webView setScalesPageToFit:YES];
NSURL *url = [NSURL URLWithString:self.currentURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
self.activityView = [[[UIActivityIndicatorView alloc] init] autorelease];
self.activityView.tag = 1;
self.activityView.hidesWhenStopped = YES;
UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
self.activityView.center = customView.center;
[customView addSubview:self.activityView];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:customView] autorelease];
[self.activityView startAnimating];
}
#pragma mark UIWebView Delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.activityView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.activityView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Error %#",[error description]);
[self.activityView stopAnimating];
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[webView stopLoading];
webView.delegate = nil;
webView = nil;
self.activityView = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
webView.frame = self.view.frame;
return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) dealloc {
[currentURL release];
[webView release];
[super dealloc];
}
#end
Here how to use this
- (void) openWebViewControllerWithURL:(NSString*) url {
if (!url) {
return;
}
NewWebViewController *vController = [[NewWebViewController alloc] initWithNibName:#"NewWebViewController" bundle:nil];
vController.currentURL = url;
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}
On click of button just pass the URL you want to load
like this
[self openWebViewControllerWithURL:#"http://www.google.com"];
I have a UIWebView embeded as a subview which loads an html form from the web. The problem is that when you select one of the text fields it receives the focus, but the keyboard does not display. this only happens on iOS6 if i run this app on iOS 4.3, 5.1 etc it works as expected. Any one have any advice
- (void)viewDidLoad
{
[super viewDidLoad];
webView = [[UIWebView alloc] init];
[webView setUserInteractionEnabled:YES];
[webView setScalesPageToFit:YES];
[webView setKeyboardDisplayRequiresUserAction:YES];
// load url here
NSURL *url = [NSURL URLWithString:redirectUrl];
//URL Requst Object
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[requestObj setHTTPMethod:#"POST"];
[webView loadRequest:requestObj];
UIScrollView* sv = nil;
for(UIView* v in [webView subviews]){
if([v isKindOfClass:[UIScrollView class] ]){
sv = (UIScrollView*) v;
sv.scrollEnabled = NO;
sv.bounces = NO;
}
}
[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor whiteColor]];
webView.delegate = self;
[self.view addSubview:webView];
}
You can use the answer of this question:
Some UITextfields don't respond in iOs6
You have to resingfirstresponder before presentmodalviewcontroller in the the controller where are you calling the view with the problem.
BUT, you also have to resignfirst responder in all previews controllers, that is.
In my case i go from a searchbar to a detail view, and from detail to share in facebook. The view of share in facebook wasn't showing the keyboard, and it was 'cause i wasn't resiginif first responder of search bar.
Hope it helps.