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

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

问题描述

那么在 iOS 7 支持更广泛的后台模式的情况下,是否有可能最终在 iOS 上拥有一个等同于 Android 的服务?

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

我所追求的本质上是在后台运行应用程序 A,并让一个或多个应用程序 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.

没有办法在后台永久执行任务,但你可以使用有限长度的任务来做到这一点,当你制作一个有限长度的任务时,这会在应用程序处于活动状态时一直运行,但是当你点击主页按钮,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 分钟内调用相同的任务,因此.

我在创建接口服务"的项目中使用它.我让你在这里的代码:

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
    //  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];
    

    如果你需要阻止它:

    [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天全站免登陆