Pages

Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Wednesday, April 14, 2010

Gesture recoginization in iPhone SDK

For many magazine applications running on iPad, you easily see that the application detect your gestures for interactions on the content: rotate, swipe right to left (or left to right), tap. If we have to handle these gestures manually by code, it is not efficient.

So, Apple tries to make our developer life easier to breath by providing us a built-in mechanism to detect these common gestures on the latest iPhone SDK version.


You can find these APIs from UIGestureRecognizer class.
"A gesture-recognizer object (or, simply, a gesture recognizer) decouples the logic for recognizing a gesture and acting on that recognition. When one of these objects recognizes a common gesture or, in some cases, a change in the gesture, it sends an action message to each designated target object" - from Apple API documentation.


This class derives to:
1. UITapGestureRecognizer: presents the gesture that p fingers tapped the screen q times consecutively.
  • Input properties: numberOfTaps (q), numberOfFingers (p)
  • Output properties: location, touches
2. UISwipeGestureRecognizer: represents the gesture that p fingers tapped the screen q times consecutively.

3. UIRotationGestureRecognizer: looks for rotation gestures involving two touches. When the user moves the fingers opposite each other in a circular motion, the underlying view should rotate in a corresponding direction and speed.

4. UIPinchGestureRecognizer: represents the gesture that 2 fingers moving towards or away from a center point. This gesture is usually used for scaling a view.
  • Input properties: scaleThreshold
  • Output properties: scale, velocity, anchorPoint, transform
scaleThreshold is the critical scale the fingers must first move apart/together before this recognizer could fire.

5. UILongPressGestureRecognizer: represents the gesture that p fingers moved/stayed on the screen continuously for T seconds.
  • Input properties: numberOfFingers (p), delay (T), allowableMovement
  • Output properties: touches, centroid, startPoint
6. UIDragRecognizer: represents the gesture that 1 or more fingers moving on the screen for a distance of d. Optionally it may also be constrained to moving only within an angle of θ ± Δθ.
  • Input properties: minimumDistance (d), angle (θ), maximumDeviation (Δθ), restrictsToAngle
  • Output properties: touch, startPosition, startAngle
Note that the angles are in radians.
7. UIPanGestureRecognizer: represents the gesture that 1 or more fingers moving on the screen for a relatively large distance. This gesture is usually used for scrolling, and is used by UIScroller and UIScrollView.
  • Input properties: directionalLockEnabled
  • Output properties: offset, velocity, transform
8. UITapAndAHalfRecognizer: represents the gesture that 1 finger tapped the screen q times consecutively, and then moved/stayed on the screen.
  • Input properties: numberOfFullTaps (q), allowableMovement
  • Output properties: touch


An example for how to use these API:
UISwipeGestureRecognizer *swipeRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(DidSwipe:)] autorelease];

By default this line of code detect the swipes from left to right.

If you want to detect a swipe from right to left add the below line after the above line:
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;

On 1 view, you can add many gesture recognizers to handle different interactions by using below function
[self.view addGestureRecognizer:swipeRecognizer];

After that, just implement the [DidSwipe] method we already registered above:
- (void) DidSwipe:(UISwipeGestureRecognizer*)sender{
   Do something...
}


Refs:
+ http://iphonedevwiki.net/index.php/UIGestureRecognizer
+ iPhone SDK 3.2 API document

Happy coding!

Wednesday, December 2, 2009

iPhoneARKit and Point Of Interest apps on iPhone

A point of interest, or POI, is a specific point location that someone may find useful or interesting. An example is a point on the Earth representing the location of the Space Needle, or a point on Mars representing the location of the mountain, Olympus Mons.

The term is widely used in cartography, especially in electronic variants including GIS, and GPS navigation software. In this context the synonym waypoint is common.
(from wikipedia)

iPhoneARKit is a great tool for developing POI (Point Of Interest) apps on iPhone. We've just done the first demo based on this library. It's a simple application to detect POI locations in camera view and show related information (in the context of this demo - is shops with coupons)


Bee Buzz Finder from Vinh Nguyen on Vimeo.


Many business ideas comes from this, right?

Saturday, November 21, 2009

Build iPhone application by Flash CS5

This week, we need to decide whether we should choose putting Flash Professional CS5 into research/training targets of next month. Actually, we already had an iPhone team - with senior developers - has much experience on many projects. However, some Flash developers in our company  want to join to iPhone development. So, we need to consider learning roads to help them to join this interesting domain in shortest time.


How Flash CS5 supports developers to create iPhone application

