Calling an URL from SQLite

Discussion in 'iOS Programming' started by busabres31, May 21, 2012.

  1. busabres31 macrumors newbie

    Feb 29, 2012
    Hello all. Having a problem calling an URL from a SQLlite DB. I have currently have the DB working calling text fields from the DB but can't get the URL to be called into the UITableViewCell
    case 0: cell.textLabel.text = animalDetail.animalName; break;
    case 1: cell.textLabel.text = animalDetail.type; break;
    case 2: cell.textLabel.text =; break;
    In the SQLite DB I have 3 fields
    Name (data examples: Bengal Tiger, Dolphin...)
    Type (data examples: Big Cats, Ocean Animals....)
    WebSite (data examples:

    I get the returns in the table cells correctly organized and in the correct labels but when it comes to an URL with a press able hyperlink I can't get it working, only a return of the text link. How can I get the field to come back as a hyperlink?

    Any help would be appreciate it. Thanks all.

  2. waterskier2007 macrumors 68000


    Jun 19, 2007
    Novi, MI
    I may not be the person to answer this and I can't remember the exact terminology, but there is a way you can enable "recognizers" in text fields that will automatically look for things like URls, addresses, phone numbers, etc. and enable them for taps so that they will open in their respective applications. I think that is what you are looking for
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    I would think adding the proper code to the didSelectRowAtIndexPath: delegate method would probably be the easiest. Otherwise, if you really want a hyperlink, you'll need a webView.
  4. busabres31 thread starter macrumors newbie

    Feb 29, 2012
    This is the didSelectRowAtInexPath code within my AnimalViewController.m. For some reason I can't find a straight forward explanation on how to make an entry in SQLite DB a hyperlink. Any help would be appreciated:

    #import "AnimalViewController.h"
    #import "AnimalHouse.h"
    #import "AnimalDetail.h"
    #import "AnimalDataController.h"
    static sqlite3_stmt *selectAnimal_statement = nil;
    @implementation AnimalViewController
    @synthesize aAnimalHouse, listOfAnimals;
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    		// Initialization code
    	return self;
     //If you need to do additional setup after loading the view, override viewDidLoad.
    - (void)viewDidLoad {
    - (void)viewWillAppear:(BOOL)animated { 
    	self.title = aAnimalHouse.animalHouseName;
    	[tableView reloadData];
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations
    	return (interfaceOrientation == UIInterfaceOrientationPortrait);
    - (void)didReceiveMemoryWarning {
    	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    	// Release anything that's not essential, such as cached data
    -(NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
    	return self.listOfAnimals.count;
    -(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
        if (cell == nil) {
            // Create a new cell. CGRectZero allows the cell to determine the appropriate size.
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIndentifier"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	//Get the object and write out the animal name.
    	AnimalDetail *cd = [self.listOfAnimals objectAtIndex:indexPath.row];
    	cell.textLabel.text = cd.animalName; cell.textLabel.numberOfLines = 3;
    	return cell;
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	 if(cdController == nil)
    		 cdController = [[DataController alloc] initWithNibName:@"AnimalData" bundle:nil];
    	 cdController.animalDetail = [self.listOfAnimals objectAtIndex:indexPath.row];
    	 [self.navigationController pushViewController:cdController animated:YES];
    -(void)setAAnimalHouse:(AnimalHouse *)ch {
    	if(aAnimalHouse == nil)
    		aAnimalHouse = [[NSMutableArray alloc] init];
    	aAnimalHouse = ch;
    	[self getListOfAllAnimalsForThisAnimalHouse:aAnimalHouse.animalHouseID];
    - (void)getListOfAllAnimalsForThisAnimalHouse:(NSInteger)animalHouseID {
    	//get the data from the database and fill the array.
    	self.listOfAnimals = [[NSMutableArray alloc] init];
    	sqlite3 *database;
    	//Get the database and connect to it.
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectory = [paths objectAtIndex:0];
        //#1 Change
    	//NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"SQLNavigation.sql"];
    	NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"AnimalInfo.sqlite"];
    	//open the database
    	if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    		const char *sql = "select id, animal, continent, type, typesoffered, reqs, scores, sizein, sizeout, totalin, totalout, website, comments from animals where continentid=?";
    		int returnValue;
    		if(selectAnimal_statement == nil)
    			returnValue = sqlite3_prepare_v2(database, sql, -1, &selectAnimal_statement, NULL);
    		sqlite3_bind_int(selectAnimal_statement, 1, animalHouseID);
    		while(sqlite3_step(selectAnimal_statement) == SQLITE_ROW)
    			//populate the object with the data.
    			AnimalDetail *cd = [[AnimalDetail alloc] init];
    			cd.animalDetailID = sqlite3_column_int(selectAnimal_statement, 0);
    			cd.animalName = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 1)] copy];
                //Added objects to facilitate additional data...
                cd.continent = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 2)] copy];
                cd.type = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 3)] copy];
                cd.typesoffered = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 4)] copy];
                cd.reqs = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 5)] copy];
                cd.scores = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 6)] copy];
                cd.sizein = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 7)] copy];
                cd.sizeout = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 8)] copy];
                cd.totalin = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 9)] copy];
                cd.totalout = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 10)] copy];
       = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 11)] copy];
                cd.comments = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 12)] copy];
    			//Add the object to the array.
    			[self.listOfAnimals addObject:cd];
    		//Reset the statement, so it can be used again.
    	//Close the connection.
    - (void)dealloc {
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Probably because there is no straight-forward answer. UIKit is not an HTML-based framework so there's no simple way, outside of a UIWebView, to "make an entry a hyperlink", where "entry" means, presumably, an NSString that happens to contain a URL.

    If you want this hyperlink to appear and act like a regular hyperlink, with underlined, colored text, I would suggest (one simple way would be) encoding it as an HTML link in a UIWebView.

    If you simply want to display the URL as plain text in your table cell, I would suggest setting the textLabel to the URL and then, in didSelectRowAtIndexPath:, detecting that a URL row was clicked and opening a browser (either internally or redirecting to mobile Safari) using the desired URL.
  6. busabres31 thread starter macrumors newbie

    Feb 29, 2012
    No it's definitely not straight forward but thank you for the advise. Still trying to figure out a good way to code this. The search continues...:)
  7. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    I gave you two possible approaches. Does neither of those sound suitable?

Share This Page