Results 1 to 6 of 6

Your favorite Apple, iPhone, iPad, iOS, Jailbreak, and Cydia site.


Thread: Tiny question (LaunchMe sample code related)

  1. #1
    Default Tiny question (LaunchMe sample code related)
    You know in the "LaunchMe" sample code how you can open Safari & it'll go to apple.com by default? Is there any way to make a UIButton have that function, without all of the messageAlert stuff from LaunchMe? I'm pretty much trying to get a UIButton to open a link to my website in MobileSafari

  2. #2
    sure.
    you have to use the addTarget:action:forControlEvents method on the button so the button invokes a method when it is clicked.
    in that method you just write

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://yoururl.com"]];

    and it should work. I never tried it, I'm just guessing from the LaunchMe-Code, but I guess that should work.

  3. #3
    Something like this maybe?

    Code:
    }
    
    - (void)imagePressed:(UIImage *)imagePressed didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex != -1) {
            // Open Safari only if the user clicked the 'Launch Safari' button, but not if this
            // delegate method is called by UIKit to cancel it. In that case, buttonIndex is -1.
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:_____________"]];
        }
    	[imagePressed release];
    }
    It produces no error but, doesn't launch the url when the button is clicked

    Here's the code for the button mentioned above

    Code:
    	#pragma mark
    	#pragma mark White Button
    	#pragma mark
    	// create the UIButtons with various background images
    	// white button:
    	UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
    	UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
    	frame = CGRectMake(xCoord, 300, kStdButtonWidth, kStdButtonHeight);	
    	[self addButtonWithTitle:	@"eMail Me"
    								selector:@selector(action:)
    								frame:frame
    								image:buttonBackground
    								imagePressed:buttonBackgroundPressed
    								darkTextColor:YES];

  4. #4
    Quote Originally Posted by celebi23 View Post
    Something like this maybe?

    Code:
    }
    
    - (void)imagePressed:(UIImage *)imagePressed didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex != -1) {
            // Open Safari only if the user clicked the 'Launch Safari' button, but not if this
            // delegate method is called by UIKit to cancel it. In that case, buttonIndex is -1.
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:_____________"]];
        }
    	[imagePressed release];
    }
    It produces no error but, doesn't launch the url when the button is clicked

    Here's the code for the button mentioned above

    Code:
    	#pragma mark
    	#pragma mark White Button
    	#pragma mark
    	// create the UIButtons with various background images
    	// white button:
    	UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
    	UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
    	frame = CGRectMake(xCoord, 300, kStdButtonWidth, kStdButtonHeight);	
    	[self addButtonWithTitle:	@"eMail Me"
    								selector:@selector(action:)
    								frame:frame
    								image:buttonBackground
    								imagePressed:buttonBackgroundPressed
    								darkTextColor:YES];
    well, yeah, that can't really work. I would be surprised if that doesn't produce any errors.
    first of all: there is no such thing as a "imagePressed"-method. just because there is a method for UIAlertView-Buttons for that that doesn't mean a method like that exists for everything. there is no imagePressed-method and therefore it is never invoked.
    secondly: there is no addButtonWithTitle: method. I mean there is one, but only to add buttons to a UIViewAlert, not to add buttons to anything else. so there is no such thing.
    I would heavily advise you to just search the iPhone OS documentation for those things next time, because you find really all the infos you need there (help->xcode documentation or something like that in xcode).
    what you have to do: create a UIButton object (for that look for UIButton in the iPhone OS Docs) and then use addTarget:action:forControlEvents on that button (look for that method in the docs).
    you can look at the "Hello World" example from apple to see how buttons work and how you notice a click on a button.

    regards

  5. The Following User Says Thank You to BlackWolf For This Useful Post:

    celebi23 (2008-04-04)

  6. #5
    Quote Originally Posted by BlackWolf View Post
    well, yeah, that can't really work. I would be surprised if that doesn't produce any errors.
    first of all: there is no such thing as a "imagePressed"-method. just because there is a method for UIAlertView-Buttons for that that doesn't mean a method like that exists for everything. there is no imagePressed-method and therefore it is never invoked.
    secondly: there is no addButtonWithTitle: method. I mean there is one, but only to add buttons to a UIViewAlert, not to add buttons to anything else. so there is no such thing.
    I would heavily advise you to just search the iPhone OS documentation for those things next time, because you find really all the infos you need there (help->xcode documentation or something like that in xcode).
    what you have to do: create a UIButton object (for that look for UIButton in the iPhone OS Docs) and then use addTarget:action:forControlEvents on that button (look for that method in the docs).
    you can look at the "Hello World" example from apple to see how buttons work and how you notice a click on a button.

    regards
    THANK YOU!!!!!!! I finally got the email button to work 1 major feature down, 1 more major one left to got & then I'm all set for version 1.0 of my app

    I do have 1 other question though, I'm using the HelloWorldClassi app as a base & I'm trying to get whatever text I enter to be entered into Google

    Code:
    //This method is invoked when the Hello button or return key is touched
    - (void)hello:(id)sender
    {
        [self.textField resignFirstResponder];
        NSString *nameString = self.textField.text;
    	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/search?hl=en&q=",nameString]];
    }
    But, I keep on getting this one error
    Code:
    error: too many arguments to function 'URLWithString:'
    Any ideas?
    Last edited by celebi23; 2008-04-04 at 03:32 AM.

  7. #6
    Quote Originally Posted by celebi23 View Post
    THANK YOU!!!!!!! I finally got the email button to work 1 major feature down, 1 more major one left to got & then I'm all set for version 1.0 of my app

    I do have 1 other question though, I'm using the HelloWorldClassi app as a base & I'm trying to get whatever text I enter to be entered into Google

    Code:
    //This method is invoked when the Hello button or return key is touched
    - (void)hello:(id)sender
    {
        [self.textField resignFirstResponder];
        NSString *nameString = self.textField.text;
    	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/search?hl=en&q=",nameString]];
    }
    But, I keep on getting this one error
    Code:
    error: too many arguments to function 'URLWithString:'
    Any ideas?
    Indeed there are too many arguments... notice your "," before nameString. I would suggest you build your complete URL variable and then use that in your "URLWithString" statement.

    Something like.... (mind you I have no idea if it works...)
    Code:
        NSString *nameString = [[NSString string] stringByAppendingString: @"http://www.google.com/search?hl=en&q=" 
        stringByAppendingString: self.textField.text];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nameString]];
    Here's how my navbar buttons call my help page and my donate page:
    Code:
    	case 1:	//
    		switch( aButton )
    		{
    			case 0:	{
    				NSURL *donateURL = [NSURL URLWithString:DONATE_URL_STRING];
    				[self openURL:donateURL];
    				[self terminate];
    				break; //Donate
    			}
    			case 1: {
    				NSString *helpLink = [NSURL URLWithString:INFO_URL_STRING];
    				[self openURL:helpLink];
    				[self terminate];
    				break; //FAQ
    				}
    		}
    		break;
    	}
    Where both "*_URL_STRING"'s are constants set in the header file.

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •