我正在尝试为我现有的 iOS 7+ 应用程序创建 Today Extension(又名 Widget)。在 iOS 模拟器中,一切正常(大部分时间),但在我的设备上,小部件是空的——只显示标题/名称,但没有内容。
我发现有几个线程处理类似的问题,但它们都与 Swift 应用程序中的一些初始化问题有关。我使用的是 Objectiv-c 而不是 Swift。
这是我做的:
- 向我的应用添加了一个新的 Today Extension 目标。相应的 方案也是自动创建的。
- 使用未更改的默认 Widget 时也会出现此问题。我只添加了 init-methodes 以查看它们是否被正确调用。所以小部件应该显示默认的
Hello World
标签。
这是代码:
@interface TodayViewController () <NCWidgetProviding>
@end
@implementation TodayViewController
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self)
NSLog(@"initWithCoder");
return self;
}
- (id)init {
self = [super init];
if (self)
NSLog(@"init");
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
NSLog(@"initWithNibName");
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
completionHandler(NCUpdateResultNewData);
}
@end
选择小部件方案并在模拟器中运行它时,选择“今日”作为容器后,小部件正确显示。此外,还会记录 initWithCoder
。
最初在设备上运行时,一切都按预期进行:出现今日屏幕并显示小部件。我的小部件也是如此,但没有任何内容。
然后 Xcode 显示以下消息:
Lost connection to "Test Device" - Restore the connection to "Test Device" and run "com.example.MyApp.Widget" again, or if "com.example.MyApp.Widget" is still running, you can attach to it by selecting Debug > Attach to Process > com.example.MyApp.Widget.
没有任何记录,我认为这是因为连接丢失。但是为什么小部件是空的?
我查看了设备日志,但没有崩溃。这个问题在我的 iPhone 6 (iOS 8.0) 和 iPad Air 2 (iOS 8.1) 上都是一样的
非常感谢!
最佳答案
经过几天的搜索,我终于找到了解决方案:
我的项目(小部件的包含应用程序)和小部件本身不包含 arm64 架构。作为Apple docs描述这不是一个有效的配置:
A containing app that links to an embedded framework must include the arm64 (iOS) or x86_64 (OS X) architecture build setting or it will be rejected by the App Store. (As described in Creating an App Extension, all app extensions must include the appropriate 64-bit architecture build setting.)
当缺少 x64 时,应用不仅会被拒绝,还会阻止小部件首先显示。
我将小部件添加到尚未针对 x64 配置的现有项目中,似乎相同的build设置已自动应用于小部件。如果 Xcode 会显示有关此问题的任何警告或提示,则可以避免很多工作......
我做了以下事情来解决这个问题:
- 单击项目导航器中的项目条目并选择应用程序目标。
- 选择
Build Settings
选项卡并导航到Architectures
部分。 - 将
Architectures
设置为Standard architectures (armv7, arm64)
- 将
Valid Architectures
设置为armv7 armv7s armv8 arm64
- 将
Build Active Architectures Only
设置为No
- 将相同的设置应用于小部件目标
此后小部件在模拟器和我的测试设备上都能正常工作。
关于ios - Today Widget 在 iOS 8 设备上没有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26551609/