AlertView Delegate with UIBackgroundFetchResult崩溃应用程序

问题描述:

我试图在应用程序处于后台时获取新的朋友请求。 抓取工作正常,它的alertView导致应用程序崩溃。我相信它是因为我使用的代理值不正确,当我使用delegate:nil时,应用程序不会崩溃。AlertView Delegate with UIBackgroundFetchResult崩溃应用程序

这里是我的应用程序的故事板,红色箭头是执行fetchNewDataWithCompletionHandler代码的地方。

screen shot, note the arrow is displayed where the code fetchNewDataWithCompletionHandler 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]; 

然后我有这样的方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {................} 

但它似乎是编译器无法找到didDismissWithButtonIndex代码的方法。因为只要按下Accept or Ignore按钮中的任何一个,该应用就会崩溃。

正如你可以在我的应用程序中看到有四个选项卡:主页,产品,朋友和帐户。 即使我选择模拟器中的朋友选项卡,然后生存后台提取过程,它仍然崩溃。 我也尝试使用selectedIndex切换到标签栏,但那也不起作用。

我被卡住了,请指教,谢谢。

在demo_Friends_ViewController.h,你忘了UIAlertViewDelegate;)

@interface demo_Friends_ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate> 
+0

谢谢,刚才添加,但仍死机一样。 – user3550458 2014-11-25 14:14:05

+0

您是否找到解决方案或者仍然没有? – Atomnium 2014-11-25 22:53:34

+0

好吧,我切换到正在工作的NSNotificationCenter,就像我想要的一样。 – user3550458 2014-11-26 13:48:48