Xcode - Help implementing some code into a view based app? - iphone

Can someone help me with implementing this code into a view based app?
This is the code that I have:
- (void)webViewUIWebView *)newsweb didFailLoadWithErrorNSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Error1!" message:#"Please confirm network connectivity and try again!" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
With this code and any other code that I may need, but don't know, I would like to know what code to implement into the .h + .m view controller files.
What I am trying to achieve is an error after starting the app if there is no network connection available. What I'm worried about also but not quite sure if it will happen, is that this will repeat the error "x" times for how many UIViews I have. If that would be the case could you also help me should that it would only show the error once.

Put a Boolean in your header file, and when the alert fires, change it. If the Boolean is false, then simply don't show the alert.
#interface
BOOL alertShown;
#end
#implementation
-(void)applicationDidFinishLaunching{
alertShown = NO;
}
-(void)webViewError{
if(alertShown == NO){
//show the alert
alertShown = YES;
}
}
#end

Related

EXC_Bad_Request "only" after calling the uialert 3 or more times

I am a newbie in objective c;
I am getting an error at creating a UIAlertview instantiation statements (UIAlert * myAlert, they are in separate scopes)
I have referred the following stackoverflow entries such as This, this, this too and much more research over the internet.
I am unable to get a break through
Following are my alert calls
This is my view controller code where I have put up the "UIAlertViewDelegate"
#import <UIKit/UIKit.h>
#interface gameFirstViewController : UIViewController<UIAlertViewDelegate>
#end
This is my class declaration
#import <Foundation/Foundation.h>
#interface GameLogic : UIView
//all sorts of various non relevant property declarations
#end
Here is my implementation for the alerts
//action to take when an alert is shown
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger) buttonIndex
{ NSLog(#"OK Tapped");
NSUserDefaults *MySettingsData = [NSUserDefaults standardUserDefaults];
row= [MySettingsData integerForKey:#"Row_count"];
col = [MySettingsData integerForKey:#"Column_count"];
if(buttonIndex==0)
{
for(int i=0;i<row;++i)
{
for(int j=0;j<col;++j)
{
myarr[i][j]=0;
}
}
if(_TimerStatus ==1)
{
[mainTimer invalidate];
mainTimer=nil;
_TimerStatus=0;
}
[self super_reset];
[self setNeedsDisplay];
NSLog(#"Game reset");
return;
}
}
//my usage of the alerts at 2 different places
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: #"GAME OVER"
message:#"You clicked on a mine, tap on ok to reset"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[myAlert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:YES];
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: #"You Won! Whoo Hoo"
message:#"You have successfully dodged every minefield"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[myAlert performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:YES];
I am not sure where I am going wrong, any help would be great!
Thanks.
The way you are creating the UIAlertView is not safe in ARC. It's fine if you use the object right away, but you're passing it the performSelectorOnMainThread method. ARC may dealloc your myAlert variable by the time the main thread gets to perform the selector. That's likely why you're seeing the EXC_Bad_Request.
Either make myAlert a strong property, so it survives, or delay the creation of the UIAlert until the main thread is able to call the show method. You can do that like this:
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:#"GAME OVER" message:#"You clicked on a mine, tap on ok to reset" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
});
Note: I removed your double nil for the titles. I don't think that would make a difference, but just in case.

Simple iPhone jailbreak tweak not working

#import <UIKit/UIAlertView.h>
#class NSObject;
#interface SBIconController : NSObject
+ (SBIconController *)sharedInstance;
- (BOOL)isEditing;
#end
%hook SBIconController
-(void)iconTapped:(id)tapped {
SBIconController *sbic = [objc_getClass("SBIconController") sharedInstance];
if ([sbic isEditing]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Message" message:[NSString stringWithFormat:#"%#", tapped] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
%orig;
}
%end
Above is a simple tweak that I have created with Logos. For some reason after installing, nothing is working, I just can't figure out what the problem is and How can I solve this problem?
Other questions that I have are:
Why do we declare class like SBIconController when there's already a SBIconController class?
Why do we declare it as a subclass of NSObject?
Why don't we just type in SBIconController when we're calling the [SBIconController sharedInstance] instead of [objc_getClass("SBIconController") sharedInstance]?
Thanks a lot for your help!
The code is fine. I tested it (I don't use logos) and iconTapped: method is indeed called when you tap an app icon. But what are you trying to achieve with isEditing? This property indicates whether you are editing SpringBoard (tap and hold an app icon) and when it equals YES method iconTapped: is NOT called when icon is tapped. It's called only when isEditing equals NO. So I suggest you insert alert without if ([sbic isEditing]) to test whether your tweak is working.
As for your other questions:
When dealing with private APIs we don't have headers and will get warnings/errors if we try to use them. In your case it's SBIconController. To solve this problem we can either download headers that others dumped using various tools like class-dump or declare these private APIs yourself. In your case it's latter.
Because SBIconController inherits from NSObject.
You can do it either way. Of course, when you have class declaration you don't need to use objc_getClass. And in your case you don't even need either of this things. You can just use self like you would in any other obj-C method. Your code will look like this:
%hook SBIconController
-(void)iconTapped:(id)tapped {
if ([self isEditing]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Message" message:[NSString stringWithFormat:#"%#", tapped] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
%orig;
}
%end

Prevent AFNetworking Reachability Status Getting Called at First Launch for iOS

I just want to know how to prevent my alert view from appearing everytime I open my application that's already connected to the internet. I'm using ARC if that helps.
This is the code I have in my didFinishLaunchingWithOptions method inside my AppDelegate:
__weak id myself = self; // to silence warning for retain cycle
_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://apple.com"]];
[_httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
{
// Not reachable
NSLog(#"Not connected to the internet");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Not connected to the internet" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:
{
NSLog(#"Connected to the internet via WiFi");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Connected to the internet via WiFi" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:
{
NSLog(#"Connected to the internet via WWAN");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Connected to the internet" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
}
default:
break;
}
}];
Echoing #D80Buckeye: just don't pop an alert for reachability. It's completely unnecessary, and doesn't add anything to the user experience (it's not like the user can do anything to fix a lack of reachability like that). If anything, you could show a non-modal indication of network reachability.
How about creating a global flag
static BOOL g_FirstTime = YES;
and check it before displaying alert view
if (g_FirstTime) {
g_FirstTime = NO;
break;
}
NSLog(#"Connected to the internet via WiFi");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Connected to the internet via WiFi" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
Without knowing the scope of your project it's hard to make a solid suggestion. My whole-hearted suggestion is that you do NOT want to do a pop-up every time the end user bounces back on the network. If you have code implemented that is reliably detecting a refreshed network connection, if I were you, I would put some sort of subtle (but obvious) on-screen indicator like a small icon/image or maybe even a UILabel that says "connected" or "disconnected" or whatever verbiage works for you. As others have suggested I would completely steer clear of instantiating a pop-up of any sort for that alert.
Regarding the code itself - get very familiar with View Controller Programming and iOS App Programming Guide. Between those two documents you can properly map out how to detect and react to when your app comes in and out of the background state, when it launches, when it becomes active, when your views appear/load, etc. Combine those methods & calls with some global state-driven variables and you should be well on your way.
Bottom line is you want to get familiar with those app state documents as well as [[NSNotificationCenter defaultCenter] addObserver:<#(id)#> selector:<#(SEL)#> name:<#(NSString *)#> object:<#(id)#>]
Let me know if that doesn't make sense.

Why is app crashing when showing UIAlertView?

I've implemented the Reachability function in a method that handles all the server requests. I can see through NSLogs that the function works perfectly. However there is never a "pause" within the method which means I can't use the UIAlertView without crashing the program.
I might be going at this the completely wrong way, but I can't find anything else...
Does anybody have an idea of how to get a notification to show somehow?
Thanks in advance
CODE:
-(id) getJson:(NSString *)stringurl{
Reachability * reach = [Reachability reachabilityWithHostname:#"www.google.com"];
NSLog(#"reached %d", reach.isReachable);
if (reach.isReachable == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Passwords don't match."
message:#"The passwords did not match. Please try again."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}else{
id x =[self getJsonFromHttp:stringurl];
return x;
}
return nil;
}
After moving the discussion to a chat, we discovered that your UIAlertView was being called from a background thread. Never do anything related to updating the UI (User-Interface) in a background thread. The UIAlertView updates the UI by adding a little pop-up dialog, so it should be done on the main thread. Fix by making these changes:
// (1) Create a new method in your .m/.h and move your UIAlertView code to it
-(void)showMyAlert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Passwords don't match."
message:#"The passwords did not match. Please try again."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
// (2) In -(id)getJson replace your original UI-related code with a call to your new method
[self performSelectorOnMainThread:#selector(showMyAlert)
withObject:nil
waitUntilDone:YES];

UIAlertView crash by adding clickedButtonAtIndex

I create a class to call UIAlertview show on my screen. I write the UIAlert function in another class. Both these two classes are not my viewController class.
I use this UIAlert, which is a UITextfield inside, to store texts into a plist file.
here is the class to call UIAlert:
#import "Story.h"
#implementation Story
...
+ (void)stage1
{
AlertClass *pointer = [AlertClass new];
[pointer doAlert];
}
here is the class AlertClass.m file:
- (void)doAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
//this makes crash!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
self.storyFlow.text = [alertView textFieldAtIndex:0].text;
}
Before I add UIAlertViewDelegate in the .h and override the method "clickedButtonAtIndex", it works great. However, I need to store some data from the UITextfield inside the alert view. I get crash and don't know the message it responds as following.
Please help me to solve this problem. Thanks.
[crash pic] https://dl.dropbox.com/u/47381923/crash.tiff
do an NSLog on the text you get back from the Alert View to see whether that is the crash or the subsequent 'self.storyFlow.text = ' is causing it. Perhaps self.storyFlow has not been created yet (with alloc/init)