2014-09-23 27 views
2

更新Xcode6後,當沒有找到符號,我得到這個代碼墜毀上IOS 7「Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings」,任何一個可以幫助它使用-ObjC/-all_load連接標誌

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert) categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
} 
else { 
    int notifyType = (UIRemoteNotificationTypeAlert | 
         UIRemoteNotificationTypeBadge | 
         UIRemoteNotificationTypeSound); 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)notifyType]; 
} 

編輯: 喜總之, 這是一個運行時的崩潰,而不是一個編譯時鏈接錯誤,

異常類型:EXC_BREAKPOINT(SIGTRAP) 異常代碼:0x0000000000000001,0x00000000e7ffdefe 通過主題:0觸發

dyld的錯誤消息: 找不到符號:_OBJC_CLASS _ $ _ UIUserNotificationSettings

和我在的Xcode 6.0(6A313),所以我不應該用任何#如果到指標的iOS版本。而這個代碼適用於iOS模擬器8罰款,但崩潰在IOS 7設備

編輯2:

最後,這個問題是由這些代碼固定的,我有以下標記正確的答案,謝謝trojanfoe。

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
{ 
    Class userNotifyClass = NSClassFromString(@"UIUserNotificationSettings"); 
    if(userNotifyClass != nil) 
    { 
     id notifyInstance = [userNotifyClass settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil]; 
     [application registerUserNotificationSettings:notifyInstance]; 
     [application registerForRemoteNotifications]; 
    } 
} 
else 
{ 
    int notifyType = (UIRemoteNotificationTypeAlert | 
         UIRemoteNotificationTypeBadge | 
         UIRemoteNotificationTypeSound); 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)notifyType]; 
} 
+0

通過 「代碼崩潰」 你的意思是 「鏈接錯誤」? – trojanfoe 2014-09-23 08:00:58

+0

@ Daij-Djan抱歉,我不明白你的意思。 – trojanfoe 2014-09-23 08:08:44

+0

你在ARM64上測試嗎? – Andy 2014-09-23 13:20:18

回答

0

您必須使用iOS SDK版本低於8編譯,以獲得該鏈接錯誤。

要解決此問題使用iOS 8 SDK(即Xcode的6),並檢查類在運行時使用存在:

if (NSClassFromString(@"UIUserNotificationSettings")) { 
    // do thing 
} 

EDIT從OP進一步信息之後:

  • 他正在使用Xcode 6和iOS 8 SDK。
  • 他正在使用-ObjC-all_load其他鏈接標記

這是-ObjC/-all_load標誌時停止測試的通常技術的一類存在在運行時和選擇性地使用它。

爲了使應用程序支持的iOS 7和iOS 8 -ObjC語義,我能想到的兩種解決方案:

  • 刪除使用贊成-force_load-ObjC/-all_load連接標誌,作爲目標一個特定的「全部負載」庫(請參閱my question here)。
  • 使用更動態的(且容易出錯)的辦法:

    1. 使用NSClassFromString()創建的UIUserNotificationSettings一個實例。
    2. 使用performSelector調用方法。

我贊成第一種方法。

+0

嗨,這是一個運行時崩潰,一些「薄弱環節」,而不是編譯時錯誤 – wangsihao 2014-09-23 08:17:49

+0

@wangsihao你使用'-ObjC' *其他鏈接器標誌*? – trojanfoe 2014-09-23 08:20:11

+0

是的,我用-ObjC和-all_load – wangsihao 2014-09-23 08:26:57

0

UIUserNotificationSettings僅適用於iOS8上

if(NSClassFromString(@"UIUserNotificationSettings") != nil) 
{ 
    //do your stuff here 
} 
+0

爲真,但他已檢查選擇器。這是一個鏈接器錯誤 – 2014-09-23 08:03:41

+0

這是真的,但我已經有一個選擇器檢查在這裏,我也嘗試了「NSClassFromString」,仍然是一個相同的錯誤崩潰。 – wangsihao 2014-09-23 08:06:10

+0

dyld:未找到符號,運行時鏈接錯誤,我有Xcode版本6.0(6A313),我應該更新到6.0.1並嘗試? – wangsihao 2014-09-23 08:07:07

2

我認爲你正在構建你的項目與「基地SDK」 7.x的 所以,你可以將其更改爲8.0或編譯期間檢查當前版本:

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1  

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert) categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
#else 
    int notifyType = (UIRemoteNotificationTypeAlert | 
         UIRemoteNotificationTypeBadge | 
         UIRemoteNotificationTypeSound); 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)notifyType]; 
#endif 
+0

我使用「最新的iOS(iOS8.0)」作爲基礎sdk。 – wangsihao 2014-09-23 08:16:59

+0

也許這個答案可以幫助你http://stackoverflow.com/questions/24043532/dyld-symbol-not-found-nsurlauthenticationmethodclientcertificate-when-trying – 2014-09-23 09:06:35

+0

謝謝,我想用這個解決方案,以後反饋 – wangsihao 2014-09-24 02:21:20

1

對我的作品上了XCode 6.1 @ iOS 8 SDK/iOS 7部署目標。我的應用也使用-ObjC鏈接器標誌。

剛在iPhone上運行的應用程序與iOS 7.1.2/ARMV7。我用下面的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
     UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil]; 
     [application registerUserNotificationSettings:notificationSettings]; 
     [application registerForRemoteNotifications]; 
    } else { 
     [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; 
    } 
    return YES; 
} 

更新:在iPad 4補測/ iOS 8的,iPhone 5S /的iOS 8.所有工作正常。

+0

與我的代碼相同,但我遇到了崩潰。 – wangsihao 2014-09-24 03:03:07

1

change UIKit.framework to optional works to me

變化UIKit.framework到可選的作品給我