Подтвердить что ты не робот

Предупреждение, которое может работать на iOS 7 и iOS 8

Я получаю dyld: Символ не найден: _OBJC_CLASS _ $_ UIAlertAction когда я пытаюсь запустить это чудовище.

Как я могу использовать слабые ссылки 8.0?

var device : UIDevice = UIDevice.currentDevice()!;
var systemVersion = device.systemVersion;
var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue;
if(iosVerion < 8.0) {
    let alert = UIAlertView()
    alert.title = "Noop"
    alert.message = "Nothing to verify"
    alert.addButtonWithTitle("Click")
    alert.show()
} else {
    var alert : UIAlertController? = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert)
    if alert {
        let actionStyle : UIAlertActionStyle? = UIAlertActionStyle.Default;
        var alertAction : UIAlertAction? = UIAlertAction(title: "Click", style: actionStyle!, handler: nil)
        if(alertAction) {
            alert!.addAction(alertAction)
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
}
return;

Решено: UIKit должен быть помечен как необязательный, а не обязательный. Упрощенная версия теперь:

var device : UIDevice = UIDevice.currentDevice()!;
var systemVersion = device.systemVersion;
var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue;
if(iosVerion < 8.0) {
    let alert = UIAlertView()
    alert.title = "Noop"
    alert.message = "Nothing to verify"
    alert.addButtonWithTitle("Click")
    alert.show()
} else {
    var alert : UIAlertController = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Click", style:.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}
4b9b3361

Ответ 1

  • Явно добавьте UIKit в раздел "Связывание двоичных файлов с библиотеками" фаз сборки проекта.

  • Вы можете проверить наличие UIAlertController следующим образом:

    if NSClassFromString("UIAlertController") != nil {
        // Use it
    } else {
        // Fall back
    }
    
  • Я написал обертку, которая работает как на iOS 7, так и на iOS 8. Вы можете найти ее здесь. Он принимает контроллер вида, за которым следует куча необязательных аргументов и любое количество кнопок:

    showAlert(self, style: .ActionSheet, sourceView: cell, completion: {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    },
        (.Default, "Send clipboard", {
            if someCondition {
                // completion must be specified because of a Swift bug (rdar://18041904)
                showAlert(self, title: "Nothing to send", message: "The clipboard is empty.", completion: nil,
                    (.Cancel, "OK", nil)
                )
            }
        }),
        (.Cancel, "Cancel", nil)
    )
    

Ответ 2

В Xcode 6 beta 5 нет ничего в Link Phases → Link Binary With Libraries, поскольку строки Swift связаны неявно. В этом случае вам нужно добавить его вручную и отметить необязательно.

Кроме того, вместо проверки на версию системы вы можете просто проверить доступность UIAlertController

if nil != NSClassFromString("UIAlertController") {
  //show alertcontroller
  ...
} else {
  //show alertview
  ...
}

Ответ 3

Попробуйте ввести код для Objective C. Он отлично работает как для iOS 8, так и для версии ниже.

if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleCancel
                             handler:^(UIAlertAction *action)
                             {

                             }];
[alertVC addAction:cancelAction];

[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{

}];
}
else{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}