PDA

View Full Version : [iPhone SDK] Got some questions about urlString...




celebi23
Jun 27, 2008, 12:53 PM
I've got two problem left with my app that I can't figure out. Looked through all of the documentation & can't seem to find the answer.

1) Can't search more than 1 word queries. Here's the code that I have for searching a site

- (void)searchBarTextDidEndEditing:(UISearchBar *)mySearchBar
{
// start your search!
UIApplication *app = [UIApplication sharedApplication];
NSString *nameString = self.mySearchBar.text;

NSString *urlString = [NSString stringWithFormat:@"Site that I am searching", nameString];
NSURL *url = [NSURL URLWithString:urlString];

[app openURL:url];
}

When I emailed Apple's Dev Tech Support, they said
I think you might need to precent-escape all spaces when constructing the URL for searching (%20) should be replaced by the space. There are APIs in NSURL
and NSString that help you do this.

I looked all through any documentation relating to NSURL & NSString & couldn't find anything related to that.

2) And when I do have a valid query entered, any other buttons on the app, like a button to the "About" page, initializes the search process.

Apple Dev Tech Support suggested this
Set some break points in your code and find out who is starting the search.

How exactly would I do that? Any help relating to either problem would be greatly appreciated



Eraserhead
Jun 27, 2008, 12:58 PM
1) take a look at the method NSString's stringByAddingPercentEscapesUsingEncoding:

2) click on the side of the code to add a breakpoint.

celebi23
Jul 7, 2008, 04:52 AM
I've got 2 final showstopping bugs left in my app. I've got a RootViewController button leading to an "About" window. A cancel button for my search bar & the search button on my app. These bugs involve:

*When no search query is entered, Search & Cancel buttons lock up the app when pressed
*When a search query is entered into the search field, the Cancel & About buttons start the search


I've set breakpoints & I think I've located the source of the problem but, I'm not sure how to fix the issue. Trying to get this submitted to Apple by 3pm EST

I've got a breakpoint set at [app openURL:url] below (RootViewController.m)
- (void)searchBarTextDidEndEditing:(UISearchBar *)mySearchBar
{
// start your search!
UIApplication *app = [UIApplication sharedApplication];
NSString *nameString = self.mySearchBar.text;

NSString *urlString = [NSString stringWithFormat:@"Site that I am searching", nameString];

NSString *fixedURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // or any other encoding
NSURL *url = [NSURL URLWithString:fixedURL];

[app openURL:url];
}

When the About button is pressed (with a search query entered), the app stops debguggin at "return self;" - AboutController.m


- (id)init
{
if (self = [super init]) {
// Initialize your view controller.
self.title = @"About";
}
return self;
}


I think I narrowed the problem down to this section in my "RootViewController.m" file

- (id)init
{
if (self = [super init]) {
// Initialize your view controller.
self.title = @"My Awesome App";

UIBarButtonItem* aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(tellParentToPush)];
self.navigationItem.rightBarButtonItem = aboutButton;
[aboutButton release];

}
return self;
}

I'm just at a loss on how to fix these last 2 bugs. Any suggestions on how to tackle this problem would be greatly appreciated

Enuratique
Jul 7, 2008, 08:59 AM
I've got 2 final showstopping bugs left in my app. I've got a RootViewController button leading to an "About" window. A cancel button for my search bar & the search button on my app. These bugs involve:

*When no search query is entered, Search & Cancel buttons lock up the app when pressed
*When a search query is entered into the search field, the Cancel & About buttons start the search


I've set breakpoints & I think I've located the source of the problem but, I'm not sure how to fix the issue. Trying to get this submitted to Apple by 3pm EST

I've got a breakpoint set at [app openURL:url] below (RootViewController.m)
- (void)searchBarTextDidEndEditing:(UISearchBar *)mySearchBar
{
//•• start your search!
UIApplication *app = [UIApplication sharedApplication];
NSString *nameString = self.mySearchBar.text;

NSString *urlString = [NSString stringWithFormat:@"Site that I am searching", nameString];

NSString *fixedURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // or any other encoding
NSURL *url = [NSURL URLWithString:fixedURL];

[app openURL:url];
}

When the About button is pressed (with a search query entered), the app stops debguggin at "return self;" - AboutController.m


- (id)init
{
if (self = [super init]) {
// Initialize your view controller.
self.title = @"About";
}
return self;
}


I think I narrowed the problem down to this section in my "RootViewController.m" file

- (id)init
{
if (self = [super init]) {
// Initialize your view controller.
self.title = @"My Awesome App";

UIBarButtonItem* aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(tellParentToPush)];
self.navigationItem.rightBarButtonItem = aboutButton;
[aboutButton release];

}
return self;
}

I'm just at a loss on how to fix these last 2 bugs. Any suggestions on how to tackle this problem would be greatly appreciated

OK, so I have an app that has a UISearchBar on it. The searchBarTextDidEndEditing method is fired whenever the keyboard is released from being the first responder. So what I did was declare a class level boolean called cancelButtonClicked that I set to YES in the cancelButtonClicked event handler and set to no in the searchButtonClicked event handler (both of these methods simply set the boolean and release first responder duties from the search bar). In my searchBarTextDidEndEditing, I check the boolean on whether I should perform my search or not. That may address issue #1. Of course, this is assuming that you use the UISearchBar's search button to invoke searching, and not a second independent search button on your app (which I would recommend, why reinvent the wheel?)

As for the about button, have you tried using the Interface Builder to create the button, and expose the button in your RootViewController by defining an IBOutlet and hooking that outlet to the button in Interface Builder? There's a good video tutorial here (http://www.iphonedevcentral.org/tutorials.php?page=ViewTutorial&id=19&uid=3928739) that shows how to add interface elements and hook them up to the code behind.
Without seeing more of your code, I can't see whats going on.

One more suggestion, if it's invalid to perform a search with an empty string, try handling the searchBarShouldTextEndEditing method and return searchBar.text > 0 (this will return YES for any string greater than or equal to length 1 and NO for empty strings); this will make it so searchBarTextDidEndEditing not even fire in that case.