Posts Tagged ‘iphoneOS’

iPhoneOS SDK – iPad Popover Controller

April 19th, 2010 iPhone SDK Tips


The iPad Popover control is possibly the least publicized feature of the iPhoneOS SDK, but becomes critical to in app actions and workflows. This is because the Popover Controller can show a small UIView of controls and more importantly save the user from having the whole screen change navigation hierarchy. A simple popover for example, can be used to edit a textfield, or change a date really quickly. In an iPhone app, this would typically be done by sending the user to a ‘DetailView’ where the setting/property can be change, whereas on the iPad this is highly discouraged. I am going to go through a quick popover example today, and on monday I will post a fantastic iPad user template.

1. Understanding the context
First, you need to know where a popover should be used. I can’t say much more than whats above. Use the apple apps, use the other top 100 apps, see how they use the popover controller. This will give you a feel of what it should do.
2. Some Code
Firstly, your main view controller {.h} needs a <UIPopoverControllerDelegate> declaration.
In your main view controller, add the following to the @interface (.h file)

//PopoverViewController
UIPopoverController *popoverController;

and just before @end add an IBAction:
- (IBAction)popOverViewCall:(id)sender;

Create a New View & ViewController {.h/.m} files and place them in your project. This is the actual popover that will come up. Place controls how ever you see fit.  I call this popover controller MyViewControllerForPopover. Add an include of this file to your main view controller, eg:
#import “FirstDetailViewControllerPopOverControl.h”

In MyViewControllerForPopop {.m file}, add the following code:
-(void)viewDidAppear:(BOOL)animated
{
self.contentSizeForViewInPopover = CGSizeMake(320,320);

}
This sets the size of the popover for showing.Go into interface builder and add a couple controls to ensure you know when it works.
And finally in your main view controller {.m file}, add the IBAction:

-(IBAction)popOverViewCall:(id)sender{
FirstDetailViewControllerPopOverControl *myViewControllerForPopover = [[FirstDetailViewControllerPopOverControl alloc] initWithNibName:@”FirstDetailViewControllerPopOverControl” bundle:nil ];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:myViewControllerForPopover];
self.popoverController = popover;
popoverController.delegate = self;

[popover release];
[myViewControllerForPopover
release];

CGPoint point = {200,200}; // Place to put on screen
CGSize size = {600,600}; // A content range (see apple docs)
[popoverController presentPopoverFromRect:CGRectMake(point.x, point.y, size.width, size.height)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

Add a button on your main view controller to call the IBAction. That covers it! One of the new features in iPhoneOS3.2 for iPad. See the screenshots for how it should look, code is downloadable below.
















iPhone SDK In App Social Links

March 30th, 2010 iPhone SDK Tips

Screenshot2010-03-29at23.33.19.XD06TIrloGSP.jpg

Whilst we consider ourselves late on the ‘social’ scene, we have decided to add twitter and Facebook links, to us, from inside our apps only. We wish to do this with a small f and a small t button, generally on the about page. See the screenshot of our favorite 1-day project, SavingsGoal.

For usability, we didn’t simply want to just link to the URLS, first closing the app and then opening safari. We wanted to provide a good in app experience. This is why we chose to use PWWebViewController by matthiasplappert. This project creates a new view with a WebView all as a control. So the resultant code is only a few lines.
The result is a in app modular popup, which opens the facebook page, from within the application.

For the twitter button we could have done this too. But we had found something better. Oliver (aka @Dr_Touch) did a pretty nice twitter follow button code piece, which infact detects if any twitter client is installed on the iPhoneOS Device and then opens the twitter page for that user. It has a safe fallover, such that if no twitter client is installed, it then opens twitter.com in a webview.
Note: We did’t want the twitter link to open up straight away, we also wanted to alert the user that they are being taken out of the application.

The post for this code is at : http://www.drobnik.com/touch/2010/02/making-a-follow-us-on-twitter-button/

But of course, this post wouldn’t be complete without a code-sample! So this is how I wrote the social integration buttons, for our apps.

Firstly, add the following projects, and the #includes too.

Inline-Web-Browser: http://github.com/matthiasplappert/iPhone-Inline-Web-Browser
Olivers Twitter Code: http://www.drobnik.com/touch/2010/02/making-a-follow-us-on-twitter-button/

#import “PWWebViewController.h”
#import “deHelpers.h” //Note: This is a method which just has the above code by Oliver.

Then in viewDidLoad, I initiate the strings. Obviously you should change these to what ever you wish.

- (void)viewDidLoad {
//…
//Internet Links
websiteURL = @“http://apps.duivesteyn.net;
supportEmail = @“apps@duivesteyn.net;
facebookURL = @“http://www.facebook.com/apps/application.php?id=107880612574963;
twitterURL = @“de_applications”;
}

Then add the IBActions for the buttons:

#pragma mark -
#pragma mark Social Buttons

-(IBAction)openLinkFacebook{
[FlurryAPI logEvent:@“Pressed Link: Facebook”];
url = [NSURL URLWithString:facebookURL];
[self openURL];
}

-(IBAction)openLinkTwitter{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Twitter” message:@“Do you want to be taken to our twitter feed? \n\nThis will close the app.” delegate:self cancelButtonTitle:nil otherButtonTitles:@“No”,@“Yes”,nil];
[alert show];

}

- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
NSLog(@“in alertView action, buttonIndex: %u”,buttonIndex);

if (buttonIndex==1) {                 NSLog(@“Open Twitter”);
//This is the Dr_Touch twitter follower code. I have just put it in a separate class, and I am calling it from there.
deHelpers *dehelper = [[deHelpers alloc] init];
[dehelper openTwitterAppForFollowingUser:twitterURL];

}
}

-(void)openURL{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
PWWebViewController *webController = [[PWWebViewController alloc] initWithRequest:request];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:webController];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissModal)];
webController.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
[self presentModalViewController:navigationController animated:YES];         [navigationController release];

}

- (void)dismissModal
{
[self.modalViewController dismissModalViewControllerAnimated:YES]; }

And thats it for the code. Don’t forget to add the buttons to interface builder and then link the IBActions.

Hope you find it useful!

.

iPhoneOS Addressbook Create New Client & Get Data

March 5th, 2010 iPhone SDK Tips


I just finished a snippet of code for an upcoming 1 day iphonedev project which is very simple. The snippet needs to show the user an add address book contact box, let the user add the contact info and then Dismiss the add contact window to get on with the rest of the app, and of course saving the data into memory.
The code is attached below, and be sure to be watching the NSLog.




iPhone/iPad Development – Screen Rotation Resizing

March 3rd, 2010 iPhone SDK Tips

There comes a time in many apps where the screen will be rotated and the view will resize to the new screen layout. This will be even more important when it comes to the iPad which recommends that the app is usable on all different orientations.

On rotation, I call a method that then goes and tediously shuffles the frames of all my onscreen items for landscape, and then it needs to be done again when going back to portrait. (There should be a class out there somewhere which automates all this).

The important part is getting the X,Y,Width,Height data of all the frames of the objects and I have lately adapted code to generate the following:

if ([[self.view subviews] count] &gt; 0) { <br />
NSLog(@"Enumerating Subview Details\n%@",[self.view subviews]); <br />
}

<p>What is does is print (to NSLog) a list of all the subviews in the main view. It makes getting the frame X,Y,W,H data much faster. Hope it helps!</p>

iPhone OS – Don’t use dateWithCalendarFormat:timeZone

February 12th, 2010 iPhone SDK Tips

My January iPhone Project was recently ‘not allowed’ onto the app store for a very simple reason:

“3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs.”

The non-public API that is included in your application is: dateWithCalendarFormat:timeZone:.


If you have defined a method in your source code with the same name as the above mentioned API, we suggest altering your method name so that it no longer collides with Apple’s private API to avoid your application being flagged with future submissions.


dateWithCalendarFormat:timezone: is an API I picked up either from a tutorial, or searching google, it is actually an old deprecated Cocoa API call, that works perfectly on the iPhone.

The Solution

More important is the solution. Luckily in this case the solution was straight forward.

Lets first have a look at the offending code:
//below dateWithCalendarFormat is not in iPhone use NSDateFormatter
//Sets a date string well (Incorrect….Apple Says Bad)
NSString *caldate = [[[picker date] dateWithCalendarFormat:@”%B %d, %Y”timeZone:nil] description];
NSLog(@”dateWithCalendarFormat (Apple Says Bad): %@”,caldate);

What to do:
//Trying new solution
NSDateFormatter *formatter = [[[NSDateFormatteralloc] init] autorelease];
[formatter setDateFormat:@"MMMM d, yyyy"];
NSString *caldate2 = [formatter stringFromDate:[picker date]]; 
NSLog(@”NSDateFormatter (Apple Says Good): %@”,caldate2);

Bring on the next API…

iPhone/iPad OS SDK Development – Quick – Print Array in NSLog

February 11th, 2010 iPhone SDK Tips

Printing an Array in NSLog can be really useful.  Just print the array as you would a string, and in NSLog you will get a nice layout of the array. I have used an example array from iPhoneSDKArticles

//Initialize the array.

listOfItems = [[NSMutableArray alloc] init];

//Add items

[listOfItems addObject:@"Iceland"];
[listOfItems addObject:@"Greenland"];
[listOfItems addObject:@"Switzerland"];
[listOfItems addObject:@"Norway"];

NSLog(@” %@”,listOfItems);

iPhone UI – Best UI Elements I have seen

February 10th, 2010 iPhone SDK Tips

This post is also a deviation from the norm, an ever updating page of iPhone/iPad OS elements that I really like.