建立 NSUserActivity
要建立 NSUserActivity
物件,你的應用必須在其 Info.plist
檔案中宣告它支援的活動型別。支援的活動由你的應用程式定義,並且應該是唯一的。使用反向域樣式命名方案(即“com.companyName.productName.activityName”)定義活動。以下是 Info.plist 中的條目可能如下所示:
鍵 | 值 |
---|---|
NSUserActivityTypes |
[陣列] |
- item0 |
com.companyName.productName.activityName01 |
- item1 |
com.companyName.productName.activityName02 |
一旦定義了所有支援的活動型別,你就可以開始在應用程式的程式碼中訪問和使用它們。
要建立 NSUserActivity
物件,你必須執行以下操作
// Initialize the activity object and set its type from one of the ones specified in your app's plist
NSUserActivity *currentActivity = [[NSUserActivity alloc] initWithActivityType:@"com.companyName.productName.activityName01"];
// Set the title of the activity.
// This title may be displayed to the user, so make sure it is localized and human-readable
currentActivity.title = @"Current Activity";
// Configure additional properties like userInfo which will be included in the activity
currentActivity.userInfo = @{@"informationKey" : @"value"};
// Configure the activity so the system knows what may be done with it
// It is important that you only set YES to tasks that your application supports
// In this example, we will only enable the activity for use with Handoff
[currentActivity setEligibleForHandoff:YES];
[currentActivity setEligibleForSearch:NO]; // Defaults to NO
[currentActivity setEligibleForPublicIndexing:NO]; // Defaults to NO
// Set this activity as the current user activity
// Only one activity may be current at a time on a device. Calling this method invalidates any other current activities.
[currentActivity becomeCurrent];
在此之後,上面的活動應該可用於 Handoff(儘管需要更多的工作才能正確處理切換)。