Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. id delegate = [[UIApplication sharedApplication] delegate];
  2. Class objectClass = object_getClass(delegate);
  3. NSString *newClassName = [NSString stringWithFormat:@"Custom_%@", NSStringFromClass(objectClass)];
  4. Class modDelegate = NSClassFromString(newClassName);
  5.  
  6. if (modDelegate == nil)
  7. {
  8. modDelegate = objc_allocateClassPair(objectClass, [newClassName UTF8String], 0);
  9.  
  10. SEL selectorToOverride4 = @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:);
  11. SEL selectorToOverride5 = @selector(application:didFailToRegisterForRemoteNotificationsWithError:);
  12. SEL selectorToOverride6 = @selector(application:didReceiveRemoteNotification:);
  13. //SEL selectorToOverride7 = @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);
  14.  
  15. // get the info on the method we're going to override
  16. Method m4 = class_getInstanceMethod(objectClass, selectorToOverride4);
  17. Method m5 = class_getInstanceMethod(objectClass, selectorToOverride5);
  18. Method m6 = class_getInstanceMethod(objectClass, selectorToOverride6);
  19. //Method m7 = class_getInstanceMethod(objectClass, selectorToOverride7);
  20.  
  21. // add the method to the new class
  22. class_addMethod(modDelegate, selectorToOverride4, (IMP)didRegisterSuccess, method_getTypeEncoding(m4));
  23. class_addMethod(modDelegate, selectorToOverride5, (IMP)didRegisterFailure, method_getTypeEncoding(m5));
  24. class_addMethod(modDelegate, selectorToOverride6, (IMP)didReceiveRemoteNotification, method_getTypeEncoding(m6));
  25. //class_addMethod(modDelegate, selectorToOverride7, (IMP)didReceiveRemoteNotificationFetchCompletionHandler, method_getTypeEncoding(m7));
  26.  
  27. // register the new class with the runtime
  28. objc_registerClassPair(modDelegate);
  29. }
  30.  
  31. // change the class of the object
  32. object_setClass(delegate, modDelegate);
  33.  
  34. void didRegisterSuccess(id self, SEL _cmd, UIApplication *application, NSData *deviceToken)
  35. {
  36. [base didRegisterSuccess:application deviceToken:deviceToken];
  37. }
  38.  
  39. void didRegisterFailure(id self, SEL _cmd, UIApplication *application, NSError *error)
  40. {
  41. [base didRegisterFailure:application error:error];
  42. }
  43.  
  44. void didReceiveRemoteNotification(id self, SEL _cmd, UIApplication *application, NSDictionary *userInfo)
  45. {
  46. [base didReceiveRemoteNotification:application userInfo:userInfo];
  47. }
  48.  
  49. /*
  50. void didReceiveRemoteNotificationFetchCompletionHandler(id self, SEL _cmd, UIApplication *application, NSDictionary *userInfo, void(^)(UIBackgroundFetchResult)handler)
  51. {
  52.  
  53. }
  54. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement