Semantic Issue using Reachability - ios

I have been stumped by this Semantic Issue warning for a week now, and it is becoming frustrating.
Anyone willing to take a look at it? Any advice will be graciously taken. <3
WebViewController.m
#import "WebViewController.h"
#import "Reachability.h"
#implementation WebViewController
#synthesize webView;
#synthesize backbtn;
#synthesize forwardbtn;
#synthesize webAddy;
#synthesize activityIndicator;
#synthesize loadingLabel;
- (void)viewDidLoad {
loadingLabel.hidden = YES;
backbtn.enabled = NO;
forwardbtn.enabled = NO;
webView.scalesPageToFit = YES;
activityIndicator.hidesWhenStopped = YES;
if([Reachability currentReachabilityStatus] == NotReachable)
{
UIAlertView *av = [[[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You are not connected to the internet. Please Try again"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[av setDelegate:self];
[av show];
}
else
{
NSURL *url = [NSURL URLWithString:webAddy];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
[super viewDidLoad];
}
The Warning redirects me to the 20th line.
The compiler tells me:
"Class method '+currentReachabilityStatus" not found (return type defaults to 'id')"
If more information is needed, please let me know. Thanks again everyone!

See here.
The - (NetworkStatus) currentReachabilityStatus; is not a class method.

Use [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] instead of [Reachability currentReachabilityStatus].
currentReachabilityStatus is an instance method, so you need an instance first.

Related

Data (char*) appears corrupt when logged after pressing button

I'm quite newbie to iphone programming.
The problem is when I push a button, a data will be destroyed.
I can't find where my code is wrong and why. Please help me.
This is overview of my program.
1. load text data from "sample.txt"
2. log the data
3. When I push a button, it logs the data again.
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIButton *myButton1;
#property (assign) unsigned char* bytePtr;
#end
AppDelegate.m:
~snip~
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton1 setFrame:CGRectMake(0, 0, 100 ,100)];
[self.myButton1 addTarget:self action:#selector(button1DidPushed) forControlEvents:UIControlEventTouchUpInside];
[self.myButton1 setTitle:#"push" forState:UIControlStateNormal];
[self.window addSubview:self.myButton1];
[self load];
return YES;
}
- (void) load
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"txt"];
NSData* data = [NSData dataWithContentsOfFile:path];
self.bytePtr = (unsigned char *)[data bytes];
NSLog(#"%s", self.bytePtr);
}
- (void)button1DidPushed
{
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:#"Enter something..." message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alerView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alerView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
}
NSLog(#"%s", self.bytePtr);
}
~snip~
sample.txt:
abcdefghijklmnopqrstuvwxyz
output:
abcdefghijklmnopqrstuvwxyz
<----- When I push the button the mark similar to "?" will appear on output window.(I couldn't show it here(It's probabily ascii code 2(STX))) That is why I think the data is destroyed.
Environment:
xcode 6.0.1
Thanks.
The issue is that you are not retaining the NSData object:
- (void) load
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"txt"];
NSData* data = [NSData dataWithContentsOfFile:path]; // here
self.bytePtr = (unsigned char *)[data bytes];
NSLog(#"%s", self.bytePtr);
}
Once that method returns the NSData object will be destroyed and therefore the buffer pointed to by self.bytePtr is no longer valid.
To solve this issue change self.bytePtr to an NSData object and store that instead.
First of all, in order to receive your alertView:clickedButtonAtIndex: method call, you need to set your UIAlertView delegate to self.
[alertView setDelegate:self] // make sure you set UIAlertViewDelegate protocol on the method owner
Otherwise you should use an NSString object initialized using initWithData:encoding:.

Checking if there is internet connection webView did not appear

I'm wondering why I don't get alert when I disconnect my internet connection. When I'm connected everything is going great, but when there is no connection, simply nothing appears, I'm pretty new in objective-c but I think this code looks correct.
Thanks for help anyway.
- (void)viewDidLoad
{
webView.hidden = YES;
NSURL *url = [NSURL URLWithString:#"https://url"];
webView.scrollView.scrollEnabled = NO;
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
if ([[webView stringByEvaluatingJavaScriptFromString:#"document.readyState"] isEqualToString:#"complete"]) {
webView.hidden = NO;
}
self.canDisplayBannerAds = YES;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Internet Connection!"
message:#"Can't connect. Please check your internet Connection"
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:nil];
[alert show]; }
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
exit(0);
}
If you want to check the connectivity with internet then you have to check it in your code. For this you can go through Apple's Reachbility code.
The method webView:didFailLoadWithError: will called if page could not be loaded due to some reason. This means it's not connectivity issue, this may be due to some internal errors at server.

i want navigate to another file in webview.

i want to call objective c file from javascript.
- (void)viewDidLoad
{
webview.delegate = self;
myButton.enabled = NO;
NSString *path=[[NSBundle mainBundle]pathForResource:#"1" ofType:#"html" inDirectory:#"files"];
NSURL *url=[NSURL fileURLWithPath:path];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webview loadRequest:request];}
i am using this code to call my html page successfully and i use the below code to call shouldStartLoadWithRequest method in objective c.
<img src="cercle24px.png" />
now i went to call new TestViewController.m file how to i call this file, i used the below code.its print the nslog correctly and give alert box also.but doesn't navigate to next file.please help me if any one know.i am waiting for your valuable reply please.
- (BOOL)webView:(UIWebView*)webview shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"what");
UIAlertView *tstAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"Allowed only alphabets and numeric" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[tstAlert show];
NSString *absoluteUrl = [[request URL] absoluteString];
NSLog(#"absolute%#",absoluteUrl);
if ([absoluteUrl isEqualToString:#"didtap://button1"]) {
NSLog(#"yes");
TestViewController *testview=[[TestViewController alloc]initWithNibName:#"TestViewController" bundle:nil];
[self.navigationController pushViewController:testview animated:YES];
return NO;
}
NSLog(#"no");
return YES;
}
Alright, I copy & tested your code, it works well, maybe somewhere else did wrong...
Create a new "Empty Template" Xcode project with ARC enabled, paste below into AppDelegat.m:
//
// AppDelegate.m
// WebTest
//
// Created by Elf Sundae on 8/5/13.
// Copyright (c) 2013 www.0x123.com. All rights reserved.
//
#import "AppDelegate.h"
#interface SampleViewController : UITableViewController
#end
#implementation SampleViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Sample Controller";
}
#end
#pragma mark -
#interface WebViewController : UIViewController <UIWebViewDelegate>
#end
#implementation WebViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.bounds];
web.delegate = self;
[self.view addSubview:web];
[web loadHTMLString:#"<a href='didTap://button1'><img src='cercle24px.png' /></a>" baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil message:#"alert message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
NSString *urlString = request.URL.absoluteString;
if ([urlString caseInsensitiveCompare:#"didtap://button1"] == NSOrderedSame) {
#define __use_method 3 // it could be: 1/2/3
#if (__use_method == 1)
SampleViewController *controller = [[SampleViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
#elif (__use_method == 2)
/* method 2 */
SampleViewController *controller = [[SampleViewController alloc] init];
[self.navigationController performSelector:#selector(pushViewController:animated:)
withObject:controller
withObject:#(YES)];
#elif (__use_method == 3)
/* method 3 */
__unsafe_unretained __typeof(self) _self = self;
double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
SampleViewController *controller = [[SampleViewController alloc] init];
[_self.navigationController pushViewController:controller animated:YES];
});
#endif
return NO;
}
return YES;
}
#end
#pragma mark -
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:
[WebViewController new]];
return YES;
}
#end

Semantic Issue on setDelegate

There is a Semantic Issue with my code. I have a UIWebView and I added an error message so if there's no internet connection, an error pops up.
This is the coding for my UIWebView in my .m file
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = #"http://example.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[website setDelegate:self];
[website loadRequest:requestObj]; }
The error is on this code
[website setDelegate:self];
It says it's a Semantic Issue and the error is Sending FirstViewController *const_strong to parameter of incompatible type id
Here is the code for the error message if there is no internet connection
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error" message:#"error connecting to the internet" delegate:self
cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show]; }
You need to specify that FirstViewController implements protocol. Something like:
#interface FirstViewController () <UIWebViewDelegate>
#end
The compiler is currently warning you that you're setting a delegate which isn't specifying that it implements the required protocol so no compile check is being performed.
Your view controller should specify that it conforms to UIWebViewDelegate by adding "<UIWebViewDelegate>" to the end of the #interface line, either in the main #interface declaration in the .h:
#interface MyViewController : UIViewController <UIWebViewDelegate>
Or to the private class extension (if you have one) in the .m file:
#interface MyViewController () <UIWebViewDelegate>
See Conforming to Protocols in the Programming with Objective-C guide.

Network Error Alert in Xcode Needed Urgently

I recently started an Xcode project, and its mostly web-based, in the sense it mostly works with UIWebView. I am a total noob at this. I would like to make an "No Internet Connection Alert". Basically An Alert that comes up if you have no internet connection. I have tried using the Reachability but since I'm such a noob, I didn't manage to figure ANYTHING out.. Heres my view controller:
This is my .h file: P.S: My WebView is called 'webone'.
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
-(IBAction)refreshClicks:(id)sender;
#property (weak, nonatomic) IBOutlet UIWebView *webone;
#end
And This is My .m file:
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize webone;
-(void)viewDidLoad {
NSURL *url = [NSURL URLWithString:#"http://www.lostcraft.net/mobile"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webone loadRequest:req];
[super viewDidLoad];
}
-(void)awakeFromNib{ //IGNORE
[self refreshClicks:self]; //IGNORE
}
-(IBAction)refreshClicks:(id)sender{//IGNORE
[webone loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.lostcraft.net/mobile"]]];//IGNORE
}
- (void)viewDidUnload
{
[self setWebone:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
Place this in your .m file
Under Your IBAction:
NSString *web = #"http://YOUR WEB ADRESS HERE";
NSURL *url = [NSURL URLWithString:web];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webdone loadRequest:requestUrl];
Then somewhere in your .m
-(void)webView:(UIWebView *)webdone didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please check your internet connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
go into connections tab in interface builder in xcode
Right click from the delegate as shown in the picture (the top option) and drag it to your view contoller where you have your web view (webdone)

Resources