I am trying to fetch new friend requests while the app is in the background.
The fetch is working fine, its the alertView that is causing the app to crash. I believe its because of the incorrect delegate value that i am using, when i use delegate:nil
then app doesn't crash.
Here is the storyboard of my app, the red arrow is where the fetchNewDataWithCompletionHandler code is being executed.
AppDelegate.m
#import "demo_AppDelegate.h"
#import "demo_Friends_ViewController.h"
@implementation demo_AppDelegate
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSDate *fetchStart = [NSDate date];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
demo_Friends_ViewController *friends_vc = (demo_Friends_ViewController *) [storyboard instantiateViewControllerWithIdentifier:@"friendsTab"];
[friends_vc fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
NSDate *fetchEnd = [NSDate date];
NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
}];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
diskCapacity:25 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
demo_Friends_ViewController.h
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface demo_Friends_ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
@end
demo_Friends_ViewController.m
-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
/* some code here for fetching */
completionHandler(UIBackgroundFetchResultNewData);
[UIApplication sharedApplication].applicationIconBadgeNumber = 1;
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"New Friend Request!"
message:responseObject[@"message"]
delegate:self
cancelButtonTitle:@"Ignore"
otherButtonTitles:@"Accept", nil] ;
alertView.tag = 2;
[alertView show];
then i have this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {................}
but it seems like the compiler couldn't locate didDismissWithButtonIndex
code method. Because as soon as i press any of Accept or Ignore
buttons, the app crashes.
As you can see in my app there are four tabs: Home, Products, Friends and Account. Even if i select the Friends tab in simulator and then beings the Background Fetch Process, it still gets crashed. I also tried switching to tab bar using selectedIndex, but that is also not working.
I am stuck and lost, please advice, thank you.
In demo_Friends_ViewController.h, you forgot the UIAlertViewDelegate ;)