Welcome to the iFans Forums Create Your Account or Ask a Question Answers in 5 minutes - no registration required!

[Tutorial] Cycript: UIAlertView

Discussion in 'iOS Development' started by hyernado, Feb 11, 2010.

  1. hyernado

    hyernado New Member

    Joined:
    Apr 13, 2009
    Messages:
    128
    Likes Received:
    0
    Device:
    2G iPod touch
    Hey, guys. I haven’t seen any tutorials for making apps with cycript before, so I thought I would make one myself. This will be one in a series of cycript application tutorials. As the title entails, this tutorial will be on incorporating UIAlertViews into your app. This honestly is very, very simple. If you know Objective-C and are comfortable with UIKit, this should be pretty familiar.
    To start, you will have to download and install the newest version of cycript from http://cycript.org/debs/, because the version on Saurik’s repository is designed only for cydgets. After that, grab the HelloCycript example from http://cycript.org/examples/ to give you the base of your app. Now, place HelloCycript (unzipped) in /Applications/, and respring. Launch the app, just to make sure it works. It should display a simple UITableView. Nothing exciting, but we are about to change that. Open up the "HelloCycript" file inside the HelloCycript.app directory that you placed in /Applications/ in a text editor (I highly recommend iFile), and go to the beginning of the "applicationDidFinishLaunching" function, and enter the following code:
    Code:
    var title = "Alert title!";
    var alertMessage = "Alert message!";
    var ok = "OK";
    
    var alert = [[UIAlertView alloc] initWithTitle:title message:alertMessage  delegate:self cancelButtonTitle:ok otherButtonTitles:nil];
    
    Let’s go over this code part-by-part now.
    Code:
    var title = "Alert title!";
    var alertMessage = "Alert message!";
    var ok = "OK";
    
    This defines three variables, title, alertMessage, and ok. These are the variables that the UIAlertView object calls to for the labels of the title, the body, and the dismiss button.
    Now, we must allocate the UIAlertView.
    Code:
    var alert = [[UIAlertView alloc] initWithTitle:title message:alertMessage  delegate:self cancelButtonTitle:ok otherButtonTitles:nil];
    
    This, as mentioned above, allocates the alertview and tells it to use the variables we declared previously for it’s lables.
    Then, at the very end of the same function (right before the ”}”), enter the following:
    Code:
    [alert show];
    [alert release];
    
    The first line shows the actual alert. This is obviously crucial, as without this line, cycript dismisses your alertview merely as another variable and ignores it.
    The second line frees your alert from memory, which is generally a good thing to do, because it may be using only a small bit of your iDevice’s memory, but it adds up.
    Now, you can go ahead and save the file and launch your app from the SpringBoard. It should work, and look like a standard alert box. Now, this is good and all, but what if you would like to have multiple buttons on your alert, which all do different things? This is also not limited to compiled applications. It’s quite simple, actually.
    We will, in this example, close the app.
    First of all, create a new variable at the beginning of your applicationDidFinishLaunching function, along with your existing variables:
    Code:
    var closeButton = "Close app";
    
    Now, right under your UIAlertView allocation line, type one more line:
    Code:
    [alert addButtonWithTitle:closeButton];
    
    Now, to add the code that will close the app when you tap the ”Close app” button.
    Add this code right after your applicationDidFinishLaunching function:
    Code:
    - alertView:alertView
     clickedButtonAtIndex:buttonIndex {
     if(buttonIndex==1) {
     SinceTheSystemFunctionIsntInCycriptYouCanJustUseAnyRandomNon-ExistentFunctionToCloseTheApp();
    }
    }
    
    Let’s take this apart now, since it’s more complex than the other code.
    Code:
    - alertView:alertView
     clickedButtonAtIndex:buttonIndex
    
    This is the function name. This function will be run each time a button on your alertview other than the cancelButton is pressed, and the ID of the button pressed is stored in a variable named buttonIndex. For example, if you had two ”addButtonWithTitle” lines, rather than the one in this tutorial, if the user tapped the first button, the value in buttonIndex would be 1, or if they tapped the second button the value would be, of course, 2.
    Code:
    if(buttonIndex==1)
    
    As explained above, the buttonIndex is the variable which stores the ID of the button pressed. The if() function’s syntax is pretty similar to English, so I don’t really think I have to explain this much more than this: if the buttonIndex = 1, perform the code in the curly-braces.
    Code:
    SinceTheSystemFunctionIsntInCycriptYouCanJustUseAnyRandomNon-ExistentFunctionToCloseTheApp();
    
    As explained in the function itself, cycript does not have a system() function, so you cannot run system(”killall HelloCycript”), so you can just run some random function that doesn’t exist.
    So I guess that brings us to the end of our tutorial.
    -Zapp Jackson
  2. paravatis

    paravatis New Member

    Joined:
    Oct 8, 2009
    Messages:
    4
    Likes Received:
    0
    Device:
    2G iPod touch
    hyernado thanx for the great tutorial.
    i have one problem with this:
    i installed cycript from cycript.org/debs , and i put the HelloCycript.app in /Applications/ , fixed the permissions and respringed , but when i try to run the app , all i get is a black screen.
    Help ?
  3. hyernado

    hyernado New Member

    Joined:
    Apr 13, 2009
    Messages:
    128
    Likes Received:
    0
    Device:
    2G iPod touch
    @paravatis After having the black screen, does the app crash? Or does it stay open, but have only a black screen?
    Thanks, Zapp Jackson.
  4. narwal13

    narwal13 Active Member

    Joined:
    Oct 12, 2009
    Messages:
    3,071
    Likes Received:
    5
    Device:
    5G iPod touch
    what...does this allow you to make apps on your iDevice? thats total pwnage if it is working. will be using in future.
  5. hyernado

    hyernado New Member

    Joined:
    Apr 13, 2009
    Messages:
    128
    Likes Received:
    0
    Device:
    2G iPod touch
    @narwal13 yep, 100% uncompiled iPhone apps, via cycript ;)
  6. paravatis

    paravatis New Member

    Joined:
    Oct 8, 2009
    Messages:
    4
    Likes Received:
    0
    Device:
    2G iPod touch
    Normally it crashes right after the black screen...
    I changed interpreter directive in HelloCycript file
    Code:
    #!/Applications/HelloCycript.app/Cycript
    to
    Code:
    #!/usr/bin/cycript
    and now it doesn't crash ,just displays a black screen...
  7. hyernado

    hyernado New Member

    Joined:
    Apr 13, 2009
    Messages:
    128
    Likes Received:
    0
    Device:
    2G iPod touch
    @paravatis Would you mind telling me the name of the deb you downloaded from cycript.org/debs ?
  8. narwal13

    narwal13 Active Member

    Joined:
    Oct 12, 2009
    Messages:
    3,071
    Likes Received:
    5
    Device:
    5G iPod touch
    uncompiled? how do you compile them? :D :D :D :D :D
  9. paravatis

    paravatis New Member

    Joined:
    Oct 8, 2009
    Messages:
    4
    Likes Received:
    0
    Device:
    2G iPod touch
    I downloaded cycript_0.9.304-1_iphoneos-arm.deb(latest) and the results were black screen/crash...
    Then i noticed the "146" from HelloCycript-146.zip and i installed cycript_0.9.146-1_iphoneos-arm.deb (maybe it was only compatible with that version) , but the results were the same :(
    btw i have mobile substrate installed and updated from Cydia. When i run Cycript from mobile terminal it runs normally... :-/
    Any help appreciated
  10. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,846
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    Depending on what you define as "compile."
    The awesome thing about Cycript is that it's a "dynamic" combination of JavaScript and Obj-C, and thus it needs no compiler. Like sh/Python/etc., Cycript parses your app/script and then executes the code as if it were pure (Objective-)C/JS. Theoretically, you could call that last step "compiling," but it really isn't.

Share This Page