Monday, February 06, 2012

Xcode snippet #3: Injecting JavaScript in UIWebView

How to get, for example, all the hyperlinks from a webpage, which is loaded in your iPhone app using the UIWebView control?
(Xcode's webbrowser counterpart)

Doing this in JavaScript is an easy task. All we have to do is to inject a JavaScript function into the loaded webpage which will do the work for us.

Injecting JavaScript into an UIWebView control can be achieved using the stringByEvaluatingJavaScriptFromString method, which returns the result of a given script. In other words you can run scripts with this method.

In the below example all links from a webpage are extracted and returned as string formatted as href|text|title.

-(void)getHyperlinks
{
    [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"  
     "script.type = 'text/javascript';"  
     "script.text = \"function getHyperlinks() { " 
     "var links = document.getElementsByTagName('a');"  
     "var allLinks = '';"
     "for (var i = 0; i<links.length; i++) {"
     "var link = links[i].href + '|' + links[i].text + '|' + links[i].title + ';';"
     "allLinks += link;"
     "} return allLinks;"
     "}\";"  
     "document.getElementsByTagName('head')[0].appendChild(script);"];  
    

    NSString *html = [webView stringByEvaluatingJavaScriptFromString: 
                      @"getHyperlinks();"];
    
    textView.text = html;
    
}
In the first step the JavaScript function getHyperlinks() is injected in the HTML document. At last the function is executed and its result is returned into a textview.

That's all!

75 comments:

youtube html5 player said...
This comment has been removed by a blog administrator.
kevin antony said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.


rpa Training in annanagar

blue prism Training in annanagar

automation anywhere Training in annanagar

iot Training in annanagar

rpa Training in marathahalli

blue prism Training in marathahalli

automation anywhere Training in marathahalli

blue prism training in jayanagar

automation anywhere training in jayanagar





shalinipriya said...

Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
Data Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data Science training in kalyan nagar
Data Science training in OMR
selenium training in chennai

Unknown said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this
Blueprism online training

Blue Prism Training in Pune

sathya shri said...

I was recommended this web site by means of my cousin. I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

angularjs Training in chennai
angularjs-Training in pune

angularjs-Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram

Unknown said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.

devops online training

aws online training

data science with python online training

data science online training

rpa online training

rose said...

Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!
Microsoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training

jaanu said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
data analytics course malaysia

gautham said...

excellnet post to learn on these technologies blockchain online training india.

easylearn said...

Hi,
Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take Data Science Training in btm. Because data science course in Bangalore is one of the best that one can do while choosing the course.

Vijiaajith said...

Really nice...
freeinplanttrainingcourseforECEstudents
internship-in-chennai-for-bsc
inplant-training-for-automobile-engineering-students
freeinplanttrainingfor-ECEstudents-in-chennai
internship-for-cse-students-in-bsnl
application-for-industrial-training

Vijiaajith said...

Good keep posting
interview-questions/aptitude/permutation-and-combination/how-many-groups-of-6-persons-can-be-formed

tutorials/oracle/oracle-delete

technology/chrome-flags-complete-guide-enhance-browsing-experience/

interview-questions/aptitude/time-and-work/a-alone-can-do-1-4-of-the-work-in-2-days


interview-questions/programming/recursion-and-iteration/integer-a-40-b-35-c-20-d-10-comment-about-the-output-of-the-following-two-statements

Shalini Kumar said...
This comment has been removed by the author.
Anand Shankar said...
This comment has been removed by the author.
nivetha said...

hii good...
internships for cse students in bangalore
internship for cse students
industrial training for diploma eee students
internship in chennai for it students
kaashiv infotech in chennai
internship in trichy for ece
inplant training for ece
inplant training in coimbatore for ece
industrial training certificate format for electrical engineering students
internship certificate for mechanical engineering students

big data analytics malaysia said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.


big data analytics malaysia

Data Science Course said...

Awesome Post!!! I really enjoyed reading this article. It's really a nice experience to read your post. Thanks for sharing.
Data Science Course in Marathahalli
Data Science Course Training in Bangalore

DataScience Specialist said...

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.
Data Science Course in Bangalore

DataScience Specialist said...

Your music is amazing. You have some very talented artists. I wish you the best of success.
Data Science Training in Bangalore

Anonymous said...

I cannot thank Mr Benjamin service enough and letting people know how grateful I am for all the assistance that you and your team staff have provided and I look forward to recommending friends and family should they need financial advice or assistance @ 1,9% Rate for Business Loan .Via Contact : .  lfdsloans@outlook.com. WhatsApp...+ 19893943740. Keep up the great work.
Thanks, Busarakham.

DataScience Specialist said...

cool stuff you have and you keep overhaul every one of us
Data Science Training in Bangalore

DataScience Specialist said...

Writing with style and getting good compliments on the article is quite hard, to be honest.But you've done it so calmly and with so cool feeling and you've nailed the job. This article is possessed with style and I am giving good compliment. Best!
Data Science Course in Bangalore

rocky said...

This article is very nice blogs. It content is very awesome weblogs.
Python Training in Chennai | Certification | Online Training Course | Python Training in Bangalore | Certification | Online Training Course | Python Training in Hyderabad | Certification | Online Training Course | Python Training in Coimbatore | Certification | Online Training Course | Python Training in Online | Python Certification Training Course




Data Science Course in Hyderabad said...

very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post!
Data Science Course in Hyderabad | Data Science Training in Hyderabad

devi said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog. Please, continue to give me such valuable posts.

Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course

EXCELR said...

Thanks for sharing nice information. I highlyrecommend you data science training Hyderabad

Jackie Co Kad said...

Great Article
Artificial Intelligence Projects

Project Center in Chennai



JavaScript Training in Chennai

JavaScript Training in Chennai

Data Science Hyderabad said...

Nice blog and great information on Coding Power thank you.

Data Science Course in Hyderabad

Data Science Training said...

Glad i found this blog resource you provided on coding was excellent thank you sharing.

Data Science Training in Hyderabad

Data Science said...

Information on coding was valuable information thanks for sharing.
Data Analytics Certification Training 360DigiTMG

360digitmgdelhi said...

This is actually the data I'm searching for, I was unable to have requested a less difficult perused with extraordinary tips this way... Much appreciated!
data science course in delhi

EXCELR said...

Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.data science courses

Data Science said...

I feel very grateful for the information provided it was very useful and top quality blog thanks for sharing.
Data Analytics Course Online

Artificial Intelligence Course said...

They are produced by high level developers who will stand out for the creation of their polo dress. You will find Ron Lauren polo shirts in an exclusive range which includes private lessons for men and women.

Artificial Intelligence Course in Bangalore

Rohini said...

You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
best data science courses in hyderabad

Tableau Course in Bangalore said...

Really fine and interesting informative article. I used to be looking for this kind of advice and enjoyed looking over this one. Thank you for sharing. Tableau Course in Bangalore

EXCELR said...

Excellent post and wonderful blog, Very good writing a university college and collecting information on this topic .I really like this type of interesting articles keep it up. data science training

data scientist course said...

This is an awesome motivating article.I am practically satisfied with your great work.You put truly extremely supportive data. Keep it up. Continue blogging. Hoping to perusing your next post
data scientist certification

360digiTMG Training said...



I truly like your composing style, incredible data, thank you for posting.

business analytics course

traininginstitute said...

I am sure that this is going to help a lot of individuals. Keep up the good work. It is highly convincing and I enjoyed going through the entire blog.

Best Data Science courses in Hyderabad

salar said...

You could be a student or a fresher just out of college or a working professional that wants to shift their career into Digital Marketing can attend this digital marketing course

Data Analytics Course in Bangalore said...

It took me a while to read all the reviews, but I really enjoyed the article. This has proven to be very helpful to me and I'm sure all the reviewers here! It's always nice to be able to not only be informed, but also have fun!
Data Analytics Course in Bangalore

traininginstitute said...

A great website with interesting and unique material what else would you need.
digital marketing courses in hyderabad with placement

traininginstitute said...

A great website with interesting and unique material what else would you need.
digital marketing courses in hyderabad with placement

traininginstitute said...

A great website with interesting and unique material what else would you need.
digital marketing courses in hyderabad with placement

Pallavi reddy said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
artificial intellingence training in chennai

traininginstitute said...

Excellent effort to make this blog more wonderful and attractive.
digital marketing courses in hyderabad with placement

traininginstitute said...

I have read your excellent post. This is a great job. I have enjoyed reading your post first time. I want to say thanks for this post. Thank you...
digital marketing courses in hyderabad with placement

Priya Rathod said...

Your article increases the curiosity to learn more about this topic. Keep sharing your information regularly for future reference.
DevOps Training in Hyderabad
DevOps Course in Hyderabad

Rajendra Cholan said...

PYTHON COURSE IN CHENNAI | INFYCLE TECHNOLOGIES:

Infycle Technologies is the best Python training in Chennai organization in Chennai and is widely known for its outstanding performance in providing the best software training in Chennai. It is the ultimate goal of Infycle Technologies to provide high-quality software programming training in a 100% safe location and to build a solid career for every individual and young professional in the IT industry. Most importantly, students like 100% hands-on training, which is the specialty of Infycle Technologies. To continue your career on a solid foundation, please contact Infycle Technologies at 7502633633.
Best training center in Chennai

Business Analytics Course said...

I have voiced some of the posts on your website now, and I really like your blogging style. I added it to my list of favorite blogging sites and will be back soon ...

Business Analytics Course

data science said...



Great to become visiting your weblog once more, it has been a very long time for me. Pleasantly this article i've been sat tight for such a long time. I will require this post to add up to my task in the school, and it has identical subject along with your review. Much appreciated, great offer. data science course in nagpur

Digital Marketing Course said...

Very good message. I came across your blog and wanted to tell you that I really enjoyed reading your articles.

Digital Marketing Course in Bangalore

DataCharge said...

Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
<a href="https://360digitmg.com/india/data-analytics-certification-training-course-in-bangalore>Data Analytics training in Bangalore</a>

Professional Courses and Training said...

It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.

Best Ethical Hacking Institute in Bangalore

Maneesha said...

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
best digital marketing training institute in hyderabad

Unknown said...

It's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information. cloud computing training institute in gurgaon

Unknown said...

Amazing! Its really amazing piece of writing, I have got much clear idea concerning from this piece of writing. cyber security course in delhi

Unknown said...

There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job! business analytics course in mysore

Unknown said...

Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say superb blog! data analytics course in surat

Unknown said...

If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state. data science training in mysore

Educational Training and Learning said...

Very good message. I came across your blog and wanted to tell you that I really enjoyed reading your articles.
Data Scientist Course Syllabus

Data Science said...

Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing.data analytics course in kolhapur

Unknown said...

Excellent effort to make this blog more wonderful and attractive. data science training in mysore

Unknown said...

I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up.. data scientist course in mysore

David Fincher said...

This post is so interactive and informative.keep update more information...
AWS Training in Anna Nagar
AWS Training in Chennai

Unknown said...

Excellent work done by you once again here. This is just the reason why I’ve always liked your work. You have amazing writing skills and you display them in every article. Keep it going! data science course in surat

Unknown said...

This post is very simple to read and appreciate without leaving any details out. Great work! data science training in mysore

Unknown said...

Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog. business analytics course in surat

Unknown said...

This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post. data science training in surat

traininginstitute said...

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
data scientist course in malaysia

PMP Training in Malaysia said...

360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

traininginstitute said...

I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at this place
cyber security course in malaysia

Professional Career Technology said...

Data Science is a growing domain with multitude of job opportunities, start your Data Scientsist Course today with 360DigiTMG and become a Data Scientist


Data Science Course in Bangalore with Placement

Muskan said...

This code snippet is quite handy for extracting hyperlinks from a webpage loaded in an iOS app's UIWebView. Injecting JavaScript to perform this task is a clever approach. Thanks for sharing this solution!
Also Read: How to Choose the Right Data Science Course for Your Skill Level and Learning Style

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...