Adobe creates a very clever solution to enable your Flash content to run on the iPhone. They don't create a Flash Player and run your Flash content inside. The Flash Professional CS5 support a iPhone packaging tool converts your Flash content into a real iPhone application. If you have experience on Unity 3D iPhone, you can imagine this as the same packaging tool.

Advantages of Flash CS5

+ Flash Professional CS5 is the only Windows development environment for iPhone Applications. We don't need to prepare expensive Mac computers for iPhone development.
+ There are more than 2 million Flash designers/developers compared to the 65,000 iPhone developers. The Flash community is so big, so it's easier to find solutions for technical issues in development.
+ There are millions of Flash games begging to be converted to run on the iPhone. => this forces Adobe must find a way to support this. It's not temporary, it's a real trend => Adobe solution must be persistent in a long time.
+ Familiar environment/languages with Flash developers => easy to learn.

Disadvantages

+ Adobe can not catch up with new advanced features in latest iPhone SDK. Flash developers can not use latest technologies from Apple immediately.
+ Currently, some video formats are not supported in the Adobe conversion solution (H264 is an example).
+ You cannot use the cool Flash PixelBender technology released last year in your iPhone Apps at this time.
+ The iPhone itself uses a fraction of the power compared to a PC. Be careful what you program. It may look good on your laptop but barely run on your iPhone.

Final conclusion

Flash CS5 for iPhone development is a great news. However, we can not choose something not stable for production. We need to try more before making decision. This will be a research target in R&D team later. Any new technology always needs time for growing and being proved its strength in the real life.

We're very happy to receive your feedback or any good news from this tech.

Thursday, November 12, 2009

How the iPhone locates you?

This article is for my beloved developers - who always want to know what the truth behind the stage is.

All content in this article is based on  iPhone cookbook - a great book for iPhone developers.




Methods for positioning used in iPhone

The iPhone uses three (or possibly four) methods to locate you.These technologies including:
+ GPS Positioning
+ WiFi Positioning
+ Cell Tower Positioning
+ Internet Provider Positioning

GPS Positioning

GPS system tracks your movement courtesy of a series of medium Earth orbit satellites provided by the U.S. Department of Defense. These satellites emit microwave signals, which your iPhone picks up and uses to triangulate your position to a high level of accuracy. Like any GPS system, this requires a clear path between you and the satellites, so it works best outdoors and away from trees.

WiFi Positioning

Core Location’s preferred pseudo-GPS geopositioning method calls on Wifi-positioning service provided by SkyHook Wireless.

SkyHook offers extremely accurate WiFi placement. When an iPhone knows the WiFi and WiMax routers you are near, it uses their MAC addresses to search SkyHook’s databases, positioning you from that data.

This way provides pretty accurate positioning and can usually locate you within a few hundred feet of your actual location

Cell Tower Positioning

Currently, iPhone use cell tower positioning service through:
+ Google Maps cell tower positioning.
+ SkyHook cell tower positioning (not sure 100%).

By this way, the iPhone uses its antenna to find the nearest four or five cell towers (of the service providers) and then triangulates your position based on the cell tower signal strength. You’ve probably seen cell tower location in action; it’s the kind that shows you about a half mile away from where you are standing—assuming you’re not standing right next to an actual cell tower.

Internet Provider Positioning

This approach uses an Internet provider location to find the nearest mapped Internet provider’s central office. The returned data is typically up to several miles off your actual location—unless you happen to be visiting your Internet provider.
SkyHook actually offers a third positioning approach, but it is never seen the iPhone use.

Hybridizing the Approaches

So, what iPhone acutally works with 4 above methods?

The iPhone approaches location in stages. Based on the accuracy level you request, it uses a fallback method. If it cannot accurately locate you with GPS or SkyHook WiFi mapping, it falls back to the cell tower location of Google Maps. If that doesn’t work, it presumably falls back further to SkyHook Internet provider location. And if that doesn’t work, it finally fails. The latest releases of the SDK actually provide multiple (asynchronous!) success callbacks for each of these fallback methods. You may receive three or four results at any time.

Thursday, October 8, 2009

Research topic for this month: iPhone AR

AR is not a new technology nowadays. However, its applications are very interesting and useful. To adapt the market trend, I decide to have a quick look into AR technology on iPhone.

Below is a very interesting demo that I've found on Internet.




Some quick notes for iPhone AR technology

Third party providers provide us solution to build iPhone AR app



