美文网首页
远程推送和后台数据获取

远程推送和后台数据获取

作者: liboxiang | 来源:发表于2018-05-31 22:53 被阅读24次

    详情:https://www.objc.io/issues/5-ios7/multitasking/

    Background Fetch

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    
        return YES;
    }
    
    - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
        NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
    
        NSURL *url = [[NSURL alloc] initWithString:@"http://yourserver.com/data.json"];
        NSURLSessionDataTask *task = [session dataTaskWithURL:url 
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
            if (error) {
                completionHandler(UIBackgroundFetchResultFailed);
                return;
            }
    
            // Parse response/data and determine whether new content was available
            BOOL hasNewData = ...
            if (hasNewData) {
                completionHandler(UIBackgroundFetchResultNewData);
            } else {
                completionHandler(UIBackgroundFetchResultNoData);
            }
        }];
    
        // Start the task
        [task resume];
    }
    

    远程推送

    推送内容结构
    {
        "aps" : {
            "content-available" : 1
        },
        "content-id" : 42
    }
    
    实现
    SELECT ALL
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
        NSLog(@"Remote Notification userInfo is %@", userInfo);
    
        NSNumber *contentID = userInfo[@"content-id"];
        // Do something with the content ID
        completionHandler(UIBackgroundFetchResultNewData);
    }
    

    后台网络请求

    - (NSURLSession *)backgroundURLSession
    {
        static NSURLSession *session = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSString *identifier = @"io.objc.backgroundTransferExample";
            NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
            session = [NSURLSession sessionWithConfiguration:sessionConfig 
                                                    delegate:self 
                                               delegateQueue:[NSOperationQueue mainQueue]];
        });
    
        return session;
    }
    
    - (void)           application:(UIApplication *)application 
      didReceiveRemoteNotification:(NSDictionary *)userInfo 
            fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
        NSLog(@"Received remote notification with userInfo %@", userInfo);
    
        NSNumber *contentID = userInfo[@"content-id"];
        NSString *downloadURLString = [NSString stringWithFormat:@"http://yourserver.com/downloads/%d.mp3", [contentID intValue]];
        NSURL* downloadURL = [NSURL URLWithString:downloadURLString];
    
        NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
        NSURLSessionDownloadTask *task = [[self backgroundURLSession] downloadTaskWithRequest:request];
        task.taskDescription = [NSString stringWithFormat:@"Podcast Episode %d", [contentID intValue]];
        [task resume];
    
        completionHandler(UIBackgroundFetchResultNewData);
    }
    

    当网络请求完成的时候,如果app还处于后台状态,需要实现下面代码

    - (void)application:(UIApplication *)application 
      handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
    {
        // You must re-establish a reference to the background session, 
        // or NSURLSessionDownloadDelegate and NSURLSessionDelegate methods will not be called
        // as no delegate is attached to the session. See backgroundURLSession above.
        NSURLSession *backgroundSession = [self backgroundURLSession];
    
        NSLog(@"Rejoining session with identifier %@ %@", identifier, backgroundSession);
    
        // Store the completion handler to update your UI after processing session events
        [self addCompletionHandler:completionHandler forSession:identifier];
    }
    
    - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
    {
        NSLog(@"Background URL session %@ finished events.\n", session);
    
        if (session.configuration.identifier) {
            // Call the handler we stored in -application:handleEventsForBackgroundURLSession:
            [self callCompletionHandlerForSession:session.configuration.identifier];
        }
    }
    
    - (void)addCompletionHandler:(CompletionHandlerType)handler forSession:(NSString *)identifier
    {
        if ([self.completionHandlerDictionary objectForKey:identifier]) {
            NSLog(@"Error: Got multiple handlers for a single session identifier.  This should not happen.\n");
        }
    
        [self.completionHandlerDictionary setObject:handler forKey:identifier];
    }
    
    - (void)callCompletionHandlerForSession: (NSString *)identifier
    {
        CompletionHandlerType handler = [self.completionHandlerDictionary objectForKey: identifier];
    
        if (handler) {
            [self.completionHandlerDictionary removeObjectForKey: identifier];
            NSLog(@"Calling completion handler for session %@", identifier);
    
            handler();
        }
    }
    

    相关文章

      网友评论

          本文标题:远程推送和后台数据获取

          本文链接:https://www.haomeiwen.com/subject/zlicsftx.html