`
johnie_sheng
  • 浏览: 110498 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

在一个应用程序中调用其他的应用,比如说网站,App Store等等

 
阅读更多

In an earlier post I talked about how to launch the browser from within an iPhone application using the UIApplication:openURL: method.

It is also possible to use this same technique to launch other applications on the iPhone that are very useful.

Examples of some of the key applications that you can launch via URL are:

  • Launch Google Maps
  • Launch Apple Mail
  • Dial a Phone Number
  • Launch the SMS Application
  • Launch the Browser
  • Launch the AppStore

 

Launch Google Maps

The URL string for launching Google Maps with a particular keyword follows this structure:

http://maps.google.com/maps?q=${QUERY_STRING}

The only trick to this is to ensure that the value for the ${QUERY_STRING} is properly URL encoded. Here is a quick example of how you would launch Google Maps for a specific address:

1
2
3
4
5
6
7
8
9
10
11
// Create your query ...

NSString
*
 searchQuery =
 @
"1 Infinite Loop, Cupertino, CA 95014"
;
 
// Be careful to always URL encode things like spaces and other symbols that aren't URL friendly

searchQuery =
  [
addressText stringByAddingPercentEscapesUsingEncoding:
 NSUTF8StringEncoding]
;
 
// Now create the URL string ...

NSString
*
 urlString =
 [
NSString
 stringWithFormat:
@
"http://maps.google.com/maps?q=%@"
, searchQuery]
;
 
// An the final magic ... openURL!

[
[
UIApplication sharedApplication]
 openURL:
[
NSURL
 URLWithString:
urlText]
]
;
Launch Apple Mail

Also very useful, is the ability to enable a user to quickly send an email by launching the email client in compose mode and the address already filled out. The format of this URI should be familiar to anyone that has done any work with HTML and looks like this:

mailto://${EMAIL_ADDRESS}

For example, here we are opening the email application and filling the “to:” address with info@iphonedevelopertips.com :

[
[
UIApplication sharedApplication]
 openURL:
[
NSURL
 URLWithString:
@
"mailto://info@iphonedevelopertips.com"
]
]
;
Dial a Phone Number (iPhone Only)

You can use openURL: to dial a phone number. One advantage this has over other URLs that launch applications, is that the dialer will return control back to the application when the user hits the “End Call” button.

Anyone familiar with J2ME or WML will find this URL scheme familiar:

tel://${PHONE_NUMBER}

Here is an example of how we would dial the number (800) 867-5309:

1
[
[
UIApplication sharedApplication]
 openURL:
[
NSURL
 URLWithString:
@
"tel://8004664411"
]
]
;

NOTE When providing an international number you will need to include the country code.

Launch the SMS Application

Also not supported by the iPod Touch, is the ability to quickly setup the SMS client so that your users can quickly send a text message. It is also possible to provide the body of the text message.

The format looks like this:

sms:${PHONENUMBER_OR_SHORTCODE}

NOTE: Unlike other URLs, an SMS url doesn’t use the “//” syntax. If you add these it will assume it is part of the phone number which is not.

1
[
[
UIApplication sharedApplication]
 openURL:
[
NSURL
 URLWithString:
@
"sms:55555"
]
]
;

NOTE: According to the official SMS specification, you should be able to send a body as well as the phone number by including “?body=” parameter on the end of the URL … unfortunately Apple doesn’t seem to support this standard.

 

Lauching Browser

Here is a simple example of how to open safari with a specific URL:

1
2
NSURL
 *
url =
 [
NSURL
 URLWithString:
@
"http://www.iphonedevelopertips.com"
]
;
[
[
UIApplication sharedApplication]
 openURL:
url]
;
Launching the AppStore

Finally, it is worth noting that you can launch the AppStore and have the "buy" page of a specific application appear. To do this, there is no special URL scheme. All you need to do is open up iTunes to the application you want to launch; right-click on the application icon at the top left of the page; and select Copy iTunes Store URL .

The URL will look something like this:

http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8

Launching the AppStore URL is exactly the same as you would launch the browser. Using the link above, here is an example of how we would launch the AppStore:

1
2
NSURL
 *
appStoreUrl =
 [
NSURL
 URLWithString:
@
"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8"
]
;
[
[
UIApplication sharedApplication]
 openURL:
appStoreUrl]
;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics