10 - 应用程序间通信、本地通知、加速计、URL传输中文
一、应用间通信
URL
调用系统服务:
tel:11111
sms:xxx@163.com
http://
URL深入
类型://主机:端口/地址?参数
label框等于文字大小快捷键:command =
两个应用程序间通信步骤:
A -> B
1.先制定一个通信的url类型iac
在B程序中选中项目名称->选TARGETS下的那个项目名称->Info->URL Types->制定URL Schemes和identifier
2.在程序B中声明支持url类型
3.程序A中openURL:
最好在open之前用canOpenURL检测是否能够打开应用程序
4.在程序B中响应
【Day1001_IAC_Target】
【Day1002_IAC_Source】
两个应用程序间的通信
target是被source打开的应用程序,不过要先把target烧在模拟器上,并设置url类型iac及标示符identifier
在source里openURL之前先使用canOpenURL判断是否有url类型iac的应用程序,然后openURL即可跳到target应用程序并会把iac:xxx传过去
(在target应用程序的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
这个方法的launchOptions会接收到传过来的iac:xxx和identifier)
验证:在target的沙箱目录里创建一个log.txt文件把信息追加进去,即可查看,注意这个log文件最好放在沙箱目录里,防止被删除。
在target应用程序里有一个API是专门在其他应用程序跳过来的时候才会调用,这个API是
// 如果是别的程序openURL 才会调用此消息
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
在该消息里直接接收数据。
source发送数据:
- (IBAction)tap:(id)sender {
// 判断是否有支持url类型iac:的应用程序
BOOL canOpen = [[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@"iac:"]];
if (canOpen) {
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"iac://function/addcontact?name=sansang&age=98"]];
}
}
target接收数据:
// 如果是别的程序openURL 才会调用此消息
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if ([[url scheme] isEqualToString:@"iac"]) {
NSString *homePath = NSHomeDirectory();
// NSLog(@"%@",homePath);
NSString *logFilePath = [homePath stringByAppendingPathComponent:@"log.txt"];
NSMutableString *log = [@""mutableCopy];
// 各种修改log
[log appendFormat:@"absoluteString:%@",[url absoluteString]]; // 绝对路径
[log appendString:@"\n"];
[log appendFormat:@"scheme:%@",[url scheme]]; // url类型本例是iac
[log appendString:@"\n"];
[log appendFormat:@"host:%@",[url host]]; // 主机
[log appendString:@"\n"];
[log appendFormat:@"query:%@",[url query]]; // 参数
[log appendString:@"\n"];
// 解析传过来的数据 a=b&c=d
NSString *queryString = [url query];
NSArray *paras = [queryString componentsSeparatedByString:@"&"];//paras参数
NSMutableDictionary *parasDic = [NSMutableDictionarydictionary];
// NSMutableDictionary *parasDic = [@{} mutableCopy];
for (NSString *para in paras) {
NSArray *paraItems = [para componentsSeparatedByString:@"="];
NSString *key = paraItems[0];
NSString *value = paraItems[1];
[parasDic setObject:value forKey:key];
}
[parasDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[log appendFormat:@"%@:%@",key,obj];
[log appendString:@"\n"];
}];
self.name = parasDic[@"name"];
self.age = [parasDic[@"age"] integerValue];
// 写入文件
[log writeToFile:logFilePath atomically:YESencoding:NSUTF8StringEncodingerror:nil];
returnYES;
}
}
----------------------------------------------
target接收到数据显示在label上
MXAppDelegate.h定义单例属性
@property (nonatomic, copy) NSString *name;
@property (nonatomic) NSInteger age;
MXAppDelegate.m赋值
接收数据消息openURL中赋值
self.name = parasDic[@"name"];
self.age = [parasDic[@"age"] integerValue];
MXViewController.m
在要显示的view中显示
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
MXAppDelegate *appDelegate = [[UIApplicationsharedApplication] delegate];
// 添加观察
[appDelegate addObserver:selfforKeyPath:@"name"options:NSKeyValueObservingOptionNewcontext:NULL];
[appDelegate addObserver:selfforKeyPath:@"age"options:NSKeyValueObservingOptionNewcontext:NULL];
}
// 属性值变化响应
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
MXAppDelegate *appDelegate = [[UIApplicationsharedApplication] delegate];
if ([keyPath isEqualToString:@"name"]) {
self.nameLabel.text = appDelegate.name;
}elseif ([keyPath isEqualToString:@"age"]) {
self.ageLabel.text = [NSStringstringWithFormat:@"%d",appDelegate.age];
}
}
// 撤销观察
-(void)viewDidAppear:(BOOL)animated{
MXAppDelegate *appDelegate = [[UIApplicationsharedApplication] delegate];
[appDelegate removeObserver:selfforKeyPath:@"name"];
[appDelegate removeObserver:selfforKeyPath:@"age"];
}
-----------------------------------------
4 通信传输参数
发邮箱(需要配置好邮箱才能发)
搜索tel:-> phone links -> mail links
mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!
5 启动系统mail程序
cc是抄送
subject主题
%20 是空格
body 是内容
参数没有顺序!
代码:
- (IBAction)mail:(id)sender {
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]];
}
----------------------------------------------
二、通知中心
1.类型:
1) Local Notification本地通知
是本地应用程序经过测算,预先设定好,在未来的某个时间点提示用户弹出通知
( 2) Apple Push Notification Service(APNs)苹果的通知推送服务
服务器不能跟手机直接沟通,你的服务器和目标手机的本地程序直接沟通
你的服务器->苹果的APNs的服务器->目标手机)
2本地通知三步
1> 构建本地通知对象
2> 安排发送时间
3> 将通知提交给系统保存
代码:
- (IBAction)tap:(id)sender {
// 1> 构建本地通知对象
UILocalNotification *localNotification = [[UILocalNotificationalloc] init];
// 2> 安排发送时间
localNotification.fireDate = [NSDatedateWithTimeIntervalSinceNow:5];
// 内容
localNotification.alertBody = @"你居然把我关了"; // 推送内容
localNotification.alertAction = @"测试"; // 显示在按钮上
localNotification.applicationIconBadgeNumber = 1; // 显示在应用程序上
// 3> 将通知提交给系统保存
[[UIApplicationsharedApplication] scheduleLocalNotification:localNotification];
}
3 收通知
两种情况:
1> 当前的程序在运行,在后台
MXAppDelegate.m
// 本地通知被点击后 程序没有被杀掉 其次用户点击通知时才会调用
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"didReceiveLocalNotification");
application.applicationIconBadgeNumber = 0; // 清除掉应用程序右上角数字
[application cancelAllLocalNotifications]; // 清除掉所有的通知
}
2> 当前的程序不在运行,被杀掉了
此时点击通知会激活当前程序,并调用下面的消息
// 如果程序不在内存中用户点击通知时会调用此消息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
returnYES;
}
4 本地通知传输数据和接收数据
传输数据:
localNotification.userInfo = @{@"name":@"sansang",@"age":@99}; // 几秒之后给自己传数据
接收数据:
// 接收到(程序没有被杀掉)通知数据
NSDictionary *userInfo = notification.userInfo;
// 接收到(程序被杀掉)通知数据
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification != Nil) {
//...
}
/////////////////////
[launchOptions enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[log appendFormat:@"%@:%@",key,obj];
[log appendString:@"\n"];
}];
-------------------------------------------------
三、复习
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
调用该消息有3中情况:
1.手指头点开(运行应用程序时)
2.通过其他程序openURL打开时
laumchPotions里有消息
有一个url
还有一个源程序名
系统调用此方法时,会在调用
// 如果是别的程序openURL 才会调用此消息
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
3.通过通知进入(当前程序被杀掉)
laumchPotions里有消息
有一个UILocalNotification对象
-------------------------------------------
accelerotemer加速计
Gyro 陀螺仪
四、加速计 / 陀螺仪
Core Motion框架
加速计
图位置:
搜索Core Motion -> Handing Accelerometer Events Using Core Motion

