Preview UIWebView in iCarousel in Swift - ios

I am writing a app in Swift where I am trying to load a preview of images using iCarousel of html pages for which I am using UIWebView.
Now the problem I have is I am unable to preview html pages, however when I trying to preview images, I am able to preview it.
Here is my code
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
var webView : UIImageView!
let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, 200, 200))
webV.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!))
webV.contentMode = .ScaleAspectFit
if webView == nil{
webView = UIImageView(frame:CGRect(x:0,y:0,width:200,height:200))
webView.contentMode = .ScaleAspectFit
}else{
webView = view as! UIImageView
}
webView.image = UIImage(named:"left-arrow.png")!
return webView //returns image carousel
//return webV //returns blank carousel. I want this to work !!!
}
How do I achieve this functionality ?
Edit 1
I can preview images for
carousel.type = iCarouselType.Linear screen attached.
This is how it looks
but I want it for any other carousel, say
carousel.type = iCarouselType.CoverFlow this is what I get
This is what I get when I am returning UIImageView

I have achieved this functionality by using WKWebView instead of UIWebView
Here is the working code
let webV:WKWebView!
webV = WKWebView(frame: CGRectMake(20, 20, 200, 200))
var url = NSURL(string:"http://www.fb.com/")
var req = NSURLRequest(URL:url!)
webV!.loadRequest(req)
return webV
Also,make sure you have imported WebKit by adding the below line
import WebKit

Related

How to display web page in ARKit ios

Which spritekit or sceneKit node should I use to display a web page in Arkit? As we usually embed other iOS views in arplane as materials.
I got empty pages containing background color of the loaded site after I tried to directly add my WKWebView instance to my node like this:
node.geometry?.firstMaterial?.diffuse.contents = webView
The only way I could show a WKWebView content in my ARKit 3D environment was to load the webView, wait for it to fully load and then take a screenshot of the loaded page and add it to my node.
class ViewController: UIViewController, ARSCNViewDelegate, WKNavigationDelegate {
#IBOutlet var sceneView: ARSCNView!
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 500, height: 500), configuration: WKWebViewConfiguration())
...
override func viewDidLoad() {
super.viewDidLoad()
...
webView.navigationDelegate = self
let url = URL(string: "https://stackoverflow.com")!
webView.load(URLRequest(url: url))
}
// WKWebView delegate method
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Page should be loaded by now. However sometimes it took even more to load, so you can use delay and wait more, then take the screenshot.
let screenshot = webView.screenshot()
let node = SCNNode()
node.geometry = SCNPlane(width: 2, height: 2)
node.geometry?.firstMaterial?.diffuse.contents = screenshot
node.geometry?.firstMaterial?.isDoubleSided = true
node.position = SCNVector3(0.1, 0.2, -2)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
screenshot method for webView:
extension WKWebView {
func screenshot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0);
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true);
let snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
}
WKWebView didn't work well with ARKit. UIWebView can show the contents in ARKit.
SpriteKit is used for 2D views and SceneKit for 3D views, so SpriteKit would be more appropriate. Although you can use 2D planes in 3D

WKWebview not fully loaded in iOS 10.3

I got the problem that I'm loading a local html file in a WKWebview.
My procedure is working for iOS 8, 9, 11 but not for 10.3. There is just white space in the middle of the text.
I'm creating my WKWebview like this:
let webConfiguration = WKWebViewConfiguration();
let frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: self.webViewHeight);
infoWebView = WKWebView(frame: frame, configuration: webConfiguration);
infoWebView.navigationDelegate = self;
infoWebView.translatesAutoresizingMaskIntoConstraints = false
infoWebView.scrollView.isScrollEnabled = false;
infoWebView.clipsToBounds = false;
infoWebView.allowsBackForwardNavigationGestures = false
infoWebView.contentMode = .scaleToFill
infoWebView.sizeToFit()
The loading of my html file is done by the following code:
if let availableUrl = url {
let urlRequest = URLRequest(url: availableUrl)
infoWebView.load(urlRequest)
}
Do you have an idea why it's working for iOS 8,9,11 but not for iOS 10.3?
Can somebody reproduce this problem?
Kind regards and have a nice day!
Since I had my wkwebview in a table view I had to override scrollViewDidScroll from UITableViewDelegate.
this code from WKWebView not rendering correctly in iOS 10 did the trick for me:
// in the UITableViewDelegate
func scrollViewDidScroll(scrollView: UIScrollView) {
if let tableView = scrollView as? UITableView {
for cell in tableView.visibleCells {
guard let cell = cell as? MyCustomCellClass else { continue }
cell.webView?.setNeedsLayout()
}
}
}

