メッセージアプリがあり、ウィジェットの作成を開始しました。ユーザーがアプリを開くと、コア データが新しいメッセージで更新されます。私の願いはいつですか:
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler
と呼ばれる を取得し、UIViewControllerメッセージ取得スレッドを呼び出します。をウィジェット ターゲットにリンクするUIViewControllerと、エラーが発生しました。
'sharedApplication' is unavailable....
なのでキャンセルしました。
私が達成しようとしていること: 1. widgetPerformUpdateWithCompletionHandler が呼び出されています 2. アプリケーションはメッセージ取得スレッド/メソッドを開始します 3. 終了すると、NSUserDefaults を使用してデータをウィジェットに送り返します
私のコード:
1:
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler
{
    // Perform any setup necessary in order to update the view.
    [self startGetMessages];
    // If an error is encountered, use NCUpdateResultFailed
    // If there's no update required, use NCUpdateResultNoData
    // If there's an update, use NCUpdateResultNewData
    completionHandler(NCUpdateResultNewData);
}
2:
- (void)startGetMessages
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSBundle *deviceBundle = [NSBundle mainBundle];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:deviceBundle];
    id MainController = [storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
    SEL getMessagesSelector = NSSelectorFromString(@"startGetMessages:");
    if (MainController)
    {
        NSThread *startGetMessagesThread = [[NSThread alloc] initWithTarget:MainController
                                                                   selector:getMessagesSelector
                                                                     object:StringForInt(HRTableDataSourceKindUpdate)];
        [startGetMessagesThread start];
    }
}
3:
- (void)notifyWidgetForChanges
{
    __block NSMutableDictionary *newMessages = [NSMutableDictionary new];
    NSArray *results = [CoreDataPhotoRecord MR_findAllSortedBy:@"message.originalDate"
                                                     ascending:NO
                                                 withPredicate:[NSPredicate predicateWithFormat:@"(message.delete_message == %@) AND (message.type.integerValue == %d) AND (message.originalDate >= %@)",
                                                                @NO, NORMAL_MESSAGE, _notiftWidgetDate]];
    NSLog(@"%s, _notiftWidgetDate: %@, newMessages.count: %d", __PRETTY_FUNCTION__, _notiftWidgetDate, newMessages.count);
    [results enumerateObjectsUsingBlock:^(CoreDataPhotoRecord *photoDetails, NSUInteger idx, BOOL *stop)
    {
        if (photoDetails != nil && photoDetails.message != nil)
        {
            NSString *cleanMobile = [[ABAddressBook sharedAddressBook] getCleanMobile:photoDetails.message.mobile];
            Contact *person = [[ABAddressBook sharedAddressBook] findContactWithPhoneNumber:cleanMobile];
            ContactWidget *contact = [[ContactWidget alloc] init];
            contact.name = (person != nil && person.name != nil && person.name.length > 0) ? person.name : cleanMobile;
            [newMessages setObject:contact forKey:cleanMobile];
        }
    }];
    [SharedUtilities archiveObject:newMessages.copy forKey:MESSAGES_KEY_NEW widget:true];
    [DEFAULTS_WIDGET setObject:@"111" forKey:@"111"];
    [DEFAULTS_WIDGET synchronize];
    newMessages = nil;
    results = nil;
}
widgetDefaults = [[NSUserDefaults alloc] initWithSuiteName:WIDGET_GROUP_NAME];
MainControllerステップ 2 が nil であるため、何も起こりません。私に何ができる?