iPhone SDK - opening links in a UITextView in a web view

前端 未结 6 1574
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 18:00

I have specified dataDetectorTypes on a UITextView so that URLs open in Safari when touched.

Is it possible to intercept this behaviour so I load the URL in a UIWebV

6条回答
  •  被撕碎了的回忆
    2021-01-06 18:48

    I did everyone a favor and answered your question with a blog post and demo app.

    http://52apps.net/post/879106231/method-swizzling-uitextview-and-safari http://github.com/marksands/UITextViewLinkOptions

    To expand on tt.Kilew's post, you create the category, but call your method something else such as customOpenURL. When you want to go back to Safari you do something called Method Swizzling. It looks like this:

    #import 
    ..
    Method customOpenUrl = class_getInstanceMethod([UIApplication class], @selector(customOpenURL:));
    Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));
    method_exchangeImplementations(customOpenUrl, openUrl);
    

    Just call this method to swap the openURL implementation with your customOpenURL implementation when you do and don't want to use Safari.

    Check out the demo app for more detail. Hope this helps! :)

    Edit

    If you don't want to risk your app getting rejected, you might want to check out a custom UITextView I developed to better suit the situation: https://github.com/marksands/MSTextView

提交回复
热议问题