IOS高级开发~开机启动&无限后台运行&监听进程
一般来说, IOS很少给App后台运行的权限. 仅有的方式就是 VoIP. IOS少有的为VoIP应用提供了后台socket连接,定期唤醒并且随开机启动的权限.而这些就是IOS上实现VoIP App的关键. 苹果官方文档对于的描述就短短的一页(点击这里),很多细节没有提及. 这篇微博通过具体实现和查阅资料,补充了这些细节.并且列举出了在实现过程中可能遇到的问题, 作为参考.
- 博客: http://www.cnblogs.com/jhzhu
- 邮箱: jhzhuustc@gmail.com
- 作者: 知明所以
- 时间: 2013-11-10
官方文档描述如是:
PS:此节纯用来占座.如果你你E文不好或者想直接切入正题, 请看下一标题.
There are several requirements for implementing a VoIP app:
- Add the UIBackgroundModes key to your app’s Info.plist file. Set the value of this key to an array that includes the voip string.
- Configure one of the app’s sockets for VoIP usage.
- Before moving to the background, call the setKeepAliveTimeout:handler: method to install a handler to be executed periodically. Your app can use this handler to maintain its service connection.
- Configure your audio session to handle transitions to and from active use.
- To ensure a better user experience on iPhone, use the Core Telephony framework to adjust your behavior in relation to cell-based phone calls; see Core Telephony Framework Reference.
- To ensure good performance for your VoIP app, use the System Configuration framework to detect network changes and allow your app to sleep as much as possible.
我的翻译:
关于IOS为VoIP应用提供的特殊权限和实现方法,我的描述如下. 我尽可能的涉及到voip实现的各种细节, 这样你能对这个运作机制有一个更好的理解,我觉得这远比单单贴几行代码有意义. 因为一个开发者在实际实现过程中遇到的千难险阻很少会体现在最终代码上, 就如你永远不知道台上的角儿在台下的挫折.
- IOS允许App的一个Socket在App切换到后台后仍然保持连接. 这样,当有通话请求的时候,App能及时处理. 这个
socket
需要在应用第一次启动的时候创建, 并标记为"此socket
用于VoIP服务". 这样当App切换到后台的时候,IOS会接管这个标记为"用于VoIP服务"的socket
. 这个socket
的响应函数(比如,一个delegate
,或者是个block
)会正常的响应, 就像App还在前台一样. - 10s魔咒. 当
socket
有任何数据从服务端传来, 你在app里为socket
写的响应函数都会做处理.但是, 你只有最多10s的时间来干你想干的事情. 也就意味着你在响应函数里新建一个大于10s的timer
是没有意义的. 并且IOS并不保证给你足够10s的时间,视系统情况而定. - 在
socket
的响应函数里, 你能通过NSLocalNotification
来通知用户"电话来了". 除此之外, 你没法做其他任何视觉上的动作来提醒用户, 因为你的app还处于某个不知道的次元, 甚至连window
都还没创建. - 你永远也没有办法知道或者决定
NSLocalNotification
的样式是banner
还是alert
. 你也许钟爱后者, 但是决定权在用户手里. - 允许在后台定期执行一段代码. 你可以设定一个大于等于10分钟的时间
t
, 和一个定期执行的handler
, IOS系统会在每次经过t
时间的时候调用一次这个handler
. 但是IOS不保证这个handler
会准时运行, 只保证在时间t
范围内的某个点会执行一次. - 我们通常用楼上的
handler
处理socket的断线重连操作. 因为网络不稳定, 或者用户开启飞行模式等原因, 我们用于voip服务的socket
会断开连接. 在这个handler
里,如果发现连接断开,我们只需要跟条目1一样的创建socket
,设置一样的socket
响应函数,一切又会恢复正常. - 不建议这个
handler
做太多事情, 因为它也有10s魔咒.(据不完全统计,苹果所有的后台处理都有这个10s限制. 不保证绝对正确哈, 仅供参考) - 自启服务. 当IOS重新启动, 或者你的app因为其他原因退出时, IOS会马上启动你注册为voip的app, 你可以很迅速的恢复
socket
连接. 但是, 从底部多任务栏中手动关闭应用除外.更"code"的说明是:当程序退出的exitcode != 0
,IOS会重启你的app.经试验发现,从底部多任务栏关闭的时候,程序的exitcode == 0
. - 如果你亲爱的用户是一个典型的"app终结者",那么你还剩最后一条路来通知来电提醒:
NSRemoteNotification
. 你也许会被NSRemoteNotification
的可靠性和实时性折腾的抓狂, 但是, 谁让你选了IOS? 你享受了封闭带来的传世体验, 也得承受封闭的限制. - 当条目8描述的情况发生之后,app的
application:didFinishLaunchingWithOptions:
会被调用. 但是,请时刻提醒自己我们的app仍然处于后台. 我们以前总在这里创建window
创建rootController
, 但现在不必了. 现在我们需要的就是把可爱的socket
连上, 像在条目1里一样,让一切回归正常(我不去写歌词真浪费了^_^). - 在
application:didFinishLaunchingWithOptions:
里你能通过[application applicationState] == UIApplicationStateBackground
来判断是正常启动应用还是系统自动启动, 然后决定该创建window
还是创建voip的socket
.
非越狱情况下实现:
开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动;
无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill;
监听进程:可获IOS设备运行除系统外的App(包括正在运行和后台运行);
配置项目 plist文件
添加:
<key>UIBackgroundModes</key>
<array>
<string>voip</string>
</array>
功能类:ProccessHelper
- #import <Foundation/Foundation.h>
- @interface ProccessHelper : NSObject
- + (NSArray *)runningProcesses;
- @end
- [cpp] view plaincopyprint?
- #import "ProccessHelper.h"
- //#include<objc/runtime.h>
- #include <sys/sysctl.h>
- #include <stdbool.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/sysctl.h>
- @implementation ProccessHelper
- //You can determine if your app is being run under the debugger with the following code from
- static bool AmIBeingDebugged(void)
- // Returns true if the current process is being debugged (either
- // running under the debugger or has a debugger attached post facto).
- {
- int junk;
- int mib[4];
- struct kinfo_proc info;
- size_t size;
- // Initialize the flags so that, if sysctl fails for some bizarre
- // reason, we get a predictable result.
- info.kp_proc.p_flag = 0;
- // Initialize mib, which tells sysctl the info we want, in this case
- // we're looking for information about a specific process ID.
- mib[0] = CTL_KERN;
- mib[1] = KERN_PROC;
- mib[2] = KERN_PROC_PID;
- mib[3] = getpid();
- // Call sysctl.
- size = sizeof(info);
- junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
- assert(junk == 0);
- // We're being debugged if the P_TRACED flag is set.
- return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
- }
- //返回所有正在运行的进程的 id,name,占用cpu,运行时间
- //使用函数int sysctl(int *, u_int, void *, size_t *, void *, size_t)
- + (NSArray *)runningProcesses
- {
- //指定名字参数,按照顺序第一个元素指定本请求定向到内核的哪个子系统,第二个及其后元素依次细化指定该系统的某个部分。
- //CTL_KERN,KERN_PROC,KERN_PROC_ALL 正在运行的所有进程
- int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0};
- size_t miblen = 4;
- //值-结果参数:函数被调用时,size指向的值指定该缓冲区的大小;函数返回时,该值给出内核存放在该缓冲区中的数据量
- //如果这个缓冲不够大,函数就返回ENOMEM错误
- size_t size;
- //返回0,成功;返回-1,失败
- int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
- struct kinfo_proc * process = NULL;
- struct kinfo_proc * newprocess = NULL;
- do
- {
- size += size / 10;
- newprocess = realloc(process, size);
- if (!newprocess)
- {
- if (process)
- {
- free(process);
- process = NULL;
- }
- return nil;
- }
- process = newprocess;
- st = sysctl(mib, miblen, process, &size, NULL, 0);
- } while (st == -1 && errno == ENOMEM);
- if (st == 0)
- {
- if (size % sizeof(struct kinfo_proc) == 0)
- {
- int nprocess = size / sizeof(struct kinfo_proc);
- if (nprocess)
- {
- NSMutableArray * array = [[NSMutableArray alloc] init];
- for (int i = nprocess - 1; i >= 0; i--)
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
- NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
- NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu];
- double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;
- NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t];
- NSString *startTime = [[NSString alloc] initWithFormat:@"%ld", process[i].kp_proc.p_un.__p_starttime.tv_sec];
- NSString * status = [[NSString alloc] initWithFormat:@"%d",process[i].kp_proc.p_flag];
- NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
- [dic setValue:processID forKey:@"ProcessID"];
- [dic setValue:processName forKey:@"ProcessName"];
- [dic setValue:proc_CPU forKey:@"ProcessCPU"];
- [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];
- [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];
- [dic setValue:startTime forKey:@"startTime"];
- // 18432 is the currently running application
- // 16384 is background
- [dic setValue:status forKey:@"status"];
- [processID release];
- [processName release];
- [proc_CPU release];
- [proc_useTiem release];
- [array addObject:dic];
- [startTime release];
- [status release];
- [dic release];
- [pool release];
- }
- free(process);
- process = NULL;
- //NSLog(@"array = %@",array);
- return array;
- }
- }
- }
- return nil;
- }
- @end
实现代码:
- systemprocessArray = [[NSMutableArray arrayWithObjects:
- @"kernel_task",
- @"launchd",
- @"UserEventAgent",
- @"wifid",
- @"syslogd",
- @"powerd",
- @"lockdownd",
- @"mediaserverd",
- @"mediaremoted",
- @"mDNSResponder",
- @"locationd",
- @"imagent",
- @"iapd",
- @"fseventsd",
- @"fairplayd.N81",
- @"configd",
- @"apsd",
- @"aggregated",
- @"SpringBoard",
- @"CommCenterClassi",
- @"BTServer",
- @"notifyd",
- @"MobilePhone",
- @"ptpd",
- @"afcd",
- @"notification_pro",
- @"notification_pro",
- @"syslog_relay",
- @"notification_pro",
- @"springboardservi",
- @"atc",
- @"sandboxd",
- @"networkd",
- @"lsd",
- @"securityd",
- @"lockbot",
- @"installd",
- @"debugserver",
- @"amfid",
- @"AppleIDAuthAgent",
- @"BootLaunch",
- @"MobileMail",
- @"BlueTool",
- nil nil] retain];
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- while (1) {
- sleep(5);
- [self postMsg];
- }
- [cpp] view plaincopyprint?
- [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
- NSLog(@"KeepAlive");
- }];
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- }
- #pragma mark -
- #pragma mark - User Method
- - (void) postMsg
- {
- //上传到服务器
- NSURL *url = [self getURL];
- NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
- NSError *error = nil;
- NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
- if (error) {
- NSLog(@"error:%@", [error localizedDescription]);
- }
- NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
- NSLog(@"%@",str);
- }
- - (NSURL *) getURL
- {
- UIDevice *device = [UIDevice currentDevice];
- NSString* uuid = @"TESTUUID";
- NSString* manufacturer = @"apple";
- NSString* model = [device model];
- NSString* mobile = [device systemVersion];
- NSString *msg = [NSString stringWithFormat:@"Msg:%@ Time:%@", [self processMsg], [self getTime]];
- CFShow(msg);
- / 省略部分代码 /
- NSString *urlStr = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *url = [NSURL URLWithString:urlStr];
- return url;
- }
- - (BOOL) checkSystemProccess:(NSString *) proName
- {
- if ([systemprocessArray containsObject:proName]) {
- return YES;
- }
- return NO;
- }
- - (BOOL) checkFirst:(NSString *) string
- {
- NSString *str = [string substringToIndex:1];
- NSRange r = [@"ABCDEFGHIJKLMNOPQRSTUVWXWZ" rangeOfString:str];
- if (r.length > 0) {
- return YES;
- }
- return NO;
- }
- - (NSString *) processMsg
- {
- NSArray *proMsg = [ProccessHelper runningProcesses];
- if (proMsg == nil) {
- return nil;
- }
- NSMutableArray *proState = [NSMutableArray array];
- for (NSDictionary *dic in proMsg) {
- NSString *proName = [dic objectForKey:@"ProcessName"];
- if (![self checkSystemProccess:proName] && [self checkFirst:proName]) {
- NSString *proID = [dic objectForKey:@"ProcessID"];
- NSString *proStartTime = [dic objectForKey:@"startTime"];
- if ([[dic objectForKey:@"status"] isEqualToString:@"18432"]) {
- NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:YES", proName, proID, proStartTime];
- [proState addObject:msg];
- } else {
- NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:NO", proName, proID, proStartTime];
- [proState addObject:msg];
- }
- }
- }
- NSString *msg = [proState componentsJoinedByString:@"______"];
- return msg;
- }
- // 获取时间
- - (NSString *) getTime
- {
- NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
- formatter.dateStyle = NSDateFormatterMediumStyle;
- formatter.timeStyle = NSDateFormatterMediumStyle;
- formatter.locale = [NSLocale currentLocale];
- NSDate *date = [NSDate date];
- [formatter setTimeStyle:NSDateFormatterMediumStyle];
- NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
- NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
- NSInteger unitFlags = NSYearCalendarUnit |
- NSMonthCalendarUnit |
- NSDayCalendarUnit |
- NSWeekdayCalendarUnit |
- NSHourCalendarUnit |
- NSMinuteCalendarUnit |
- NSSecondCalendarUnit;
- comps = [calendar components:unitFlags fromDate:date];
- int year = [comps year];
- int month = [comps month];
- int day = [comps day];
- int hour = [comps hour];
- int min = [comps minute];
- int sec = [comps second];
- NSString *time = [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d", year, month, day, hour, min, sec];
- return time;
- }
- @end