二 、打开地图《苹果iOS实例编程入门教程》

时间:2023-03-08 20:04:07
二 、打开地图《苹果iOS实例编程入门教程》

该app为应用的功能为给你的iPhone打开google地图有效地址连接

现版本 SDK 8.4 Xcode

运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 OpenURL

(1)  在xCode打开 ViewController.h 文件

(红色为所添加的代码)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)openMaps:(id)sender;

@end

(2)  在xCode打开 ViewController.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)openMaps:(id)sender {

NSString *addressText = @"1 Queen st, Auckland, NZ";

addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSString *urlText = [NSString stringWithFormat:@"http://www.google.cn/maps/place/%@",addressText];

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlText]];

/*

  openURL的使用方法:

  其中系统的appString有:

  Map http://maps.google.com/maps?q=Shanghai

  Email mailto://myname@google.comTel

  tel://10086Msg

  sms://10086

  除此之外,还可以自己定义URL,方法如下:

  打开info.plist,添加一项URL types 展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme 展开URL Scheme,将Item1的内容修改为myapp 其他程序可通过myapp://访问此自定义URL

  openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序(给被启动的应用程序自定义URL)

*/

}

@end

(3) UIView 界面设置
- 黑色背景

点击Main.storyboard

选择view controller ->view

将背景设为黑色

选择Attributes Inspector->Background->Black Color

(4) 加入 Button ,点击后打开连接

选择: Tools -> Library ;从Library显示菜单中拖拉一个 Button 到 Main View

在主显示上点击 Button;从Button Attributes 里面Title内填上 "Open Google Map"。

(5)写入 UIButton 的 class file

鼠标右击Button控件,鼠标移动到"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "openMaps:"; 选上它。

选择: File -> Save

最后在 xCode 选择 Build and then Running

(6) 模拟器调试效果图

二 、打开地图《苹果iOS实例编程入门教程》   二 、打开地图《苹果iOS实例编程入门教程》

本文源于网上博客教程,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100dmln.html