How to call an application to retrieve data via REST-Service?

I am looking for a way to run an application to retrieve data from a remote REST service. The approach should not require a survey.

IOS push notifications do not seem to be an option because the user can disable it. However, I could be wrong.

Is there a better way to do this?

+4
source share
4 answers

In fact, Push Notification is the way to go.

ios8 push- . - ( , , json , ), .

:

, . , .

+5



@Scholle,
1. NSURLRequest:, NSURLRequest NSURLConnetionDelegate. JSON.
NSUrlConnectionDidReceiveData

NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myNSData      options:kNilOptions error:&error];

if (error != nil) {
    NSLog(@"Error parsing JSON.");
}
else {
    NSLog(@"Array: %@", jsonArray);
}



2. AFNetworking afnetworking 3.0 . , .

- (void)requestWithURL:(NSString *)url parameterDictionary:(NSMutableDictionary *)data  requestType:(NSString *)reqType  handler:(NSObject *)handler selector:(SEL)selector{
// Request Type is GET or POST
// handler is set to be self if we want to handle response in selector method defined in same viewController

NSError *error;

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:reqType URLString:[NSString stringWithFormat:@"%@",url] parameters:nil error:nil];

req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
if (data != nil) {
    NSLog(@"Data is not nil");
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

}else{
    NSLog(@"Data is nil");
}



[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    if (!error) {
 //            NSLog(@"Reply JSON: %@", responseObject);
        [handler performSelector:selector withObject:responseObject];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            //blah blah
        }
    } else {
        NSLog(@"Error: %@, %@, %@", error, response, responseObject);
        [handler performSelector:selector withObject:responseObject];
    }
}] resume];
}
+1

iOS . Apple , , , .

, iOS 5 6, Apple , , , . , ( , ).

, , , . , Background fetch " " "" Xcode. ( , UIBackgroundModes Info.plist .) , . , . , , .

documentation, , beginBackgroundTaskWithName: expirationHandler:

+1

, , , , , .

, , , .

+1

All Articles