Difficulty loading Image in Swift with Kingfisher

I have a strange problem. I am trying to use Kingfisher in order to load and an cache an Image from Firebase in my app. The problem is that kKingfisher does not download the image. I am using a Placeholder Image that is locally stored and the scroll view displays that Image. If I remove the placeholder part from the command, the app crashes. So I know the Kingfisher function works by at least placing the placeholder Image into the UIImageView but not the image from the URL. Here is the code:
Can you point me to the right direction?
import UIKit
import Kingfisher
class Backprogramme: UIViewController {
override var prefersStatusBarHidden: Bool {
return true
}
#IBOutlet var BackPScroll: UIScrollView!
var imageArray = [UIImage]()
var folie1Image = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
let folie1URL = URL(string: "https://firebasestorage.googleapis.com/v0/b/backmaster-cdb60.appspot.com/o/Folie1.PNG?alt=media&token=efcb8e93-b817-41f3-a96f-946fd47cf468")!
folie1Image.kf.setImage(with: folie1URL, placeholder: #imageLiteral(resourceName: "first"))
imageArray = [folie1Image.image!]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.image = imageArray[i]
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.BackPScroll.frame.width, height: self.BackPScroll.frame.height)
BackPScroll.contentSize.width = BackPScroll.frame.width * CGFloat(i+1)
BackPScroll.addSubview(imageView)
}
// Do any additional setup after loading the view.
}
}
I had the same issue , discovered I had this in the log "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection" .
I fixed it by adding NSAppTransportSecurity as a Dictionary in the info.plist and then I added Allow Arbitrary Loads boolean with YES as a value . It worked instantly.

How to add sticker VIEWS to browser view in swift?

Alright, like everyone Im new to ms stickers in Swift but I am trying to figure out the purpose of / difference between an mssticker and msstickerview. I have read the API here https://developer.apple.com/reference/messages/msstickerview/1648434-sticker but cant find an answer to this relatively simple problem - it seems you can only add MSStickers (not StickerViews) to an MSStickerBrowserView, which is the only way to display them. I however need to add StickerVIEWS because I have a custom sticker view class I am trying to implement.
My stickers are added to my browser view here:
func loadStickers() {
var url: URL?
var i = 1
while true {
url = Bundle.main.url(forResource: "test\(i)", withExtension: "png") //change test for packs
print("URL IS THIS: \(url)")
guard let url = url else { break }
//make it a sticker
let sticker = try! MSSticker(contentsOfFileURL: url, localizedDescription: "")
let stickerView = InstrumentedStickerView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
stickerView.sticker = sticker
stickerView.delegate = self
stickerViews.append(stickerView)
stickers.append(sticker)
i += 1
}
}
func createStickerBrowser() {
let controller = MSStickerBrowserViewController(stickerSize: .regular)
addChildViewController(controller)
view.addSubview(controller.view)
controller.stickerBrowserView.backgroundColor = UIColor.white
controller.stickerBrowserView.dataSource = self
//resize this programmatically later
view.topAnchor.constraint(equalTo: controller.view.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: controller.view.bottomAnchor).isActive = true
view.leftAnchor.constraint(equalTo: controller.view.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: controller.view.rightAnchor).isActive = true
}
As you can see, I am creating both a sticker and a sticker view for each sticker - my stickers are stored in the stickers array and sticker views in stickerViews array.
Here is how the browser is populated:
func numberOfStickers(in stickerBrowserView: MSStickerBrowserView) -> Int {
return stickers.count
}
func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSSticker {
return stickers[index] //this isnt displaying stickerveiws only stickers
}
I have tried changing the return type on these methods to StickerView and returning the stickerView array instead
func stickerBrowserView(_ stickerBrowserView: MSStickerBrowserView, stickerAt index: Int) -> MSStickerView {
return stickerViews[index] //this isnt displaying stickerveiws only stickers
}
however this gets me the following error:
messagesviewcontroller does not conform to protocol
msstickerbrowserviewdatasource
Because the required function isn't being implemented as it was before. How does one display sticker views? What am I doing wrong?
You cannot add MSStickerViews to MSStickerBrowserView. In order to use your subclass, you will have to build your own interface per Apple's documentation for MSStickerBrowserView:
If you need additional customizations, you must build your own user interface using MSStickerView objects.
If you want to emulate the look of the browser view, you can just use a UICollectionView and populate the cells with your sticker views

How do I Add A GIF/Video To Landing Screen Background In Xcode 6 using swift?

what is the most efficient way to add a GIF/Video to the background of the landing screen ( home screen or first view controller) of my app in Xcode? i.e apps like spotify, uber, insagram etc. Being that my app is universal, how would i make it fit accordingly?
Do you mean the first screen that is displayed after your app is launched? If so: unfortunately you can't have dynamic content; you won't be able to use a gif/video.
That said, what you can do if you have some app-setup on background threads that will take some time anyway, or if you simply want the user to wait longer before interaction so that you can display the gif/video, you can make the static image match the first frame of the gif/video, and have your your entry point be a ViewController that displays the actual gif/video. Because this would delay the time to interaction, though, this would never be recommended.
As for making it fit: as of iOS 8 Apple recommends using LaunchScreen.xib. With it you can use Auto Layout to achieve universality.
To add a video you can use MPMoviePlayerController, AVPlayer, or if you're using SPritekit you can use an SKVideoNode.
EDIT (in response to follow-up comments):
An NSURL is a reference to a local or remote file. This link will give you a decent overview. Just copy the movie in and follow that guide.
In addition to the MPMoviePlayerController solution Saqib Omer suggested, here's an alternative method that uses a UIView with an AVPlayerLayer. It has a button on top of the video as an example, since that's what you're looking for.
import AVKit
import AVFoundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Start with a generic UIView and add it to the ViewController view
let myPlayerView = UIView(frame: self.view.bounds)
myPlayerView.backgroundColor = UIColor.blackColor()
view.addSubview(myPlayerView)
// Use a local or remote URL
let url = NSURL(string: "http://eoimages.gsfc.nasa.gov/images/imagerecords/76000/76740/iss030-e-6082_sdtv.mov") // See the note on NSURL above.
// Make a player
let myPlayer = AVPlayer(URL: url)
myPlayer.play()
// Make the AVPlayerLayer and add it to myPlayerView's layer
let avLayer = AVPlayerLayer(player: myPlayer)
avLayer.frame = myPlayerView.bounds
myPlayerView.layer.addSublayer(avLayer)
// Make a button and add it to myPlayerView (you'd need to add an action, of course)
let myButtonOrigin = CGPoint(x: myPlayerView.bounds.size.width / 3, y: myPlayerView.bounds.size.height / 2)
let myButtonSize = CGSize(width: myPlayerView.bounds.size.width / 3, height: myPlayerView.bounds.size.height / 10)
let myButton = UIButton(frame: CGRect(origin: myButtonOrigin, size: myButtonSize))
myButton.setTitle("Press Me!", forState: .Normal)
myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myPlayerView.addSubview(myButton)
}
}
For playing video add following code, declare class variable var moviePlayer:MPMoviePlayerController! . Than in your viewDidLoad()
var url:NSURL = NSURL(string: "YOUR_URL_FOR_VIDEO")
moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 0, y: 0, width: 200, height: 150)
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.Embedded
This will play video. But to fit it you need to add layout contraints. See this link to add constraints pragmatically.
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
// Load the video from the app bundle.
let videoURL: NSURL = NSBundle.mainBundle().URLForResource("video", withExtension: "mp4")!
// Create and configure the movie player.
self.moviePlayer = MPMoviePlayerController(contentURL: videoURL)
self.moviePlayer.controlStyle = MPMovieControlStyle.None
self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
self.moviePlayer.view.frame = self.view.frame
self.view .insertSubview(self.moviePlayer.view, atIndex: 0)
self.moviePlayer.play()
// Loop video.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loopVideo", name: MPMoviePlayerPlaybackDidFinishNotification, object: self.moviePlayer)
}
func loopVideo() {
self.moviePlayer.play()
}
https://medium.com/#kschaller/ios-video-backgrounds-6eead788f190#.2fhxmc2da

Resources