[iPhone] App에서 다른 Native App을 띄우는 방법 정리 – Launching Other Apps within an iPhone Application

No Comments

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

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

2. Launch Google Maps
아래와 같이 QUERY_STRING에 원하는 검색 string을 넣어서 URL을 launch해준다.
http://maps.google.com/maps?q=${QUERY_STRING}

// 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]];


3. Launch Apple Mail

mailto://에 email address를 넣어주면 된다.

mailto://${EMAIL_ADDRESS}

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://gidaeyeo@gmail.com]];

4. Dial a Phone Number

4-1. URL을 open하는 방식은 아래와 같이 tel: 뒷 부분에 번호를 쓰면 된다.

tel://${PHONE_NUMBER}

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

4-2. UIWebView를 이용하는 방법

아래와 같이 웹 페이지를 통해서 콜을 부를 수도 있다.

아래와 같은 내용을 가진 about.txt가 있다고 할 때,

1-800-466-4411

GOOG-411

applicationDidFinishLoading에서 WebView를 만들고 이것을 window에 add하는 코드는 아래와 같다.

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  // Define the frame (location/size) for the webview
  UIWebView *webView =
      [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 35)];
  webView.backgroundColor = [UIColor whiteColor];
  [webView setOpaque:NO];

  // Access the html file
  NSString *textpath = @"about.txt";
  NSString *filePath =
       [[NSBundle mainBundle] pathForResource:textpath ofType:nil];
  NSString *html = [NSString stringWithContentsOfFile:filePath];  

  // Define the html body info
  NSString *htmlOpen = @"";
  NSString *htmlClose = @""; 

  // The overall document structure
  NSString *bodyhtml =
      [NSString stringWithFormat:@"%@ %@ %@", htmlOpen, html, htmlClose];

  // Load the html in the webview
  [webView loadHTMLString:bodyhtml baseURL:nil];   

  // Add webview to current view
  [window addSubview:webView];

  // self.view retained the webview, we can release it
  [webView release];

  [window makeKeyAndVisible];
}

5. Launch the SMS Application

다음을 채워서 URL을 open 해준다.

sms:${PHONENUMBER_OR_SHORTCODE}

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

6. Launching App’s page in AppStore

[[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]

Ref 1. Launching the Browser from within an iPhone application

Ref 2. Launching Other Apps within an iPhone Application

Ref3. Dial a Phone Number – So Many Options