Displaying UIButton Not Working iOS 7 Crashes Device - objective-c

What is wrong with my code?? I am somewhat new to the whole jailbreak development process. If anyone could help me with this. I was trying to do a test of something new that I learned and I was trying to display a UIButton that creates a UIAlertView on the home screen.
%hook SBUIController
- (void)finishLaunching
{
UIButton *myButton;
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(25, 85, 100, 35);
[myButton setTitle:#"Test" forState: UIControlStateNormal];
[myButton addTarget:self action:#selector(myButtonPressed) forControlEvents:UIControlEventTouchUpInside];
SBUIController *ui = MSHookIvar<id>(self, "_uiController");
[[ui window] addSubview:myButton];
}
%new(v#:)
-(void)myButtonPressed{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Title of Alert" message:#"Message of Alert" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[theAlert show];
[theAlert release];
}
%end

#selector(myButtonPressed)
simply add : after my button pressed so it become
#selector(myButtonPressed:)

Related

My Tweak Isn't Executing

EDIT:
Running on an iPad Mini on iOS 8.1
I tried a test project to hook viewDidLoad in the app to show a UIAlertView, but nothing happened. Does anyone have idea what could cause the tweak to compile but not run properly?
10 hours of debugging, I'm turning to SO.
There's obviously something wrong with this code, and I have no idea what could be causing it. I'm hooking the TestOut view in the ChineseSkill app, but when I get to the view it acts as if nothing changed from the original app.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface TestOutViewController : UIViewController <UIAlertViewDelegate>
- (UIButton *)quitButton;
#end
%hook TestOutViewController
- (void)viewDidAppear:(BOOL)view {
%orig;
UIButton *infiniteLivesButton = [UIButton buttonWithType:UIButtonTypeSystem];
[infiniteLivesButton setTitle:#"Infinite Lives" forState:UIControlStateNormal];
[infiniteLivesButton setFrame:CGRectMake(/*[self quitButton].frame.origin.x + [self quitButton].frame.size.width, [self quitButton].frame.origin.y + 20*/50, 50, 200, 50)];
[infiniteLivesButton setBackgroundColor:[UIColor redColor]];
[infiniteLivesButton addTarget:self
action:#selector(makeLivesInfinite:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *passTestButton = [UIButton buttonWithType:UIButtonTypeSystem];
[passTestButton setTitle:#"Skip Test" forState:UIControlStateNormal];
[passTestButton setFrame:CGRectMake(infiniteLivesButton.frame.origin.x + infiniteLivesButton.frame.size.width + 10,
infiniteLivesButton.frame.origin.y,
150,
50)];
[passTestButton addTarget:self
action:#selector(skipTest:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:passTestButton];
[self.view addSubview:infiniteLivesButton];
UIAlertView *successAlertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Infinite hearts for the rest of this test." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[successAlertView show];
}
- (void)touchesBegan:(id)began withEvent:(id)event {
UIAlertView *testView = [[UIAlertView alloc] initWithTitle:#"Test" message:#"Test Alert" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[testView show];
}
%new
- (void)makeLivesInfinite:(UIButton *)sender {
[self performSelector:#selector(setNNumberofLeftHearts:) withObject:#1000000];
UIAlertView *successAlertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Infinite hearts for the rest of this test." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[successAlertView show];
}
%new
- (UIButton *)quitButton {
return objc_getAssociatedObject(self, #selector(quitButton));
}
%new
- (void)skipTest:(UIButton *)sender {
[self performSelector:#selector(setNTotalTest:) withObject:#1];
[self performSelector:#selector(CheckOrContinueButtonClick:) withObject:NULL];
[self performSelector:#selector(CheckOrContinueButtonClick:) withObject:NULL];
}
%end
There's no alert views, no added views, no nothing. Nearly anything will help.
Your problem is you have to set the right filters in your tweak's plist file. By default the plist file has a bundle identifier filter com.apple.springboard. You should change this to the bundle identifier of the app you want your code injected.
Furthur information (Just a little):
What is happening in behind? MobileLoader is responsible for patching your code into a program. These plist files tell MobileLoader to patch the code in which program(s), in which framework(s) or in which running process. So your process is the app and not SpringBoard. This link
has good explanation about CydiaSubstrate (MobileSubstrate). You could also have a look at its documentation.

textField does not show in the alertView

Here is my code:
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(12, 45, 260, 30)];
[textField setBackgroundColor: [UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
[theAlert addSubview:textField];
[theAlert show];
[textField release];
[theAlert release];
There is no textFiled in alertView, only a title "Alert" and a button "OK"
I will refer you the Apple Documentation in regards to UIAlertView Class Reference. Specifically
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified
So what this means is you can't add subviews to an instance of UIAlertView the recent UIAlertView still has the addSubview: method is because UIAlertView is a subclass of UIView which has this method, but as of iOS7 the method addSubview: for UIAlertView no longer calls the super method on UIView it just does nothing.
So basically what you are after is not possible.
There are however alertViewStyles that you can use like UIAlertViewStylePlainTextInput which will add a single UITextField to the UIAlertView which you can then access using the method textFieldAtIndex:. However please be aware that you can't really do anything with this again because it is part of the UIAlertViews hierarchy so again it is made to be used as is.
Messing with the UIAlertView hierarchy will get your app rejected from the Apple Review process under
2.5 - Apps that use non-public APIs will be rejected
your background color is clear so It's not showing
[textField setBackgroundColor: [UIColor redColor]];.
and also show default text field
theAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
//or for username and password
theAlert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// or for password
theAlert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Try this
You can use the default alertView with textfield.For that you need to set the alertViewStyle
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
Here message is object of alertView.
You can get the textfield value using the delegate method as shown below
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"]) {
UITextField *username = [alertView textFieldAtIndex:0];
UITextField *password = [alertView textFieldAtIndex:1];
NSLog(#"Username: %#\nPassword: %#", username.text, password.text);
}
}
Or you can set alert.alertViewStyle = UIAlertViewStylePlainTextInput;
This will add a text field for you. You can access it in the
UIAlertView delegate callback by using
UITextField *textField = [alertView textFieldAtIndex:0];
If your using ios 7 then add below code to your set of code
theAlert.alertViewStyle=UIAlertViewStylePlainTextInput;
Full code
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(12, 45, 260, 30)];
[textField setBackgroundColor: [UIColor greenColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
theAlert.alertViewStyle=UIAlertViewStylePlainTextInput;
[theAlert show];

Still Dismissing Alert

I am trying to keep this UIAlertView up after the presses either of the buttons (1 & 2).
Once i click on the "+" button or the "-" button, I can see the UILabel text increment, then it closes the UIAlertView.
This is what i currently am using:
#pragma Alert View Methods
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
[self dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
#pragma count functions
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1 || buttonIndex == 2) {
return;
}
else{
[self dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
self.currentCountButtonCount++;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",self.countButtonCount + 1]];
}if (buttonIndex == 2) {
self.currentCountButtonCount--;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",self.countButtonCount - 1]];
}
}
- (IBAction)countClick:(id)sender {
// tallies and keeps current count number
if (!self.currentCountButtonCount)
self.currentCountButtonCount = 0;
NSString *alertMessage = [NSString stringWithFormat:#"%d", self.countButtonCount];
self.countAlert = [[UIAlertView alloc]initWithTitle:#"Count" message:alertMessage delegate:self cancelButtonTitle:#"end" otherButtonTitles:#"+",#"-", nil];
[self.countAlert show];
}
On my last question someone told me to do it custom and this is what im trying now and it still dismisses the UIAlert.
How can i keep it up while the label changes until they touch the end button?
What are you using is default button of AlertView and after click on that button it will automatically dismissed the alertview.
So you have to create your buttons programatically and add that buttons in your alertview like:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn setTitle:#"+" forState:UIControlStateNormal];
[countAlert addSubview:btn ];
on this btn call your method.
So you have to create two custom button with "+" and "-". and add that buttons in AlertView.
-(void)setAlertValue:(id)sender{
switch ([sender tag]) {
case 1:
{
// currentCountButtonCount++;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",++countButtonCount]];
}
break;
case 2:
{
//currentCountButtonCount--;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",--countButtonCount]];
}
break;
default:
break;
}
}
- (IBAction)countClick:(id)sender {
// tallies and keeps current count number
if (!currentCountButtonCount)
currentCountButtonCount = 0;
NSString *alertMessage = [NSString stringWithFormat:#"%d", countButtonCount];
self.countAlert = [[UIAlertView alloc]initWithTitle:#"Count" message:alertMessage delegate:self cancelButtonTitle:#"end" otherButtonTitles:nil, nil];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 40, 20)];
[btn addTarget:self action:#selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor greenColor]];
btn.tag = 1;
[btn setTitle:#"+" forState:UIControlStateNormal];
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(230, 50, 40, 20)];
[btn1 addTarget:self action:#selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundColor:[UIColor redColor]];
btn1.tag = 2;
[btn1 setTitle:#"-" forState:UIControlStateNormal];
[countAlert addSubview:btn];
[countAlert addSubview:btn1];
[self.countAlert show];
}
I don't want to rain in your parade, but if you think that an alert view is the best way to handle an increment/decrement of a variable, I would suggest you to reconsider your design.
UIAlertViews are meant for transient information and simplified decision making. A simple "Are you sure?" is the text-book example of an alert view usage.
From the user stand point, it's much more comforting being able to modify all the attributes in sliders or any other form of permanent input, and then, when sure, hit the confirm button with an alert view (or confirmation screen). Doing it in an Alert view is not only error prone, but counter-intuitive compared in the way that the rest of iOS works.
If you're having trouble on fitting another form of input in your application, please read on how to perform animations and reveal control as they are needed, hiding the input inside an UIAlertView is simply the easiest solution for you, but not the best for the user.

Actionsheet buttons do not respond when using showInView

I have this code on my program
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *saveMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[saveMessageBtn setImage:[UIImage imageNamed:#"btn_done.png"] forState:UIControlStateNormal];
[saveMessageBtn addTarget:self action:#selector(saveMessage) forControlEvents:UIControlEventTouchUpInside];
[saveMessageBtn setFrame:CGRectMake(0, 0, 49, 30)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveMessageBtn];
}
-(IBAction)saveMessage:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Send Now",#"Non Recurring",#"Recurring", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
NSLog(#"Send Now");
}
else if (buttonIndex == 1){
[self performSegueWithIdentifier:#"modalNonRecurring" sender:self];
}
else if (buttonIndex == 2){
[self performSegueWithIdentifier:#"modalRecurring" sender:self];
}
else{
NSLog(#"Cancel Clicked");
}
}
as you can see in the code, it supposed to perform a segue or do 'NSLog' when a particular button clicked.
But when I click a button, it does not perform what I want it to do, instead it displays this message in the debug area..
Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:].
By the way, I am using a UINavigationController that is inside a UITabBarController.
Anyone that has a great idea how to fix this? your help would be much appreciated. Thanks!
Add UIActionSheetDelegate delegate in .h file and try like this then it'l work.
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Send Now",#"Non Recurring",#"Recurring", nil];
actionSheet.delegate = self;
[actionSheet showInView:self.view];

Custom AlertView With Background

Everybody, I need to set one image on UIAlertView..
I have attached my UIAlertview with image prob..
i have used this code lines..
UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:#"Atention"
message: #"YOUR MESSAGE HERE", nil)
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[theAlert show];
UILabel *theTitle = [theAlert valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];
UILabel *theBody = [theAlert valueForKey:#"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];
UIImage *theImage = [UIImage imageNamed:#"Background.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16];
CGSize theSize = [theAlert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[theAlert layer] setContents:[theImage CGImage]];
please solve this issue..
i need only image with alert..
Try this...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlert View" message:#"hello" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Close",nil];
UIImage *alertImage = [UIImage imageNamed:#"plus.png"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:alertImage];
backgroundImageView.frame = CGRectMake(0, 0, 282, 130);
backgroundImageView.contentMode = UIViewContentModeScaleToFill;
[alert addSubview:backgroundImageView];
[alert sendSubviewToBack:backgroundImageView];
[alert show];
[alert release];
You should consider to not use UIAlertView, but have your own AlertView see TSAlertView for an alternative implementation, that is not derived from UIAlertView.
TSAlertView is allowing you to set your own background image.
Another solution that I am not recommending could be:
You can use introspection: Loop over the UIAlertViews subviews, identify the one that holds the background image set it hidden and place your own backroundimage at an index below/over the original image view.
I found this project: Subclass UIAlertView to customize the look of an alert. It is not working for iOS 4.2+, but with my introspection idea you can make it work again:
change the -layoutSubviews of JKCustomAlert to:
- (void) layoutSubviews {
for (UIView *v in [self subviews]) {
if ([v class] == [UIImageView class]) {
[v setHidden:YES];
}
}
alertTextLabel.transform = CGAffineTransformIdentity;
[alertTextLabel sizeToFit];
CGRect textRect = alertTextLabel.frame;
textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect)) / 2;
textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect)) / 2;
textRect.origin.y -= 30.0;
alertTextLabel.frame = textRect;
alertTextLabel.transform = CGAffineTransformMakeRotation(- M_PI * .08);
}
I am NOT promising, that this trick will work in future versions of iOS
A solution I like to use for this is to add a UIView to the ViewController, which mimics the appearance of an alert.
The other property of a UIAlertView is that no other part of the app can be used until the alert is dismissed. This can easily be mimicked by making your UIView a subview of another UIView (with a clear background), which takes up the entire screen.
If you don't want to implement that yourself, here's a Custom Alert View class you could use: https://github.com/shivsak/CustomAlertView