1. iPhone ARKit: open source iphone AR
Ref links:
http://github.com/zac/iphonearkit/
http://www.iphonear.org/
http://code.google.com/p/iphonearkit/


2. SREngine
SREngine stands for Scene Recognition Engine, which is a software engine to recognize static scenes of architectures, streets, posters, rooms, and so on. Due to the ability to specify what user is.



3. ARToolKit v4.4 iPhone
The ARToolKit v4.4 is the first fully-featured AR framework that supports native operation on Apple iPhone, which lets you create applications with sample real world imagery captured with your iPhone camera, and merges them with software-generated 3D objects. The ARToolKit is also well integrated with the accelerometer of the iPhone, which allows it to automatically detect movements from the iPhone, and impose those movements on 3D images instantaneously.


4. D'Fusion Mobile - a solution provided by T-Immersion company
(http://demos.t-immersion.com/)
D’Fusion mobile will operate seamlessly on Symbian and Windows devices, with iPhone and Android support following thereafter. Providing a simple and user friendly experience on mobile phone Total Immersion offers turn key solutions adapted to your needs. Put your smartphone in front of a marker and get a unique 3D Augmented Reality experience!


5.Metaio Mobile
Link: http://www.metaio.com/products/mobile/

Unifeye Mobile is a glimpse of the (virtual/real) future - through the viewfinder of your cell phone's camera! With Unifeye Mobile, metaio is developing the first fully-integrated Augmented Reality software for mobile devices.

Latest news from Apple with AR technology:

Link: http://arstechnica.com/apple/news/2009/07/augmented-reality-coming-to-iphone-with-iphone-os-31.ars

Developers should be able to use official APIs in iPhone OS 3.1 to create so-called "augmented reality" apps for the iPhone 3GS, according to comments from one developer. Already, developers are trying to find uses for such apps

Wednesday, September 23, 2009

Meditation Garden iPhone app and SiO2


I love this app. It inspires me to develop app with SiO2 game engine!

MeditationGarden from SIO2 Interactive on Vimeo.

Saturday, July 18, 2009

iPhone OS - drawing image and stupid thing from Apple

Just because of a piece of code, a team in Singapore gave up an iPhone project. Actually, they do not know how to play tricks with Apple. ... and I also faced to a similar situation when I play as a technical consultant to fix this stupid bug.

The need of client is: drawing some effects on an image and save it as a photo in Photo library. So, if anyone has experience with graphic programming may think this is a very simple task. However, that's not true.

Many forums and online articles just shows us a real problem on iPhone graphics programming: the root position (0,0) of the image to draw is on the bottom left and the root position of the graphic context is on the top-left corner. So, we must do some transformation to keep the image in the correct position.

And the simple code to fix this issue is as below:
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
//create the rect zone that we draw from the image
CGRect imageRect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);
//Do stupid stuff to draw the image correctly
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, imageRect, image.CGImage);
//After drawing the image, roll back all transformation by restoring the 
//old context
CGContextRestoreGState(context);
DO OTHER EFFECTS HERE
//get the image from the graphic context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//commit all drawing effects
UIGraphicsEndImageContext();
Above piece of code is thing that you see in all online technical articles related to iPhone graphic programming. Anyway, that's not enough. If you deploy that piece of code into iPhone OS 3.0 on real device, it does not run correctly: saved images are always scaled in horizontal and inverse. However, there is a very strange thing: this code is run perfectly on simulator for iPhone OS 3.0.

After 1 day to detect the problem, I found that: when an image is shown on iPhone, it has a direction. Direction is included: UP, DOWN, LEFT, RIGHT. So, we must to fix the code to satisfy when they're in those cases. The above code is run correctly in case image direction is UP.

Here is the fixed version:
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
//create the rect zone that we draw from the image
CGRect imageRect;

if(image.imageOrientation==UIImageOrientationUp 
|| image.imageOrientation==UIImageOrientationDown) 
{
    imageRect = CGRectMake(0, 0, width, height); 
}
else 
{
    imageRect = CGRectMake(0, 0, height, width); 
}

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);

//Do stupid stuff to draw the image correctly
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);

if(image.imageOrientation==UIImageOrientationLeft) 
{
    CGContextRotateCTM(context, M_PI / 2);
    CGContextTranslateCTM(context, 0, -width);
}
else if(image.imageOrientation==UIImageOrientationRight) 
{
    CGContextRotateCTM(context, - M_PI / 2);
    CGContextTranslateCTM(context, -height, 0);
} 
else if(image.imageOrientation==UIImageOrientationUp) 
{

//DO NOTHING

}
else if(image.imageOrientation==UIImageOrientationDown) 
{
    CGContextTranslateCTM(context, width, height);
    CGContextRotateCTM(context, M_PI);
}

