当用户仅从iOS 11中的活动控制器中选择"复制"时,该URL将重复.它在iOS 10上正常运行
使用以下代码
@IBAction func shareButtonPressed() {
guard let url = URL(string: "http://google.com") else { return }
let shareText = "Share Text!"
let items: [Any] = [shareText, url]
let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
将共享文本提供为:
Share Text!
http://google.comhttp://google.com
Run Code Online (Sandbox Code Playgroud) 我已经为我的应用添加了iCloud支持.当用户更新他的数据时,我收到了通知NSPersistentStoreDidImportUbiquitousContentChangesNotification
那很棒
但是,我有这些问题
will这个通知的版本,我可以用它来显示加载器,例如数据库将被更新will版本,所以我使用相同的通知来显示加载器,告诉用户数据库正在更新...此通知中的问题是,如果内容发生更改,可以多次调用它iCloud相对较大,导致我显示几个加载指示器!.
我通过这样做添加了对Core数据的iCloud支持:
在NSManagedObjectContext中
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
Run Code Online (Sandbox Code Playgroud)
在NSPersistentStoreCoordinator中
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSPersistentStoreUbiquitousContentNameKey:@"iCloudStore"} error:nil];
Run Code Online (Sandbox Code Playgroud)
并在NSPersistentStoreDidImportUbiquitousContentChangesNotification通知方法
- (void)persistentStoreDidImportUbiquitousContentChanges:(NSNotification *)notification
{
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
[managedObjectContext performBlock:^{
[managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
dispatch_async(dispatch_get_main_queue(), ^{
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
MBProgressHUD *hud = [MBProgressHUD HUDForView:mainWindow];
if (hud == nil) {
hud = [[MBProgressHUD alloc] initWithWindow:mainWindow];
}
[mainWindow addSubview:hud];
hud.labelText = NSLocalizedString(@"Progress updated", nil);
[hud show:YES];
[hud hide:YES …Run Code Online (Sandbox Code Playgroud) 要获取Objective-c中的rootViewController,您可以使用下面的代码行
[[[UIApplication sharedApplication] delegate] window].rootViewController
Run Code Online (Sandbox Code Playgroud)
我试图在swift中做同样的事情
UIApplication.sharedApplication().delegate?.window?.rootViewController
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误
"的UIWindow?没有名为'rootViewController'的成员
和建议说,你必须解开一个UIWindow两次UIWindow??是
UIApplication.sharedApplication().delegate?.window??.rootViewController
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么我需要打开窗口两次?
.
.
我查看了API并发现了
protocol UIApplicationDelegate : NSObjectProtocol {
optional var window: UIWindow? { get set }
}
Run Code Online (Sandbox Code Playgroud)
窗口有一个可选的
class UIWindow : UIView {
var rootViewController: UIViewController?
}
Run Code Online (Sandbox Code Playgroud)
rootViewController也有一个可选项
.
.
我想可能是因为UIApplicationDelegate协议中的UIWindow已经有了optional,UIWindow?所以我在Playground中尝试了以下内容
@objc protocol MyApplicationDelegate {
optional var myWindow: MyWindow? { get set }
}
class MyWindow : NSObject {
var rootViewController: Int?
init(number: Int) {
rootViewController = number
}
}
class …Run Code Online (Sandbox Code Playgroud)