Increase Pages After Each Swipe Objective-C - objective-c

I have one UIViewController and inside it I have 3 UIWebView. I added swipe left to my webViews programatically. Right now I can swipe, but just 3 pages and then repeat again. I want to increase my currentPage and add this to my webView3, but I don't know how should I do that.
Would you please give me some hints? I really have problem in this part!
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"test view");
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource: #"Name/Name1" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView2 loadRequest:request];
[webView2 bringToFront];
NSURL *url = [NSURL fileURLWithPath:[ [ NSBundle mainBundle ] pathForResource:
#"Name/Name2" ofType:#"html" ]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView3 loadRequest:request];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;
[self.view addGestureRecognizer:swipeLeft];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer
{
return YES;
}
currentPage is an integer and at the beginning it is 0. My question is how should I add my currentPage to webView3 so that when I Swipe it increase my pages?
- (void)swipeLeftAction:(id)ignored
{
NSLog(#"Swipe Left");
UIWebView *temp = webView2 ;
webView2 = webView3;
webView3 = webView;
webView = temp;
[webView2 bringToFront];
currentPage++;
}
Thanks in advance!
****Edit:**
This current page is not connected to any webView, How Can I get this increase in my webView?**

Based on information from this post (Does UIGestureRecognizer work on a UIWebView?) it looks like gesture recognizers and webviews do not always play nice together, the reason being because of a webview's own gesture recognizers. Perhaps the best way would be to add links inside of your webview that when touched use your own scheme to increment currentPage
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] absoluteString] hasPrefix:#"mySchemeIncrementPage:"] ) {
currentPage++;
NSLog(#"CURRENT PAGE COUNT: %i", currentPage);
return NO;
}
}

Related

Load initial webview every time tabbar is pressed

Edited
My initial webview is loaded when a tabbar is pressed, then when clicked on the webview, I called
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
to detect the URLString clicked
When the string matches my destination string, I will call the segue [self performSegueWithIdentifier:#"NewsDet" sender:self]; and also pass the URLString and load the detail page on another ViewController
Now, when I click the another tabbar and then click back to this tabbar. The detail page remains. I want to refresh back to initial page. How do I do so?
Is it the I need to create a new class with a subclass of tabviewcontroller? Please help.
Gym.m
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp"];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp"];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}
GymDet.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
sURL = self.urlString;
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}
Basically I have 5 tab controllers. Each Tab has a Navigation Controller. When I click on say Tab-A it will load initial webview in VC1 then when I click on webview URL, it will navigate to a new VC say VC2 and load the detailed page.
Now, if I click on say Tab-B and back to Tab-A. I want it to load the initial webview or VC1. Now, my program always stay at VC2.
After clarification on what you're trying to do...
Your issue has nothing to do with webviews or loading URLs. The problem is that you are navigating to a new VC in your NavigationController, then switching tabs, then switching back and you are still at the "pushed" VC.
A comment not related to the problem:
I am on Tab-A, viewing VC-A1... Via "normal" Navigation Controller
action, VC-A2 slides-in from the right. Now I select Tab-B, and I see
VC-B1. Now, I want to switch back to Tab-A, and I expect to see what
was there: VC-A2... but instead, you want to send me back to VC-A1. That really doesn't follow Apple's interface guidelines, and can be very confusing to your users.
However, to accomplish that, you want to subclass UITabBarController and implement didSelectViewController:
#import "MyTabBarViewController.h"
#interface MyTabBarViewController () <UITabBarControllerDelegate>
#end
#implementation MyTabBarViewController
- (void) viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
#end
That will work, but it will "reset" any Navigation Controller in every tab. You'd need to add some other logic to handle only the first tab.
(original answer)
viewDidLoad is only called once - when the view loads. Switching between tabs does not "re-load" the view.
You probably want to reload your original URL in viewWillAppear as that will be called each time you switch back to that tab.
Although, you probably also want to check if the original URL is the current one displayed - you likely only want to reload it if it's not.
And... you never want to call [self viewWillAppear:YES]; -- that is a notification provided by the system.

svprogresshud not showing in xcode5 ios7

I am trying to use SVProgressHUD with cocoapods. I have install it in my xcode workspace following numerous tutorials online. It seems simple enough to use, but I cannot get it to work. Here my code to load data from rottentomatoes app. I want to show "loading" while the network request is doing its thing. I am wondering if this is ui thread issue ? I added a sleep inside the network request because the results were coming back too fast !
- (void) reload{
[SVProgressHUD showWithStatus:#"Updating" maskType:SVProgressHUDMaskTypeBlack];
NSString *url = #"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
[NSThread sleepForTimeInterval:3];
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[object objectForKey:#"movies"]];
self.movies = [[NSMutableArray alloc]init];
for(NSDictionary *item in array){
SFMovie *movie = [[SFMovie alloc]initWithDictionary:item];
[self.movies addObject:movie];
}
[SVProgressHUD dismiss];
[self.tableView reloadData];
}];
}
EDIT for comment:
The reload is called on both init methods (since I am using storyboard for this project)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self reload];
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
[self reload];
}
return self;
}
Second EDIT:
I added a pull down to refresh and the "updating ..." shows up when I pull the tableview down. But it does not show up on init.
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to refresh..."];
self.refreshControl = refreshControl;
}
- (void)refresh:(UIRefreshControl *)sender{
[self reload];
[sender endRefreshing];
}
So I guess I cannot run UI stuff on init methods as the view is not ready. I am now calling the reload method on viewdidload and it works file. Thank you John !
In any init methods, messing with the view hierarchy is tricky business -- keep that logic in viewDidLoad to avoid any of those modifications from being wiped (especially from storyboard initialization).

