的iOS相当于Android的服务? [英] iOS equivalent to Android Service?

查看:185
本文介绍了的iOS相当于Android的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,与iOS 7支持更广泛的背景模式,是有可能终于有一个相当于Android的服务在iOS上?

So with iOS 7 supporting a broader background mode, is it possible to finally have an equivalent to Android Service on iOS?

我是什么后,基本上是运行一个应用程序在后台有一个或多个应用程序B和C聊到该应用程序(不显示应用A的GUI)。

What I am after is essentially running app A in the background and have one or more apps B and C talk to that app (without showing the GUI of app A).

请注意,当使用连接和推送通知可能不是一种选择,虽然这是这样做的推荐的方式。 有任何想法吗?

Please note that using connectivity and push notifications may not be an option although this is the recommended way of doing so. Any ideas?

推荐答案

编辑:工作不正常。看到这个答案的最好的解决办法:推送通知

Not working as expected. See this answer for best solution: Push Notifications

编辑:接下来的解决方案,同时用户在应用程序保持同步它是唯一有用

The next solution is only useful while the user is in the app to maintain it synced.

有没有办法在后台执行任务永久,但可以使用有限长度的任务要做到这一点,当你犯了一个有限的长度,这会一直运行,而应用程序是活跃的,但是当你点击home键,IOS给你只有10分钟来执行任务,并失去它,但它给你一个机会,使一个无效处理程序块在这里你可以肯定完成之前做最后的行动。

There is no way to perform tasks in the background permanently, but you can use the finite-length tasks to do that, when you make a finite-length, this gonna run always while the app is active, but when you click home button, ios gives you only 10 min to perform your task and invalidate it, but it gives you a chance to make a 'invalidate handler block' where you can do last actions before finish definitely.

因此,如果您使用的处理器块调用有限长度的任务其他的时间,你可以模拟一个服务,通过运行一个任务10分钟,当它结束时,调用它的相同的其他10分钟,从而

我用在项目创建界面服务。我让你在这里的code:

I use that in a project creating a interface 'Service'. I let you here the code:

  • Service.h
//
//  Service.h
//  Staff5Personal
//
//  Created by Mansour Boutarbouch Mhaimeur on 30/09/13.
//  Copyright (c) 2013 Smart & Artificial Technologies. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Service : NSObject

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;
@property (nonatomic) NSInteger frequency;
@property (nonatomic, strong) NSTimer *updateTimer;

- (id) initWithFrequency: (NSInteger) seconds;
- (void) startService;
- (void) doInBackground;
- (void) stopService;
@end


  • Service.m

    • Service.m
    • //
      //  Service.m
      //  Staff5Personal
      //
      //  Created by Mansour Boutarbouch Mhaimeur on 30/09/13.
      //  Copyright (c) 2013 Smart & Artificial Technologies. All rights reserved.
      //
      
      #import "Service.h"
      
      @implementation Service
      @synthesize frequency;
      
      -(id)initWithFrequency: (NSInteger) seconds{
          if(self = [super init]){        
              self.frequency = seconds;
              return self;
          }
          return nil;
      }
      - (void)startService{
          [self startBackgroundTask];
      }
      
      - (void)doInBackground{
          //Español //Sobreescribir este metodo para hacer lo que quieras
          //English //Override this method to do whatever you want
      }
      
      - (void)stopService{
          [self.updateTimer invalidate];
          self.updateTimer = nil;
          [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
          self.backgroundTask = UIBackgroundTaskInvalid;
      }
      
      - (void) startBackgroundTask{
          self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:frequency
                                                              target:self
                                                            selector:@selector(doInBackground)
                                                            userInfo:nil
                                                             repeats:YES];
          self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
              [self endBackgroundTask];
          }];
      }
      - (void) endBackgroundTask{
          [self.updateTimer invalidate];
          self.updateTimer = nil;
          [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
          self.backgroundTask = UIBackgroundTaskInvalid;
          [self startBackgroundTask];
      }
      
      @end
      


      使用这个类我执行我的服务,但我不测试它的很长一段时间。 最好的测试我确实持续了16个小时的模拟机,一切工作正常!

      编辑:这是在模拟器上进行测试,但在手机不工作的应用程序已经终止后,

      That was tested on the simulator, but in phone doesnt work after the application has been terminated.

      我让你一个例子:

      // SomeService.h
      @interface SomeService : Service
      
      @end
      
      
      // SomeService.m
      #import "SomeService.h"
      
      @implementation SomeService
      
      // The method to override
      - (void)doInBackground{
          NSLog(@"Background time remaining = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);
          NSLog(@"Service running at %.1f seconds", [self getCurrentNetworkTime]);
      }
      // Your methods
      - (long) getCurrentNetworkTime{
          return ([[NSDate date] timeIntervalSince1970]);
      }
      
      @end
      


      而在你的应用程序代理或您需要提高服务,你写的下一行:


      And in your app delegate or where you need to raise the service, you write the next line:

      Service myService = [[SomeService alloc] initWithFrequency: 60]; //execute doInBackground each 60 seconds
      [myService startService];
      

      如果你需要停止它:

      And if you need to stop it:

      [myService stopService];
      

      也许已经解释了超过必要的,但我想保持清楚的人! 我希望它的帮助,对不起我的英语水平。

      May have explained more than necessary, but i want to keep it clear for anyone! I hope its help and sorry for my english.

      这篇关于的iOS相当于Android的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