【day1004_CoreMotion】
导入CoreMotion.framework框架
关掉左屏右屏:项目名称->General->Deployment Info->Device Orientation-> 勾掉Landscape Left, Landscape Right
作业
1. TContact项目升级
1) 拨打电话:
查看联系人界面
点击联系人的电话 立刻拨打出去
2) 共享联系人:
查看联系人界面下面增加一个按钮
按钮“共享联系人”
点击共享联系人后
TContact程序跳转到TMessage程序
TMessage的输入框内有该联系人的基本信息,
正在准备被发送
“小明的电话是18612345678”
TContact程序跳转到TMessage程序
首先在TMessage的Info->URL Types中设置URL Schemes和Identifier
设置Identifier一般格式为:
公司网址名+ios+项目名称+scheme(跳转标识名)
比如:come.tarena.ios.TMessage.iac
解决传输中文问题
把url转为utf8编码
NSString *urlString = [paras stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
解码:
self.name = [name stringByRemovingPercentEncoding];
10 - 应用程序间通信、本地通知、加速计、URL传输中文的更多相关文章
- iOS 10 UserNotification框架解析 – 本地通知
iOS 10以前的通知比较杂乱,把本地通知和远程通知分开了,诞生了许多功能类似的API,很容易让初学者犯迷糊.而iOS 10的通知把API做了统一,利用独立的UserNotifications.fra ...
- 10-IOSCore - 应用间通信、本地通知
一.应用间通信 URL 调用系统服务: tel:11111 sms:xxx@163.com http:// URL深入 类型://主机:端口/地址?参数 label框等于文字大小快捷键:command ...
- IOS 本地通知推送消息
在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
- IOS本地通知
发送通知: UILocalNotification *newNotification = [[UILocalNotification alloc] init]; if (newNotifica ...
- IOS本地通知:UILocalNotification使用记录
第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...
- ios开发——实用技术OC-Swift篇&本地通知与远程通知详解
本地通知与远程通知详解 一:本地通知 Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...
- UILocalNotification本地通知
// 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...
- IOS 本地通知
操作流程 1.接收通知 2.注册发送通知 用途:提示时间,闹钟 //接收本地通知(在Appdelegate里面实现) - (void)application:(UIApplication *)appl ...
- IOS 本地通知 UILocalNotification
IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...
随机推荐
- VCS引起的oracle数据库异常重新启动一例
1. 环境描写叙述 操作系统版本号:SUSE Linux Enterprise Server 10 sp2 (x86_64) 数据库版本号:Oracle 11.1.0.7.16 VCS版本号:5.1 ...
- JavaSript模块化 && AMD CMD 详解.....
模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题进行系统性的分解以之处理.模块化是一种处理复杂系统分解为代码结构更合理,可维护性更高的可管理的模块的方式.可以想象一个巨大 ...
- [置顶] 浅谈大型web系统架构
转载原文:http://blog.csdn.net/dinglang_2009/article/details/6863697 分类: 大规模Web 2.0架构 2011-10-11 18:27 12 ...
- CodeFirst-Section1之小例子
尽可能做到不说一些晦涩难懂的语言,Follow Me...... 环境:Visual Studio 2013+.Net Framework 4.5 1.什么是Code First? 说白了就是先建好C ...
- scanf 与 cin 的区别
在论坛上看到有人提出一个如下的问题,在此总结一下. 原问题: http://topic.csdn.net/u/20110414/22/90d0606c-9876-48e4-9b69-bd8bd8a41 ...
- 杭电ACM 素数判定
素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Collection类学习笔记
binarySearch原理: public static index halfSearch(List<String> list, String key) { int max,min,mi ...
- 织梦dedecms|图片模型内容页标签
图片列表开始:{dede:productimagelist}图片列表结束:{/dede:productimagelist}图片显示: [field:imgsrc/]图集缩略图: {dede: ...
- php定时自动执行 需启动第一次
1 2 3 4 5 6 7 8 9 10 11 12 <? ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_li ...
- javascript 检测密码强度 美化版
模仿美团的美化 <!DOCTYPE> <head runat="server"> <title></title> <link ...