UIWebView zoom script not working

I want to be able to zoom in and scroll to a certain fixed position on a Facebook-webpage in my application. I manage to view the page without problems, but when I try to to zoom or scroll the UIWebView nothing happens. What am I doing wrong?
Code from ViewController.m
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:#"https://www.facebook.com/xxxx"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[amba loadRequest:requestURL];
[super viewDidLoad];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[amba stringByEvaluatingJavaScriptFromString:#"document. body.style.zoom = 999;"];
CGPoint topOffset = CGPointMake(50, 20);
[scrollView setContentOffset:topOffset animated:YES];
}
Forgot to put webView.delegate=self; in viewDidLoad

passing NSURL and keeping it

I am passing a NSURL from my pickerviewcontroller to my mainviewcontroller. The url is dependent on the user choice in pickerviewcontroller. It works fine, but the user has to repetedly go to the pickerviewcontroller every time the app is restarted to make the choice.
Maybe the code will explain the problem better: This is the pickerviewcontrollers relevant parts...
//The save button on the pickerviewcontroller
- (IBAction)selectedRow:(id)sender {
MainViewController *vc1 = [self.storyboard
instantiateViewControllerWithIdentifier:#"webview"];
vc1.destinationweb = selectedRow;
[self presentViewController:vc1 animated:YES completion:nil];
Once in MainViewController the destinationweb log shows the address of the user choice in pickerview. But If I turn off the app or go forwards in the app (I have another view controller) and then back to MainViewController - it shows destinationweb = null
This is the MainViewController relevant parts:
- (void)viewDidLoad {
{
//webView.hidden = YES;
self.webView.delegate = self;
NSURL *url = destinationweb;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
NSLog(#"destinationweb shows= %#", destinationweb);
}
[super viewDidLoad];
}
I have no idea what I am supposed to do to make the MainViewController remember the url until another row in picker view is selected??
Just save the url string in user defaults and retrieve the url string in view controller
- (IBAction)selectedRow:(id)sender
{
MainViewController *vc1 = [self.storyboard
instantiateViewControllerWithIdentifier:#"webview"];
//Instead of passing it just save the url string in user defaults
NSString *urlString = [selectedRow absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:#"UserURL"];
//vc1.destinationweb = selectedRow;
[self presentViewController:vc1 animated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//webView.hidden = YES;
self.webView.delegate = self;
NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:#"UserURL"];
NSURL *url = [NSURL URLWithString:urlString ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
NSLog(#"destinationweb shows= %#", destinationweb);
}
if you want to keep the value once the app is restarded, you should storage the value (url) in a file, so when you restart it you can read it from there.

iOS: NSURLRequest returns the same page each time on different storyboards

I have a UIWebView class that is added to each storyboard on load, but when passing into this class the page I require form the parent ViewController it only show the file from the first ever page and on each different page/viewController its the same even though I set it different on each.
Is it somehow caching this page and if so, how can I have a clean UIWebview everytime?
my UIwebview class m file (ContentView.m):
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisplay ofType:#"html" inDirectory:#"pageContent"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlpath];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self loadRequest:request];
}
return self;
}
My Parent view passes the requested page using the following variable pageToDisplay:
Parent View m file:
#import "ContentView.h"
- (void)viewDidLoad {
ContentView *dispalyPageContent = [[ContentView alloc] init];
[dispalyPageContent setPageToDisplay:#"THEpg1"];
[self.view addSubview:dispalyPageContent];
//--
[super viewDidLoad];
}
For each other parent view the code is the same but the [displayPageContent setPageToDisplay:#"THEpg1"]; which should change the page I want, e.g. next view would be [displayPageContent setPageToDisplay:#"THEpg2"];
Is it possible to clear this and load a fresh request each time?
It's because when you call ContentView *dispalyPageContent = [[ContentView alloc] init]; in your viewDidLoad , you init it with:
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisplay ofType:#"html" inDirectory:#"pageContent"];
And in pathForResource:pageToDisplay at the moment of initializing is some trash.
And then, when you call [dispalyPageContent setPageToDisplay:#"smth"] , it changes nothing, because your class ContentView has been already inited with some trash in PageToDisplay
(Also, you call [[ContentView alloc] init]; and in your code there is only initWithFrame:... for objects of ContentView class)
You can solve this in this way:
1) in your ContentView.m
- (id)initWithFrame:(CGRect)frame pageToDisp:(NSString *)pageToDisp {
self = [super initWithFrame:frame];
if (self) {
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisp ofType:#"html" inDirectory:#"pageContent"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlpath];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self loadRequest:request];
}
return self;
}
2) In your ContentView.h:
- (id)initWithFrame:(CGRect)frame pageToDisp:(NSString *)pageToDisp
3) And then, in viewDidLoad:
- (void)viewDidLoad {
ContentView *dispalyPageContent = [[ContentView alloc] initWithFrame:someFrame pageToDisp:#"THEpg1"];
[self.view addSubview:dispalyPageContent];
//--
[super viewDidLoad];
}