慢速iOS共享扩展

d2b*_*rke 4 ios uistoryboardsegue ios8 ios8-share-extension

我正在开发一个共享扩展程序,只需抓取一个链接,选择几个名称来共享它,然后分享.数据层尚未添加,只有用于在tableview中显示某些名称的UI(使用自定义单元格),我从扩展上下文中提取共享URL.VC中的所有代码都在下面.所有视图都在故事板中设置.两个UIButtons,两个UILabels,一个TableView和一个UIView来支撑它,所以我可以很容易地绕过角落.

在此输入图像描述

我遇到的问题是,_linkLabel我使用显示器的URL在近10秒钟内无法直观更新!What.In.The.World.我在这做什么导致了这个?

我正在退出回调中的URL,hasItemConformingToTypeIdentifier一旦扩展名出现就会发生,但不会更新标签?? !! 帮助.请.

#import "ShareViewController.h"
#import "UserCell.h"

@interface ShareViewController ()

@end

@implementation ShareViewController

- (void)viewDidLoad{
    self.view.alpha = 0;
    _friends = [@[@"Ronnie",@"Bobby",@"Ricky",@"Mike"] mutableCopy];
    _containerView.layer.cornerRadius = 6.f;
    _selectedIndexPaths = [[NSMutableArray alloc] init];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIView animateWithDuration:0.5 animations:^{
        self.view.alpha = 1;
    }];
}

- (void)viewDidAppear:(BOOL)animated{
    //pull the URL out
    NSExtensionItem *item = self.extensionContext.inputItems[0];
    NSItemProvider *provider = item.attachments[0];
    if ([provider hasItemConformingToTypeIdentifier:@"public.url"]) {
        [provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
            NSURL *url = (NSURL*)item;
            _linkLabel.text = url.absoluteString;
            NSLog(@"Link: %@", url.absoluteString);
        }];
    }
    else{
        NSLog(@"No Link");
    }
}

#pragma mark - UITableView Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UserCell *cell = (UserCell*)[tableView cellForRowAtIndexPath:indexPath];
    if([_selectedIndexPaths containsObject:indexPath]){
        [_selectedIndexPaths removeObject:indexPath];
        cell.selected = NO;
    }
    else{
        cell.selected = YES;
        [_selectedIndexPaths addObject:indexPath];
    }
    NSLog(@"Share to %i friends", (int)[_selectedIndexPaths count]);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Later, calc height based on text in comment
    return  44;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_friends count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"UserCell";
    UserCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selected = ([_selectedIndexPaths containsObject:indexPath]) ? YES : NO;
    cell.nameLabel.text = [_friends objectAtIndex:indexPath.row];
    return cell;
}

- (IBAction)dismiss {
    [UIView animateWithDuration:0.34 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
    }];
}

@end
Run Code Online (Sandbox Code Playgroud)

Tom*_*ton 7

UI元素更新的延迟是尝试从主队列外部更新UI的典型标志.这是在这发生了什么.你有这个:

[provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
    NSURL *url = (NSURL*)item;
    _linkLabel.text = url.absoluteString;
    NSLog(@"Link: %@", url.absoluteString);
}];
Run Code Online (Sandbox Code Playgroud)

除此之外NSItemProvider不保证将在您启动的同一队列上调用完成处理程序.你几乎可以保证在这里有一个不同的队列,所以你得到了这个奇怪的延迟.您需要调度回主队列以执行更新:

[provider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURL *url = (NSURL*)item;
        _linkLabel.text = url.absoluteString;
        NSLog(@"Link: %@", url.absoluteString);
    });
}];
Run Code Online (Sandbox Code Playgroud)