IOS App Extension
yaofly2012 opened this issue · comments
名词和术语:
名称 | 解释 |
---|---|
App extension | 应用扩展 |
Containing app | 载体应用,即应用扩展所属的应用(an app that contains one or more extensions is called a containing app)。应用扩展的容器在我们实现应用扩展的时候一并开发出来的。 |
Host App | 宿主应用,唤起AppExtension的App |
Extension Point | 扩展点(官方定义:A system area that enables extensions is called an extension point),是指一个特定的,范围明确的任务。即表明App Extension的能力(使用场景)。每个App Extension都对应且只能对应一个预定义的扩展点。 |
Extesion type template | XCode对每个Extension point提供target模板 |
Endpoint | |
Principal Class | App Extension的主入口类。当宿主App调用App Extension时,Extension Point会实例化Principal Class。 |
storyboard | The default storyboard file for the extension, usually named MainInterface. |
App Groups | |
separate screen | |
code sign | |
Embedded Framework | iOS Library, Bundle, and Frameworks, NSBundle |
Link Binary |
要求
- At a high level, the best user experience for all extensions is quick, streamlined, and focused on a single task.
- 尽量1s内启动,太慢了系统会自动关闭
- 单一职责,必须匹配指定的Extension point.
- Ensure Your iOS App Extension Works on All Devices.
- App Extension自定义UI要和对应的Extension Point协调
App Extension和载体应用关系
开发,发布,安装,启动,执行:
- 一起开发,发布,安装;
- App Extension由Host App发起启动请求,后由系统启动,并建立App Extension和Host App之间的通讯渠道;
- App Extension和载体应用本质互相独立的Bundle:
- 在两个独立的进程里执行
- 独立的Bundle ID,Scheme(不过这些配置一般派生自载体APP,比如载体APP叫
com.issuer.issuerApp
, 那App Extension可以是com.issuer.issuerApp.UIExt
)
如何工作的
生命周期
通讯
-
主要AppExtension和host app之间的通讯(
request/response
事务模式的通讯方式)
本质上系统使用进程间通信来确保主机应用程序和应用程序扩展可以协同工作以实现一致的体验 -
AppExtension和Containing App之间可以有限的通讯,只能打开Containing App
限制
创建App Extension
- 选择合适的Extension Point
- 创建App Extension Target
处理输入项:
After users work with the input items (if doing so is part of using the app extension), an app extension typically gives users a choice between completing or canceling the task
App Extension模板
- target代码结构
不使用模板
测试App Extension
注意:
- An app extension target must include the arm64 (iOS) or x86_64 architecture (OS X) in its Architectures build settings or it will be rejected by the App Store
其他场景
参考
In-App Provisioning Extension
Non-UI Extension
Authorization UI Extension
Troubleshoot
- iOS Apple Pay in-App provisioning extension:
viewDidLoad
method not called after invokinginit
在Apple Forum的相同提问
![](https://private-user-images.githubusercontent.com/6823623/244282343-42d8984a-d12e-4035-bdf9-6b397dbf952e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI1ODE0MzMsIm5iZiI6MTcyMjU4MTEzMywicGF0aCI6Ii82ODIzNjIzLzI0NDI4MjM0My00MmQ4OTg0YS1kMTJlLTQwMzUtYmRmOS02YjM5N2RiZjk1MmUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgwMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MDJUMDY0NTMzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MzhkN2U5YmJhN2QzMTk0ZDEyMTZhZGFiZjcxZDgxYTcwYTE2NzgwMmNhNWM3NjRiNGVhYTY1ZDhlYzhhYzdkMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.F5Fl9wJIFcseQVUA1ZPUSijzMPEBrYU3XUZj8fasXzk)
原因应该是Apple创建UI Extension的UIViewController
的实例后,发现UIViewController实例不正确(比如缺少相关方法),所以就提示了,也不继续挂载UIViewController
实例了(也就是只能看到UIViewController实例的
init和
dealloc`声明周期函数调用,其他方法就没有调用了)。
问题解决灵感来自Chat-GPT,Chat-GPT完整的回答如下:
#import <UIKit/UIKit.h>
#import <PassKit/PassKit.h>
@interface MyProvisioningExtensionViewController : UIViewController <PKIssuerProvisioningExtensionAuthorizationProviding>
@end
@implementation MyProvisioningExtensionViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Perform any additional setup for your provisioning extension view controller
}
- (void)beginAuthorizationWithCompletion:(void (^)(PKPaymentAuthorizationResult *))completion {
// Handle the authorization process
// Gather necessary information
NSDictionary *cardDetails = ...;
NSDictionary *authorizationCredentials = ...;
// Communicate with your backend or issuer
NSDictionary *authorizationResponse = ...;
// Provide the result to the completion block
PKPaymentAuthorizationResult *authorizationResult = [[PKPaymentAuthorizationResult alloc] initWithStatus:PKPaymentAuthorizationStatusSuccess errors:nil];
completion(authorizationResult);
}
@end
In this Objective-C code, MyProvisioningExtensionViewController is a subclass of UIViewController that adopts the PKIssuerProvisioningExtensionAuthorizationProviding protocol. It overrides the viewDidLoad method to perform any necessary setup for the provisioning extension's view controller.
The required method beginAuthorizationWithCompletion: is implemented to handle the authorization process. You should gather any necessary information, communicate with your backend or issuer, and provide the appropriate result to the completion block.
Make sure to replace the placeholder code (...) with your actual implementation for gathering necessary information, communicating with your backend or issuer, and providing the appropriate authorization result.
Remember to handle any errors or exceptional cases appropriately and provide meaningful error messages to the user when necessary.
期望Apple的回答....