问题
I'm starting a newsstand application and first I'm testing all the framework to see who everything works. I already downloaded an issue triggered by a notification when in foreground. but I don't know how to download in background, or at least I'm missing something... Here is the stuff I added to plist:
The app is targeted for IOS 5... here is my code... of course I also implemented the three URLConection methods of NKAssetDownload
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([launchOptions objectForKey:UIApplicationLaunchOptionsNewsstandDownloadsKey] || [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
NKLibrary *nkLib = [NKLibrary sharedLibrary];
for(NKAssetDownload *asset in [nkLib downloadingAssets]) {
[asset downloadWithDelegate:self];
}
}else{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeNewsstandContentAvailability
)];
}
[[NSUserDefaults standardUserDefaults]setBool: YES forKey:@"NKDontThrottleNewsstandContentNotifications"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"didReceiveRemoteNotification");
if (userInfo) {
NKIssue *issue4 = [[NKLibrary sharedLibrary] issueWithName:@"01_Primera"];
if (!issue4) {
issue4= [[NKLibrary sharedLibrary] addIssueWithName:@"01_Primera" date:[NSDate date]];
}
if([issue4 status]==NKIssueContentStatusNone) {
NSURL *downloadURL = [NSURL URLWithString:@"http://www.viggiosoft.com/media/data/blog/newsstand/magazine-4.pdf"];
NSURLRequest *req = [NSURLRequest requestWithURL:downloadURL];
NKAssetDownload *assetDownload = [issue4 addAssetWithRequest:req];
[assetDownload downloadWithDelegate:self];
}
}
}
What am I missing, and also do I have extra unnecessary code? please help.
回答1:
If you are testing waking up the app (after swiping it away) with a notification with content-available:1 on iOS7, it has a bug: read here and here (log in with your dev account). It should work on iOS 5-6, if you have a device to test that hasn't been updated.
You also need a key in the Info.plist: Required background modes - newsstand-content
Finally in your code I think you should change a bit in your didFinishLaunchingWithOptions:
if ([launchOptions objectForKey:UIApplicationLaunchOptionsNewsstandDownloadsKey] || [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) { [self handleNotification:launchOptions]; } //...other code... //This code can be the same as with didReceiveRemoveNotification -(void) handleNotification:(NSDictionary*)userInfo{ //check userInfo for "content-available" key //if there is content-available:1 check for an issue_id/content_id in the rest of the notification payload (userInfo), and download the issue }
来源:https://stackoverflow.com/questions/20764967/ios-newsstand-background-troubleshouting