Created
May 18, 2016 20:08
-
-
Save FatihDurmus/dba74b42425fe0f71685acec7be12aa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "WebBrowserViewController.h" | |
#import "Reachability.h" | |
@interface WebBrowserViewController () | |
@property (nonatomic,strong) NSTimer *timer; | |
@property (nonatomic,assign) UIGestureRecognizerState gestureState; | |
@end | |
static const CGFloat kNavBarHeight = 52.0f; | |
static const CGFloat kLabelHeight = 14.0f; | |
static const CGFloat kMargin = 10.0f; | |
static const CGFloat kSpacer = 2.0f; | |
static const CGFloat kLabelFontSize = 12.0f; | |
static const CGFloat kAddressHeight = 26.0f; | |
@implementation WebBrowserViewController | |
@synthesize webView = mWebView; | |
@synthesize toolbar = mToolbar; | |
@synthesize timer; | |
@synthesize gestureState; | |
- (void)dealloc | |
{ | |
[mWebView release]; | |
[mToolbar release]; | |
[super dealloc]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
-(IBAction)launch:(id)sender{ | |
[self viewDidLoad]; | |
} | |
-(IBAction)goBack:(id)sender{ | |
[mWebView goBack]; | |
} | |
-(IBAction)goForward:(id)sender{ | |
[mWebView goForward]; | |
} | |
#pragma mark - View lifecycle | |
- (BOOL)connected | |
{ | |
Reachability *reachability = [Reachability reachabilityForInternetConnection]; | |
NetworkStatus networkStatus = [reachability currentReachabilityStatus]; | |
return networkStatus != NotReachable; | |
} | |
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[UIApplication sharedApplication].idleTimerDisabled = YES; | |
NSURL* Rurl = [NSURL URLWithString:@"http://yemekler.com.tr/yukleniyor.html"]; | |
NSURLRequest* request = [NSURLRequest requestWithURL:Rurl]; | |
[mWebView loadRequest:request]; | |
/*---------*/ | |
/*----------*/ | |
self.webView.scalesPageToFit = YES; | |
self.navigationController.toolbarHidden = NO; | |
[self performSelector:@selector(loadURL:) withObject:nil afterDelay:0.1]; | |
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateWeb) userInfo:nil repeats:YES]; | |
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(appearToBar) userInfo:nil repeats:NO]; | |
//[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(SuggestUs) userInfo:nil repeats:NO]; | |
} | |
/*-(void)SuggestUs{ | |
UIAlertView *suggest = [[UIAlertView alloc]initWithTitle: @"Değerlendirin!" | |
message: @"Uygulamamız hakkında görüşlerinizi bizimle paylaşın." | |
delegate: self | |
cancelButtonTitle:@"İptal" | |
otherButtonTitles:@"Tamam", nil]; | |
suggest.tag=2; | |
[suggest show]; | |
}*/ | |
-(NSString *)hexStringFromColor:(UIColor *)color { const CGFloat *components = CGColorGetComponents(color.CGColor); CGFloat r = components[0]; CGFloat g = components[1]; CGFloat b = components[2]; | |
return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)]; | |
} | |
-(void) appearToBar{ | |
self.toolbar.barTintColor = nil; | |
} | |
/* | |
- (void) alertView:(UIAlertView *)suggest clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
if (![self connected]) { | |
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Internet Yok" | |
message: @"Uygulamayı kullanabilmeniz için internet bağlantınız olmalıdır" | |
delegate: self | |
cancelButtonTitle:@"İptal" | |
otherButtonTitles:@"Tamam",nil]; | |
alert.tag=1; | |
[alert show]; | |
} else { | |
if(buttonIndex == 0 ) //cancel button | |
{ | |
if (suggest.tag == 1) { | |
} | |
} | |
} | |
else if (buttonIndex == 1 ){ | |
[mWebView stopLoading]; //added this line to stop the previous request | |
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/us/app/rainbow-blocks-free/id515802017?mt=8"]; | |
NSURLRequest *req = [NSURLRequest requestWithURL:url]; | |
[mWebView loadRequest:req]; | |
} | |
} | |
}*/ | |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
if(alertView.tag == 1) | |
{ | |
if (buttonIndex == 0) { | |
if (![self connected]) { | |
[mWebView stopLoading]; | |
mWebView.delegate = self; | |
NSString *indexPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:nil]; | |
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]]; | |
} | |
else{ | |
NSURL *url = [NSURL URLWithString:@"http://www.yemekler.com.tr/yukleniyor.html"]; | |
NSURLRequest *req = [NSURLRequest requestWithURL:url]; | |
[mWebView loadRequest:req]; | |
} | |
} | |
else if (buttonIndex == 1){ | |
if (![self connected]) { | |
[mWebView stopLoading]; | |
mWebView.delegate = self; | |
NSString *indexPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:nil]; | |
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]]; | |
} | |
else{ | |
NSURL *url = [NSURL URLWithString:@"http://www.yemekler.com.tr/yukleniyor.html"]; | |
NSURLRequest *req = [NSURLRequest requestWithURL:url]; | |
[mWebView loadRequest:req]; | |
} | |
} | |
} | |
/* else if (alertView.tag==2){ | |
if (buttonIndex == 0) { | |
} | |
else if (buttonIndex == 1){ | |
if (![self connected]) { | |
[mWebView stopLoading]; | |
NSString* htmlString= @"<b><h1>İnternet Yokluğu Nedeniyle Açılamaz</h1></b>"; | |
[mWebView loadHTMLString:htmlString baseURL:nil]; | |
} | |
else{ | |
[mWebView stopLoading]; //added this line to stop the previous request | |
NSURL *url = [NSURL URLWithString:@"http://www.yemekler.com.tr/appstore.html"]; | |
NSURLRequest *req = [NSURLRequest requestWithURL:url]; | |
[mWebView loadRequest:req]; | |
} | |
} | |
}*/ | |
} | |
-(void)loadURL:(id)sender{ | |
[mWebView stopLoading]; //added this line to stop the previous request | |
NSURL *url = [NSURL URLWithString:@"http://www.yemekler.com.tr/yukleniyor.html"]; | |
NSURLRequest *req = [NSURLRequest requestWithURL:url]; | |
[mWebView loadRequest:req]; | |
} | |
- (void)viewDidUnload | |
{ | |
self.webView = nil; | |
self.toolbar = nil; | |
[super viewDidUnload]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
return YES; | |
} | |
// MARK: - | |
// MARK: UI methods | |
/** | |
* \brief Updates the forward, back and stop buttons. | |
*/ | |
- (void)refreshWebBrowser { | |
[mWebView reload]; | |
} | |
// MARK: - | |
// MARK: UIWebViewDelegate protocol | |
/** | |
* \brief | |
* | |
* This is called for more than just the toplevel page so is not ideal for | |
* updating the loading URL. | |
* | |
* \param navigationType is one of: | |
* <ol> | |
* <li>UIWebViewNavigationTypeFormSubmitted,</li> | |
* <li>UIWebViewNavigationTypeBackForward,</li> | |
* <li>UIWebViewNavigationTypeReload,</li> | |
* <li>UIWebViewNavigationTypeFormResubmitted,</li> | |
* <li>UIWebViewNavigationTypeOther.</li> | |
* </ol> | |
*/ | |
-(void)updateWeb | |
{ | |
if (![self connected]) { | |
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Internet Yok" | |
message: @"Uygulamayı kullanabilmeniz için internet bağlantınız olmalıdır" | |
delegate: self | |
cancelButtonTitle:@"İptal" | |
otherButtonTitles:@"Tamam",nil]; | |
alert.tag=1; | |
[alert show]; | |
}else{ | |
} | |
} | |
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType | |
{ | |
if (![self connected]) { | |
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Internet Yok" | |
message: @"Uygulamayı kullanabilmeniz için internet bağlantınız olmalıdır" | |
delegate: self | |
cancelButtonTitle:@"İptal" | |
otherButtonTitles:@"Tamam",nil]; | |
alert.tag=1; | |
[alert show]; | |
return NO; | |
}else{ | |
return YES; | |
} | |
} | |
// This is the second method to recognize touch event | |
- (void)webViewDidStartLoad:(UIWebView *)webView | |
{ | |
if (![self connected]) { | |
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Internet Yok" | |
message: @"Uygulamayı kullanabilmeniz için internet bağlantınız olmalıdır" | |
delegate: self | |
cancelButtonTitle:@"İptal" | |
otherButtonTitles:@"Tamam",nil]; | |
[alert show]; | |
} else { | |
NSURL* Rurl = [NSURL URLWithString:@"http://yemekler.com.tr/yukleniyor.html"]; | |
NSURLRequest* request = [NSURLRequest requestWithURL:Rurl]; | |
[webView loadRequest:request]; | |
} | |
} | |
- (void)webViewDidFinishLoad:(UIWebView *)webView | |
{ | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; | |
} | |
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error | |
{ | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; | |
} | |
// MARK: - | |
// MARK: Debugging methods | |
- (NSString*)navigationTypeToString:(UIWebViewNavigationType)navigationType | |
{ | |
switch(navigationType) | |
{ | |
case UIWebViewNavigationTypeFormSubmitted: | |
return @"UIWebViewNavigationTypeFormSubmitted"; | |
case UIWebViewNavigationTypeBackForward: | |
return @"UIWebViewNavigationTypeBackForward"; | |
case UIWebViewNavigationTypeReload: | |
return @"UIWebViewNavigationTypeReload"; | |
case UIWebViewNavigationTypeFormResubmitted: | |
return @"UIWebViewNavigationTypeReload"; | |
case UIWebViewNavigationTypeOther: | |
return @"UIWebViewNavigationTypeOther"; | |
} | |
return @"Unexpected/unknown"; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment