Archive

Archive for the ‘Coding’ Category

mySlate for iPad 1.03 Approved

May 20th, 2010

Screen shot 2010-05-20 at 3.56.12 PM.png

Just a heads up for anyone who has it … 1.03 of mySlate for iPad was just approved by Apple and should be propagating its way through the app store now. Here’s the changelog:

 

#WebView:

Added Bookmark Folders

Save Images with 2 finger tap on image

2 finger swipe left/right to go back/forward

#Photos:

Option to randomize the slideshow

#Videos

New display layout

#General

Change password

Contact Support from the app

 

brandon Coding, iPhone

Limiting UITableview Cell Moves Between Sections

May 1st, 2010

CBD4D958-0624-4B0E-9D08-022F7C6C8CDE.jpg

Problem: You want to limit if a tableview cell is ABLE to move to a new section, or is limited to ONLY moving to a new section. For example in mySlate I’m supporting moving bookmarks to a new section/folder. However the bookmarks are laid out in alphabetical order inside a section/folder, so re-ordering them would do no good. Each time you open the bookmarks view they are returned alphabetically from the data store.

Solution:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if( sourceIndexPath.section != proposedDestinationIndexPath.section )
    {
        return proposedDestinationIndexPath;
    }
    else
    {
        return sourceIndexPath;
    }
//Reverse those to support only moving inside a section
}

Of course supporting movement in the tableview is yours to implement.

Source: stackoverflow

brandon Coding, iPhone

Saving an Image from UIWebView

April 30th, 2010

IMG_0049.jpg

I spent the last few days trying to wrap my head around how to save a user selected image in a UIWebView. The concept itself would seem really simple, and if you’re using Safari on the iPad or iPhone it is really simple. You just tap and hold on an image. Simple. Problem is, when you’re developing you’re own application, this functionality doesn’t exist in UIWebView. Don’t believe me? Try it out for yourself. Go ahead… I’ll wait.

Ok, so if you’re still here you must have found out I’m right. The worst part of all this is I found out the hard way… from a user. I rarely save images from the web, so it never occurred to me to try it in my own app (and honestly I thought this was built in freebie functionality). Well, I got an email the other day from a user who was shocked that this didn’t work. He was not nearly as shocked as I was. I was even more shocked to find out that there’s pretty much nobody on the web who has discussed how to do this. Much time was spent searching for a solution, a couple tweets sent with only crickets responding, time was spent filling out a bug report with Apple (radar) only to have it closed without a work around as a duplicate. So, I basically hit a brick wall.

Alright, let’s get down to it. My complaining is done. Well not really. Did you also know its a huge PITA to handle touches in a UIWebView? Like to the point where you’re either having to sub-class UIWindow to trap touches or you’re having to add a view above the UIWebView. I’m going to leave it up to you as to how you want to implement what we’re talking about. I personally decided to not bother with any of it. Why? Well, I’ve already started building in a bunch of gestures, so it just made sense use a gesture recognizer instead of adding in a bunch of code which may or may not end up breaking sooner than later. My hope is that Apple fixes the problem, but I’m not holding my breath. Anyway, choose for yourself. For this example we’ll just follow the KISS method.

Ok, here come the code. First things first, you need to have some kind of way to collect touches. For simplicity we’ll use a UITapGestureRecognizer:

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[self.webView addGestureRecognizer:doubleTap];
[doubleTap release];

What we’ve done is simply define a gesture (two fingers on tap the screen at the same time) and add that to the webview. When the webview detects two fingers on the screen (tap) it’ll fire the doubleTap method which is where we’ll figure out what image has been selected (if any at all).

- (void) doubleTap:(UITapGestureRecognizer *)sender {
	int scrollPosition = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
	CGPoint startPoint = [sender locationInView:self.webView];
	NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", startPoint.x, startPoint.y+scrollPosition];
	NSString *value = [self.webView stringByEvaluatingJavaScriptFromString:js];
	if ([value isEqualToString:@"IMG"]) {
		NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", startPoint.x, startPoint.y+scrollPosition];
		NSString *urlToSave = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
		}

//Do whatever you want - like a UIAlertView or UIPopover to let the user chose what to do with the image
}

So, let’s walk through the code. The first thing we need to get is where we are on the page. Without this vital chunk of information we end up sending the same coordinates to the document so even if we’re at the bottom of the web page, when we tap the top of the screen it sends the coordinates for the header image. Not good. Next we get the location of the tap in the webView itself. After that we determine which element is at the location where the user tapped. Notice we added the scroll position to the y coordinate so again we don’t end up with the same x,y all the time. ‘tagName’ returns back the element type – ‘A’, ‘IMG’, ‘IFRAME’ etc. We only want images right now (you could always grab the a ref if you wanted to do other fun stuff) so we throw an if statement in to check if the element is an image. If it is, we grab the URL of the element and do stuff with it.

When that code is all said and done you’ll end up with an NSString like so:
http://justanotheripadblog.com/wp-content/uploads/2010/04/3GiPad_thumb.jpg
Nice right?

So, that’s how I ended up saving images from a UIWebView without resorting to private API calls or other wacky business.

Sound Off: Did this work for you? Am I crazy and there was a much easier way? Please let me know in the comments. Thanks!

Credits: iCab Blog for the elementFromPoint idea – see Alexander’s comment on 9/1 11:55am, and the support folks at perfectbrowser.com who got me started looking at doing this through javascript.

brandon Coding, iPhone

gCalWall Updates

April 23rd, 2009

gCalWall_128.png

I just wanted to throw up a quick post on the current state of gCalWall. I know there’s some things that are a little “wonky”. Some (minor number) of folks have seen issues with all day events, and some other stuff like blurry images. I have every intention of doing everything I can to fix these issues. I also have every intention of working in some requested improvements. But at the same time, I haven’t made much of anything off of gCalWall wall – so while its a product I intend to continue working on, it’s not a “top” priority.

So, long story short – I’m working on it, but I wouldn’t expect to release anything (aside from a major affects everybody emergency bugfix) until the iPhone 3.0 software is released. Why? Well, there’s some things I have to change for gCalWall in 3.0 to work properly and I’ve already got a development build started. So, rather than try and maintain separate builds, I’m jumping ship from 2.x and moving on to 3.x…

brandon Coding, iPhone

Building an iPhone App Without Interface Builder

February 25th, 2009


Building iPhone Applications without Interface Builder from Troy Mcilvena on Vimeo

Adding this one for my own future reference. The important things to note was really how to remove the references to the NIB files when first building the project.

In the Resouces like locate the info.plist file and right click then go to source view and delete:

key - NSMainNibFile
string - MainWindow

While you’re in he resources list also delete the mainWindow.xib file. Finally open “Other Sources” main.m and you’re going to edit this line:

int retVal = UIApplicationMain(argc, argv, nil, nil);

and make it look like this:

    int retVal = UIApplicationMain(argc, argv, nil, @"Your_App_Delegate_Class");

That pretty well sums up the key points. There’s some good information about UIWindow and UIView in the video as well if you’re not familiar with building your base view without IB. gCalWall was built largely without using Interface Builder at all so I was pretty familiar with most of it.

brandon Coding

Excellent iPhone Email App Tutorial

February 25th, 2009


iphone programming from Joe on Vimeo

Definitely worth the time to watch. Make sure you click on through and say thanks to Joe!

brandon Coding ,

NSCFArray append string errors, distinct Objective-C types

January 22nd, 2009

Picture 1.png

Ran into some weirdness earlier which was absolutely killing me. I’m building a project using SQLite Persistent Objects, and this:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
	dict = [Commands sortedFieldValuesWithKeysForProperty:@"commandname"];
	self.keyLookup = [dict mutableDeepCopy];

	NSMutableArray *array = [[NSMutableArray alloc] init];
	array = [[[self.keyLookup allValues] sortedArrayUsingSelector:@selector(compare:)] retain];

was throwing an error about assigning the values from a distinct Objective-C type. I was able to fake my way around it in another viewController I had a similar issue with, but this time it was a show stopper because when I tried to work with the resulting array later (by adding/removing some stuff from it) I would get another error:

exception: *** -[NSCFArray addObject:]: mutating method sent to immutable object

Well, I was pretty dammed sure I hadn’t asked for my array to be immutable, but I’m guessing somewhere in the code for the persistent object (which I’ll be checking into) there’s a copy happening (or something similar) and its turning the returned data into an NSCFArray. To fix it… I needed to make it a mutable array again:

