【问题标题】:Slow iOS Share Extension慢速 iOS 共享扩展
【发布时间】:2014-11-12 18:51:17
【问题描述】:

我正在开发一个共享扩展程序,只需获取一个链接,选择几个名称来共享它,然后共享。数据层尚未添加,只有 UI 用于在 tableview 中显示一些名称(使用自定义单元格),并且我正在从扩展上下文中提取共享 URL。 VC中的所有代码如下。所有视图都在情节提要中设置。两个 UIButtons、两个 UILabels、一个 TableView 和一个 UIView 来容纳它们,所以我可以轻松地绕过角落。

我遇到的问题是,我正在使用显示 URL 的 _linkLabel 在近 10 秒内没有视觉更新! What.In.The.World。我在这里做了什么导致了这种情况?

我正在注销来自hasItemConformingToTypeIdentifier 的回调中的 URL,它会在扩展出现后立即发生,但不会更新标签??!!有帮助。请。

#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

【问题讨论】:

    标签: ios ios8 uistoryboardsegue ios8-share-extension


    【解决方案1】:

    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);
    }];
    

    除了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);
        });
    }];
    

    【讨论】:

    • 做到了 :) 很好的答案汤姆
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多