CGContextDrawImage(context, imageRect, image.CGImage);
//After drawing the image, roll back all transformation by restoring the 
//old context
CGContextRestoreGState(context);
DO OTHER EFFECTS HERE
//get the image from the graphic context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//commit all drawing effects
UIGraphicsEndImageContext();
After fixing this bug, I can say only one thing: why Apple always do something stupid and make developer do the stupid thing?

Thursday, July 9, 2009

Build and deploy iPhone application with offical and unofficial ways

I always call myself a 2 faces man because I accept both 2 things at the same time for everything: black and white, the good and the bad, angel and evil. In my point of view: nothing is only quite 100% good or bad and we must accept that truth in the real life.
As the title of this article, I will share you 1 trick and 1 official way to build your iPhone application. Trick is the bad, and the official way is good (if you accept to pay 99$/month for iPhone developer account).

Section 1: the good - how to build your iPhone application on XCode as official way of Apple

Below is the process to build and distribute your application on the real iPhone
If you have the iPhone developer account, please go to the iPhone developer program portal and access to below link:
http://developer.apple.com/iphone/manage/distribution/index.action

It has very detail guideline to help you in step by step. Just follow them.

Section 2 (the bad) build your iPhone application on XCode without IDP membership

This trick is only used in case you don't want to pay money (99$/month) for the developer account. However, I must warn you about some limitations as below:
1. You can not upload your iPhone application into AppStore. (sure. because it is an unofficial way)
2. You can only deploy and run your build app in iPhone OS <= 2.2. Currently, I can not run it in iPhone OS 3.0.
3. The iPhone SDK you can use for this trick is only from: 2.0 - 2.2

And ... now, below is the link that guide you about the trick:
http://www.vinodlive.com/2009/01/16/how-to-deploy-an-iphone-application-without-an-idp-membership/

Actually, the above trick does below things:


1. Create a Signing Identify on your Mac by following this simple guide from Apple.
In this step, we create a certificate (actually, it is not approved by Apple) in our computer and use it to sign for our code.
2. TextEdit the /Developer/Platforms/iPhoneOS.platform/Info.plist file with some special keys and values to skip checking mapping between the certificate and the provisioning profile.

3. Change the Project Settings . In the Properties List, find the Code-Signing-Identity and Select the Certificate Name(iPhone Developer) you created earlier. This step helps you sign your code with the created certificate.
4.  Add the a snippet on to your projects info.plist using TextEdit to cheat the compiler


If you succeed with above article, you're a lucky man. If you're failed, just do a small step in addition:
Go to Project/Edit project settings - check true for Generate profiling code.

I succeed with both ways.  So I write this article to share with you my experience. If you have any other "bad" or "good" ways, please share me.

Monday, July 6, 2009

Deploy app on iPhone - no wifi - no appstore with full explanation

I received an iPhone (OS 3.0) 2 days ago. My new task is: learning how to deploy our iPhone applications on this device. There is no wifi at my home. Our application is also not uploaded into Apple App Store. So, I need a way that:
1. Only need to copy our application into iPhone and run.
2. No need wifi to copy and setup

Is that difficult?

I read tons of articles and search on many forums but when I follow their steps (no understanding) I got different failed results.

Finally, I find a way after analyzing from many sources. Below is the list of step that I will guide you with my explanation. Anyway, I write this article just for iPhone developers (not for end user). However, in case you are an end user, you can also find some helpful information to solve your problem here.

The solution is:
Connect to your iPhone by using SSH with root account. With the console on SSH, you have the full access right to run any commands (supported by the kernel system behind) to copy/set access right/install/remove your software. However, if you have no wifi - you need a software to bind your an available port on your computer with port 22 (of SSH) on iPhone. The soft to do that is called iTunnel.
iTunnel is worked based on iTune. You should choose to install latest version of iTune to make sure this works ok.
Below is the list of step you need to follow:

Step 0: install latest version of iTune in your computer


This is very important. Without iTune, iTunnel can be run on command line ok. However, when you use SSH to connect to the available port - it will throw error.
In the past, I think this trick does not need installing iTune anymore but I found a problem when I help my team member to do this. At last, we found that the difference between our computer is the iTune version. You must download the latest version of iTune and install it on your computer to make sure this trick works.

Step 1: Install OpenSSH on your iPhone