if (self.commandArray != array){
		[self.commandArray release];
		self.commandArray = [array mutableCopy];

Of course this may be simple if you’re the programming type… but it drove me nuts for a while!

brandon Coding

UISearchBar to be the top item in the view

January 22nd, 2009

Picture 1.png

So what to do if you’re building a view and you have a navigationBar at the top of the view but you want a searchbar at the top instead?

Try this in your viewDidLoad:

 self.navigationController.navigationBar.topItem.titleView = searchBar;

Of course you’ll need to define searchBar in the header file too…

IBOutlet UISearchBar *searchbar;

Along with doing all the NIB file stuff, property declarations, blah blah blah… but you get the idea.

brandon Coding

Had to write a VBScript today

January 13th, 2009

003_p5_det_c
Creative Commons License photo credit: wisof

Had to write a VBScript today to do some funky stuff. Basically we needed to copy in a string of text and then output that text so it would be formatted to go into an email.

Input text looked something like:

x.x.x.0/x Company  How Assigned    Location

The big issue I had with all of this is that there’s spaces in the company names, spaces in the location, spaces in how assigned… so I couldn’t use a space as the delimter. Well, the code is UGLY… but the general concept is I knew I had certain points where I would have more than one space in a row. After the company name I had 2 spaces. After the How assigned I had 4 spaces. So I could create a couple arrays and delimit using those known values. The only other problem I had was how to get rid of the IP address before the company name… well I solved that by getting rid of all the numbers from the section of the array where the company should be. Again this isn’t pretty – but it got the job done.

window.resizeTo 450,500

dim arrHeaders(3), strIP, strCompany, strStatus, strNewContent
arrHeaders(0) = "IP Block:"
arrHeaders(1) = "Customer:"
arrHeaders(2) = "Status:"
arrHeaders(3) = "IDC:"

Sub SetCopyPaste

strPortal = "From the site:"

strline = CopyPaste.value
arrFields = Split(strLine)
arrFields2 = Split(strline, "  ")
cutString = arrFields2(0)
arrFields3 = Split(strline, "    ")

strAlphaNumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
For i = 1 to len(cutString)
strChar = mid(cutString,i,1)
If instr(strAlphaNumeric,strChar) Then
CleanedString = CleanedString & strChar
End If
next

strIP = arrFields(0)
strCompany = CleanedString
strStatus = arrFields2(1)
strIDC = arrFields3(1)
strNewContent = strPortal & vbCrLf & vbCrLf & arrheaders(0) & " " & strIP & " - " & strStatus & vbCrLf _
& arrHeaders(1) & " " & strCompany & vbCrLf & arrheaders(3) & " " & strIDC

CombinedText.Value = strNewContent

End Sub

Obviously there’s VBSCRIPT tags at the top and the bottom… and some HTML to format the page.

The attached file you can grab and see the whole thing with the code and whatnot.

Output text looked like this:
From the site:

IP Block: x.x.x.0/x – How Assigned
Customer: Company
IDC: Location

spam-email

brandon Coding

TabBarViewController with TableViews

January 10th, 2009

Coding can kill
Creative Commons License photo credit: databhi ♪♫

Here’s the solution to that generic problem of creating a tabBarController that has Navigation Controllers / tableViews under each tab.

(SOURCE LINK – See posts 5 & 8 )

Here's the code. Reposted for my future reference in case the source disappears. 

@interface AppDelegate : NSObject  {
    UIWindow *window;
    UITabBarController *tabBarController;
    MyTableViewController *myTableViewController;
    MySecondTableViewController *mySecondTableViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;
@property (nonatomic, retain) MyTableViewController *myTableViewController;
@property (nonatomic, retain) MySecondTableViewController *mySecondTableViewController;

@end

@implementation AppDelegate

@synthesize window, myTableViewController, mySecondTableViewController, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

       tabBarController = [[UITabBarController alloc] init];          // creates your tab bar so you can add everything else to it

       myTableViewController = [[MyTableViewController alloc] init];               // creates your table view - this should be a UIViewController with a table view in it, or UITableViewController
       UINavigationController *tableNavController = [[[UINavigationController alloc] initWithRootViewController:myTableViewController] autorelease];
       [myTableViewController release];                                                              // creates your table view's navigation controller, then adds the view controller you made. Note I then let go of the view controller as the navigation controller now holds onto it for me. This saves memory.

       mySecondTableViewController = [[MySecondTableViewController alloc] init];
       UINavigationController *table2NavController = [[[UINavigationController alloc] initWithRootViewController:mySecondTableViewController] autorelease];
       [mySecondTableViewController release];                                                    // does exactly the same as the first round, but for your second tab at the bottom of the bar.

       tabBarController.viewControllers = [NSArray arrayWithObjects:tableNavController, table2NavController, nil]; add both of your navigation controllers to the tab bar. You can put as many controllers on as you like, but they will turn into the more button like in the iPod program.

       [window addSubview:tabBarController.view];                                              // adds the tab bar's view property to the window
       [window makeKeyAndVisible];                                                                  // makes the window visible
}

- (void)dealloc {
       [tabBarController release];
       [window release];
       [super dealloc];
}                                           // lets go of everything else, thats so your program doesn't create any leaks of memory.

@end

brandon Coding

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.