[Tutorial] Cycript: UIAlertView

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

Thread Status:
Not open for further replies.
  1. hyernado

    hyernado New Member

    Joined:
    Apr 13, 2009
    Messages:
    127
    Likes Received:
    0
    Device:
    2G iPod touch
    Hey,guys.Ihaven'tseenanytutorialsformakingappswithcycriptbefore,soIthoughtIwouldmakeonemyself.Thiswillbeoneinaseriesofcycriptapplicationtutorials.Asthetitleentails,thistutorialwillbeonincorporatingUIAlertViewsintoyourapp.Thishonestlyisvery,verysimple.IfyouknowObjective-CandarecomfortablewithUIKit,thisshouldbeprettyfamiliar.
    Tostart,youwillhavetodownloadandinstallthenewestversionofcycriptfromhttp://cycript.org/debs/,becausetheversiononSaurik'srepositoryisdesignedmainlyforcydgets.Afterthat,grabtheHelloCycriptexamplefromhttp://cycript.org/examples/togiveyouthebaseofyourapp.Now,placeHelloCycript(unzipped)in/Applications/,andrespring.Launchtheapp,justtomakesureitworks.ItshoulddisplayasimpleUITableView.Nothingexciting,butweareabouttochangethat.Openupthe"HelloCycript"fileinsidetheHelloCycript.appdirectorythatyouplacedin/Applications/inatexteditor(IhighlyrecommendiFile),andgotothebeginningofthe"applicationDidFinishLaunching"function,andenterthefollowingcode:
    Code:
    vartitle="Alerttitle!";
    varalertMessage="Alertmessage!";
    varok="OK";
    
    varalert=[[UIAlertViewalloc]initWithTitle:titlemessage:alertMessagedelegate:selfcancelButtonTitle:okotherButtonTitles:nil];
    
    Let'sgooverthiscodepart-by-partnow.
    Code:
    vartitle="Alerttitle!";
    varalertMessage="Alertmessage!";
    varok="OK";
    
    Thisdefinesthreevariables,title,alertMessage,andok.ThesearethevariablesthattheUIAlertViewobjectcallstoforthelabelsofthetitle,thebody,andthedismissbutton.
    Now,wemustallocatetheUIAlertView.
    Code:
    varalert=[[UIAlertViewalloc]initWithTitle:titlemessage:alertMessagedelegate:selfcancelButtonTitle:okotherButtonTitles:nil];
    
    This,asmentionedabove,allocatesthealertviewandtellsittousethevariableswedeclaredpreviouslyforit'slables.
    Then,attheveryendofthesamefunction(rightbeforethe"}"),enterthefollowing:
    Code:
    [alertshow];
    [alertrelease];
    
    Thefirstlineshowstheactualalert.Thisisobviouslycrucial,aswithoutthisline,cycriptdismissesyouralertviewmerelyasanothervariableandignoresit.
    Thesecondlinefreesyouralertfrommemory,whichisgenerallyagoodthingtodo,becauseitmaybeusingonlyasmallbitofyouriDevice'smemory,butitaddsup.
    Now,youcangoaheadandsavethefileandlaunchyourappfromtheSpringBoard.Itshouldwork,andlooklikeastandardalertbox.Now,thisisgoodandall,butwhatifyouwouldliketohavemultiplebuttonsonyouralert,whichalldodifferentthings?Thisisalsonotlimitedtocompiledapplications.It'squitesimple,actually.
    Wewill,inthisexample,closetheapp.
    Firstofall,createanewvariableatthebeginningofyourapplicationDidFinishLaunchingfunction,alongwithyourexistingvariables:
    Code:
    varcloseButton="Closeapp";
    
    Now,rightunderyourUIAlertViewallocationline,typeonemoreline:
    Code:
    [alertaddButtonWithTitle:closeButton];
    
    Now,toaddthecodethatwillclosetheappwhenyoutapthe"Closeapp"button.
    AddthiscoderightafteryourapplicationDidFinishLaunchingfunction:
    Code:
    -alertView:alertView
    clickedButtonAtIndex:buttonIndex{
    if(buttonIndex==1){
    SinceTheSystemFunctionIsntInCycriptYouCanJustUseAnyRandomNon-ExistentFunctionToCloseTheApp();
    }
    }
    
    Let'stakethisapartnow,sinceit'smorecomplexthantheothercode.
    Code:
    -alertView:alertView
    clickedButtonAtIndex:buttonIndex
    
    Thisisthefunctionname.ThisfunctionwillberuneachtimeabuttononyouralertviewotherthanthecancelButtonispressed,andtheIDofthebuttonpressedisstoredinavariablenamedbuttonIndex.Forexample,ifyouhadtwo"addButtonWithTitle"lines,ratherthantheoneinthistutorial,iftheusertappedthefirstbutton,thevalueinbuttonIndexwouldbe1,oriftheytappedthesecondbuttonthevaluewouldbe,ofcourse,2.
    Code:
    if(buttonIndex==1)
    
    Asexplainedabove,thebuttonIndexisthevariablewhichstorestheIDofthebuttonpressed.Theif()function'ssyntaxisprettysimilartoEnglish,soIdon'treallythinkIhavetoexplainthismuchmorethanthis:ifthebuttonIndex=1,performthecodeinthecurly-braces.
    Code:
    SinceTheSystemFunctionIsntInCycriptYouCanJustUseAnyRandomNon-ExistentFunctionToCloseTheApp();
    
    Asexplainedinthefunctionitself,cycriptdoesnothaveasystem()function,soyoucannotrunsystem("killallHelloCycript"),soyoucanjustrunsomerandomfunctionthatdoesn'texist.
    SoIguessthatbringsustotheendofourtutorial.
    -ZappJackson
  2. 42395_skyline

    42395_skyline Community Development Staff Member

    Joined:
    Feb 14, 2009
    Messages:
    10,912
    Likes Received:
    489
    Device:
    iPhone 6
    wtf?! i cant read this.
  3. hyernado

    hyernado New Member

    Joined:
    Apr 13, 2009
    Messages:
    127
    Likes Received:
    0
    Device:
    2G iPod touch
    WTF why did iptf delete all the spaces in my post??
    ------------------double post merged------------------
    Well, whatev. Posted to my blog. At least wordpress doesn't remove whitespace from it's posts.
    [LINK REMOVED: CONTACT BGIZZLE FOR ADVERTISING RATES. AS A FORUM MEMBER, HE SHOULD GIVE YOU A GREAT RATE]
  4. exit1010

    exit1010 Well-Known Member

    Joined:
    Dec 30, 2007
    Messages:
    4,812
    Likes Received:
    48
    Device:
    4G iPod touch
    Talk to BGizzle about that... I know you would not do that on a forum like this if you want us to learn something.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    And there we have it. This thread is nothing but SPAM, designed to drive traffic to that user's blog.
Thread Status:
Not open for further replies.

Share This Page