You need wifi only in this step. To do this, you just need to connect to wifi - go to Cydia - search OpenSSH and install this package. The final result of this step is: OpenSSH will be run as a service in background and open the port 22 to connect. Anyway, you need to check whether the service is turned on or off. To do this, you need to install another software to check. I recommend you should search BossPref (also using Cydia to install).

Step 2: Download and run iTunnel

You can search on Internet to download this software into your PC. I recommend a link that you can download as below:
http://www.makkiaweb.net/blog/2008/09/iphone-tunnel-suite-27

After downloading this, you only need to unzip into a folder. Open the console and go to this directory. Type into the console:

iTunnel.exe 22 [any available port on your computer]
The final result of this step is: iTunnel binds port 22 on your iPhone with a port on your computer.

Step 3: Use any SSH client to connect to the port (on step 2) with root account.


I recommend that you should use WinSCP. The password of root account is: alpine

Example: if I use the port on my computer to bind is: 9000. I will login to SSH on my computer with below information:
HOST: 127.0.0.1 (localhost)
Account: root
Pass: alpine
Port: 9000

After this step, you can see a window (if you use WinSCP) with 2 panels. Left is for your computer, right is folder structure on iPhone. You can copy your app and set execute rights on this. You can also go to menu, open terminal to run any commands you like with root account on iPhone.

Good luck! If you have any problem, leave comments here and I will help.

Update for common questions:
Question 1: I meet "bind error" on the command line when I run iTunnel.
Answer: The port you want to bind was used by another program. So, please try with another port. See step 2 with the command line

iTunnel.exe 22 [any available port on your computer]
After you run the iTunnel successfully, use above port to connect with WinSCP.

Saturday, June 13, 2009

iPhone programming kungfu

I have a bad habit: before starting learning, researching something, I always want to know what is the highest level of the expert in that domain. Usually I map the research domain into kungfu. I call this bad habit because it often takes my time to know that.
In recent months, we setup a team to research iPhone programming. So, this is a good chance for me to learn a new kungfu. After a few months, I identified the Kungfu levels of iPhone programming. It helps me to know what our current level is to improve more skill.

iPhone programming kungfu

Level 1: Tool master

At this level, you must know:
+ Tools using in iPhone programming, the role and how to use them.
+ MAC OS environment: if you're user from Window, Unix, ... you will be surprise with differences on MAC OS and you must get acquainted with the environment here.
+ Hack to install MAC OS in PC: you should know this if you do not have money to buy a real MAC.
+ Master of languages using in iPhone programming: Objective C, C

Level 2: Native application master

At this level, you must have strong experience with building user interface for native application. Below is the list of thing required:
+ Have deep understanding about Model View Controller pattern and how to implement that using iPhone SDK.
+ Strong knowledge about common UIControl.
+ Experience with touch programming.
+ Must know user interface patterns in iPhone interface design.
+ Familiar with data storage: XML, SQLLite
+ Familiar with interact with calling remote services.
+ Familiar with UI animation and effects.

Level 3: Game programming master

Game is a common application type on iPhone. So, you should know how to develop a game on iPhone. You can use some popular game engines on iPhone to do that. Currently, we choose Cocos2D for the 2D game engine and Chipmunk as the physic engine behind. (Remember that: a game engine can not be completed if it does not have a physic engine)
I don't want to talk much about this level because I am not a master in this domain (game programming). However, below is the required list for level 3 (that I've discovered until now)
+ Can load/use sprite/bitmap into game engine
+ Create scene and navigate between scenes
+ Move and animate with 2D object
+ Using physic engine to create physic effect and animation.

Note: the above list is just for 2D game programming. You can find another list in 3D programming - but it is out of the scope because we do not have enough land to write about that here.

Level 4: Web programming master

At this level, you must know:
+ HTML tags that allows in iPhone safari browser.
+ Layout an HTML page to view best on iPhone
+ Using iUI library to build a web app liked native app.

Level 5: iPhone kungfu master

This is the highest level in iPhone programming. At this level, you can understand and program fluently with 4 above levels. Besides that, you must know:
+ Using advanced services and utilities in iPhone: geopositioning (GPS), maps, weather.
+ Can combine both Web + Native application into one.
+ Can design the system architecture for bigger application on iPhone with big storage data, synchronization solution, ...
+ Know how to optimize the program on iPhone and how to use tool for detect leak memory and high rate CPU cost in code.
Above is the list of 5 levels - kungfu in iPhone programming. Please feel free to post your comments or another level if you found.
Happy programming!