2010년 12월 14일 화요일

아이폰 내부 API 관련


iPhone developer:tips에 다음과 같은 Native App을 띄우는 방법에 대해서 잘 정리해서 포스팅 해본다.
  • Launch the Browser
  • Launch Google Maps
  • Launch Apple Mail
  • Dial a Phone Number
  • Launch the SMS Application
  • Launch the AppStore
아래는 URL을 open함으로써 다른 App을 띄우는데, iPod에서는 Call과 SMS 관련 App을 띄울 수 없을 것이다. 이 경우 openURL 함수의 return 값을 체크 (못 띄우는 경우 NO 반환) 할 수 있을 것이다.
1. Launch the Browser
1
NSURL *url = [NSURL URLWithString:@"http://www.iphonedevelopertips.com"];
2
[[UIApplication sharedApplication] openURL:url];
2. Launch Google Maps
아래와 같이 QUERY_STRING에 원하는 검색 string을 넣어서 URL을 launch해준다.
http://maps.google.com/maps?q=${QUERY_STRING}
01
// Create your query ...
02
NSString* searchQuery = @"1 Infinite Loop, Cupertino, CA 95014";
03
04
// Be careful to always URL encode things like spaces and other symbols that aren't URL friendly
05
searchQuery =  [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
06
07
// Now create the URL string ...
08
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", searchQuery];
09
10
// An the final magic ... openURL!
11
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
3. Launch Apple Mail
mailto://에 email address를 넣어주면 된다.
mailto://${EMAIL_ADDRESS}
1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://gidaeyeo@gmail.com]];
4. Dial a Phone Number
4-1. URL을 open하는 방식은 아래와 같이 tel: 뒷 부분에 번호를 쓰면 된다.
tel://${PHONE_NUMBER}
1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
4-2. UIWebView를 이용하는 방법
아래와 같이 웹 페이지를 통해서 콜을 부를 수도 있다.
아래와 같은 내용을 가진 about.txt가 있다고 할 때,
1
1-800-466-4411
2
3
<a href="tel://8004664411">GOOG-411</a>
applicationDidFinishLoading에서 WebView를 만들고 이것을 window에 add하는 코드는 아래와 같다.
01
- (void)applicationDidFinishLaunching:(UIApplication *)application
02
{
03
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
04
05
  // Define the frame (location/size) for the webview
06
  UIWebView *webView =
07
      [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 35)];
08
  webView.backgroundColor = [UIColor whiteColor];
09
  [webView setOpaque:NO];
10
11
  // Access the html file
12
  NSString *textpath = @"about.txt";
13
  NSString *filePath =
14
       [[NSBundle mainBundle] pathForResource:textpath ofType:nil];
15
  NSString *html = [NSString stringWithContentsOfFile:filePath]; 
16
17
  // Define the html body info
18
  NSString *htmlOpen = @"";
19
  NSString *htmlClose = @"";
20
21
  // The overall document structure
22
  NSString *bodyhtml =
23
      [NSString stringWithFormat:@"%@ %@ %@", htmlOpen, html, htmlClose];
24
25
  // Load the html in the webview
26
  [webView loadHTMLString:bodyhtml baseURL:nil];  
27
28
  // Add webview to current view
29
  [window addSubview:webView];
30
31
  // self.view retained the webview, we can release it
32
  [webView release];
33
34
  [window makeKeyAndVisible];
35
}
5. Launch the SMS Application
다음을 채워서 URL을 open 해준다.
sms:${PHONENUMBER_OR_SHORTCODE}
1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:55555"]];
6. Launching App’s page in AppStore
1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302820334&mt=8"]];
위 코드에서 띄우고 싶은 App을 App Store에서 찾아서 app 의 url을 복사 후 id부분을 써주면 된다.
(iTunes App Sotre의 App URL (http://itunes.XXX 로 시작하는)을 그대로 띄우면 안된다.)
[Refs]
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.

댓글 없음:

댓글 쓰기