10-IOSCore - 应用间通信、本地通知
一、应用间通信
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 陀螺仪
四、加速计 / 陀螺仪
10-IOSCore - 应用间通信、本地通知的更多相关文章
- 背水一战 Windows 10 (100) - 应用间通信: 分享
[源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...
- 背水一战 Windows 10 (102) - 应用间通信: 剪切板
[源码下载] 背水一战 Windows 10 (102) - 应用间通信: 剪切板 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 剪切板 - 基础, 复制/粘贴 text ...
- 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用
[源码下载] 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用 作者:weba ...
- Java 线程间通信 —— 等待 / 通知机制
本文部分摘自<Java 并发编程的艺术> volatile 和 synchronize 关键字 每个处于运行状态的线程,如果仅仅是孤立地运行,那么它产生的作用很小,如果多个线程能够相互配合 ...
- Java多线程基础——线程间通信
在使用多线程的时候,经常需要多个线程进行协作来完成一件事情.在前面两章分析了Java多线程的基本使用以及利用synchronized来实现多个线程同步调用方法或者执行代码块.但上面两章的内容涉及到的例 ...
- 《java多线程编程核心技术》不使用等待通知机制 实现线程间通信的 疑问分析
不使用等待通知机制 实现线程间通信的 疑问分析 2018年04月03日 17:15:08 ayf 阅读数:33 编辑 <java多线程编程核心技术>一书第三章开头,有如下案例: ...
- 系统间通信(10)——RPC的基本概念
1.概述 经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的讲解:RPC.在后续的几篇文章中,我们首先讲解R ...
- 10 - 应用程序间通信、本地通知、加速计、URL传输中文
一.应用间通信 URL 调用系统服务: tel:11111 sms:xxx@163.com http:// URL深入 类型://主机:端口/地址?参数 label框等于文字大小快捷键:command ...
- iOS 10 UserNotification框架解析 – 本地通知
iOS 10以前的通知比较杂乱,把本地通知和远程通知分开了,诞生了许多功能类似的API,很容易让初学者犯迷糊.而iOS 10的通知把API做了统一,利用独立的UserNotifications.fra ...
随机推荐
- JavaScript 高级程序设计(第3版)笔记——chapter7:函数表达式
一.函数表达式的语法形式 匿名函数 var functionName = function(arg0, arg1, arg2) { //函数体 } 二.函数表达式没有函数提升 var a = 1; i ...
- c#语言基础之组成结构
一.项目结构 .cs--- 源文件(程序代码) .csproj---项目文件(管理文件项) .sln--- 解决方案文件(管理项目) .config---配置文件 函数的四要素:名称.输入. ...
- linux命令:rsync, 同步文件和文件夹的命令
Usage: rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or ...
- PHP 判断数据类型
isset()://变量是否已经声明 empty()://变量是否为空 defined()://常量是否已经定义 define() array_key_exists(mixed key, array ...
- python subprocess重定向标准输出
subprocess.call("ping -c 1 %s" % ip,shell = True,stdout = open('/dev/null','w'),stderr = s ...
- spoj 3871 gcd extreme
题目大意给出一个n,求sum(gcd(i,j),<i<j<=n); 可以明显的看出来s[n]=s[n-]+f[n]; f[n]=sum(gcd(i,n),<i<n); 现 ...
- Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 治标又治本的解决方法: 找到在 { C:\Windows\Microsoft.NET\Fra ...
- elk工作原理
这个配置文件,是读取nginx日志写入到redis zjtest7-redis:/usr/local/logstash-2.3.4/config# cat logstash_agent.conf in ...
- HDU 2087 剪花布条 KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2087 KMP匹配数—— AC代码: #include <iostream> #includ ...
- 深入浅出 JSTL
JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.这个算是一个java程序员的一个基本功吧. JSP Standard Tag Library (JSTL) 的规范完成于2002年7月, ...