Use of Wkwebview-fit screen width

Source: Internet
Author: User
Tags mongodb postgresql redis firewall


Recently in the app, you need to open a local HTML file, after iOS 8, Apple introduced a new framework Wekkit, with Wkwebview instead of UIWebView, because Wkwebview "accounted for the low memory, speed," the advantages of So just use Wkwebview to load the local HTML file.



First introduce the header file #import <webkit/webkit.h>, initialize the WebView:



Initialization



Wkwebview *webview = [[Wkwebview alloc] init];



Webview.frame = Self.view.bounds;



[Self.view Addsubview:webview];



Next, load the local text.html with WebView:



NSString *path = [[NSBundle mainbundle] pathforresource:@ "text" oftype:@ "html"];



Nsurl *bundleurl = [Nsurl fileurlwithpath:[[nsbundle mainbundle] Bundlepath]];



Nserror *error = nil;



NSString *html = [[NSString alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding error:&error];



if (Error = = nil) {



[WebView loadhtmlstring:html Baseurl:bundleurl];



}



Finally command +r, found the problem, left and right both sides have a large gap. This can only be manually scaled by the customer, of course, the user experience is not good.
Original effect Diagram



In UIWebView, this scalespagetofit can be self-adaptive screen, but there is no such attribute in Wkwebview, in order to solve this problem, there are two methods: 1, directly in the HTML file source code to change. 2. Modify the size of the string in HTML by JS. The second method I used,



You first need to instantiate the wkwebviewconfiguration when instantiating the Wkusercontentcontroller class and assign it to the properties of Confiuration: Usercontentcontroller.



Wkwebviewconfiguration *wkwebconfig = [[Wkwebviewconfiguration alloc] init];



Adaptive screen Width js



NSString *jsstring = @ "var meta = document.createelement (' meta '); Meta.setattribute (' name ', ' viewport '); Meta.setattribute (' content ', ' width=device-width '); document.getElementsByTagName (' head ') [0].appendchild (meta); ";



Wkuserscript *wkuserscript = [[Wkuserscript alloc] initwithsource:jsstring injectiontime: Wkuserscriptinjectiontimeatdocumentend Formainframeonly:yes];



Add Adaptive screen Width js call method



[Wkucontroller Adduserscript:wkuserscript];



Wkwebview *webview = [[Wkwebview alloc] InitWithFrame:self.view.bounds configuration:wkwebconfig];



[Self.view Addsubview:webview];






Then load the text.html file as described above, Run
Modified effect Diagram



OK, although the title is a bit ugly, but the desired effect is out.


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.